In Python, there are 3 ways to list all files of a directory.
Using os.listdir Method
Return a list containing the names of the entries in the directory given by path. The list is in arbitrary order, and does not include the special entries '.' and ...
In Python, there are 3 ways to execute a program or call a system command.
Using subprocess Module
The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. See the following e...
In Python, there are 2 ways to make a time delay.
Using time.sleep Function
This function actually suspends execution of the calling thread for the given number of seconds. The argument may be a floating point number to indicate a more precise sleep...
In Python, there are 2 ways to get the current time.
Using datetime Module
The datetime module supplies classes for manipulating dates and times. See the following example:
#!/usr/bin/python3
# -*- coding: utf8 -*-
# Import module
import datet...
In Python, there are 3 ways to merge two dictionaries into a new dictionary.
Using Additional Unpacking Generalizations
Starting with Python 3.5, we can merge in with literal notation as well. See the following example:
#!/usr/bin/python3
# -*-...
In Python, there are 3 ways to check if a file exists.
Using os.path.exists Method
Return True if path refers to an existing path or an open file descriptor. Returns False for broken symbolic links. On some platforms, this function may return False ...
In Python, there are 3 ways to generate a random string.Using List Comprehension
List comprehensions provide a concise way to create lists. See the following example:
#!/usr/bin/python3
# -*- coding: utf8 -*-
# Import module
import random
impor...
In Python, there are 2 ways to convert string to int.
Using int Built-in Function
Return an integer object constructed from a number or string x, or return 0 if no arguments are given.
#!/usr/bin/python3
# -*- coding: utf8 -*-
s = "100"
# clas...
In Python, there are 4 ways to find the intersection between two lists.
Using Set Intersection
A set is an unordered collection with no duplicate elements. Basic uses include membership testing and eliminating duplicate entries. Set objects also sup...
In Python, there are 5 ways to find the difference between two lists.
Using Set Difference
A set is an unordered collection with no duplicate elements. Basic uses include membership testing and eliminating duplicate entries. Set objects also support...
In Python, there are 3 ways to split a list into equally sized chunks.
Using List Comprehension
List comprehensions provide a concise way to create lists. The following example:
#!/usr/bin/python3
# -*- coding: utf8 -*-
# initialize
lst = ['a'] *...
In Python, there are 3 ways to read a file line-by-line.
Using For Loop
For reading lines from a file, you can loop over the file object. This is memory efficient, fast, and leads to simple code:
#!/usr/bin/python3
# -*- coding: utf8 -*-
# read a...
4 ways to create multiple lines string in Python.Using triple-quotes
String literals can span multiple lines. One way is using triple-quotes: """...""" or '''...'''. End of lines are automatically included in the string, but it’s possible to pr...
3 ways to initialize a Python Array .Initialize a list with values
If you want to initialize a list of any number of elements where all elements are filled with any values, you can use the * operator as follows.
#!/usr/bin/env python3
arr = ['H'] *...
Correct way to define Python source code encoding.PEP 263
Python will default to ASCII as standard encoding if no other encoding hints are given.
PEP 263 defines how to declare Python source code encoding.
Using Coding
To define a source code encodin...