All Go Rust Python PHP JavaScript
Chrome Dev Summit to secure your spot in workshops, office hours and learning lounges!

How to List All Files of a Directory in Python

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 ...
ada

How to Execute a Program or Call a System Command in Python

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...
Patcher56

How to Make a Time Delay in Python

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...
aweis

How to Get the Current Time in Python

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...
Patcher56

How to Merge two Dictionaries into a new Dictionary in Python

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 # -*-...
Unused

How to Check whether a File Exists in Python

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 ...
Patcher56

How To Generate a Random String in Python

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...
Sambhav Khandelwal

How To Convert string to integer in Python

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...
aweis

How to find the intersection between two lists in Python

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...
Unused

How to find the difference between two lists in Python

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...
Sambhav Khandelwal

How To Split a list into equally sized chunks in Python

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'] *...
Patcher56

How to Read a File Line-by-Line in Python

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...
pooriabt

How to to create a multiline string in Python

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...
aweis

How to fill a list with same values in Python

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'] *...
aweis

How to define python source code encodings

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...
Tomoki