Python 3 is the latest version of the popular programming language, Python. It was released in 2008 and has since become the preferred version for many developers. Python 3 is an improvement over Python 2, with new features and enhancements that make it more powerful and easier to use.
One of the most significant changes in Python 3 is the way it handles strings. In Python 2, strings were represented as ASCII characters, which limited the range of characters that could be used. Python 3, on the other hand, uses Unicode, which allows for a much broader range of characters, including non-Latin characters.
Another major change in Python 3 is the print function. In Python 2, the print statement was used to output text to the console. In Python 3, the print function is used instead, which makes it easier to format output and avoid errors.
This cheat sheet provides a quick reference for some of the most commonly used Python 3 syntax and functions.
Table of Contents
Variables and Data Types
Syntax Description x = 5
Assigns the value 5
to the variable x
x = ""hello""
Assigns the string ""hello""
to the variable x
x = True
Assigns the boolean value True
to the variable x
x = None
Assigns the value None
to the variable x
type(x)
Returns the data type of x
int(x)
Converts x
to an integer float(x)
Converts x
to a float str(x)
Converts x
to a string bool(x)
Converts x
to a boolean list(x)
Converts x
to a list dict(x)
Converts x
to a dictionary
Operators
Syntax Description +
Addition -
Subtraction *
Multiplication /
Division //
Floor division %
Modulo **
Exponentiation =
Assignment ==
Equality !=
Inequality <
Less than >
Greater than <=
Less than or equal to >=
Greater than or equal to and
Logical and or
Logical or not
Logical not
Control Flow
Syntax Description if condition:
Executes the code block if the condition is true elif condition:
Executes the code block if the previous condition(s) were false and this condition is true else:
Executes the code block if all previous conditions were false for variable in iterable:
Loops through each item in the iterable, assigning it to the variable while condition:
Loops through the code block as long as the condition is true break
Exits the current loop continue
Skips the current iteration of the loop pass
Does nothing (used as a placeholder)
Functions
Syntax Description def function_name(parameters):
Defines a function with the given name and parameters return value
Returns the value from a function lambda parameters: expression
Defines an anonymous function with the given parameters and expression
Lists
Syntax Description my_list = []
Creates an empty list my_list = [1, 2, 3]
Creates a list with the given values my_list.append(value)
Adds the value to the end of the list my_list.insert(index, value)
Inserts the value at the given index my_list.remove(value)
Removes the first occurrence of the value from the list my_list.pop(index)
Removes and returns the value at the given index my_list.index(value)
Returns the index of the first occurrence of the value my_list.count(value)
Returns the number of times the value appears in the list my_list.sort()
Sorts the list in ascending order my_list.reverse()
Reverses the order of the list
Dictionaries
Syntax Description my_dict = {}
Creates an empty dictionary my_dict = {""key"": ""value""}
Creates a dictionary with the given key-value pair my_dict[key] = value
Adds or updates the key-value pair in the dictionary del my_dict[key]
Removes the key-value pair with the given key my_dict.keys()
Returns a list of all the keys in the dictionary my_dict.values()
Returns a list of all the values in the dictionary my_dict.items()
Returns a list of all the key-value pairs in the dictionary
Strings
Syntax Description my_string = """"
Creates an empty string my_string = ""hello""
Creates a string with the given value len(my_string)
Returns the length of the string my_string.upper()
Returns a new string with all uppercase letters my_string.lower()
Returns a new string with all lowercase letters my_string.capitalize()
Returns a new string with the first letter capitalized my_string.replace(old, new)
Returns a new string with all occurrences of the old string replaced with the new string my_string.split(separator)
Returns a list of substrings separated by the given separator my_string.join(iterable)
Returns a new string by concatenating the elements of the iterable with the string
File I/O
Syntax Description file = open(filename, mode)
Opens the file with the given filename and mode file.read()
Returns the contents of the file as a string file.readline()
Returns the next line of the file as a string file.readlines()
Returns a list of all the lines in the file file.write(string)
Writes the string to the file file.close()
Closes the file
Modules
Syntax Description import module_name
Imports the module with the given name from module_name import function_name
Imports the specified function from the module from module_name import *
Imports all functions from the module module_name.function_name()
Calls the specified function from the module
Reference
Python 3 Documentation