MacOSShortcutsWindows

Python Cheat Sheet for Python Developers

List of Python Cheat Sheet Code

Learn Python Cheat Sheet for Beginners

Main Python Data Types

Action Python Cheat Sheet
Boolean True/False
Integer 10
Float 10.01
String “123abc”
List [value1, value2, …]
Dictionary {key1:value1, key2:value2, …}

Python Built-in Function

Action Python Code
Prints x objects separated by y print(x, sep=’y’)
Prints s and waits for an input that will be returned input(s)
Returns the length of x (s, L, or D) len(x)
Returns the minimum value in L min(L)
Returns the maximum value in L max(L)
Returns the sum of the values in L sum(L)
Returns the absolute value of n abs(n)
Returns the n1 number rounded to n digits round(n1, n)

Python Special Characters

Action Python Code
Comment #
New line \n
Scape char \

Python String Operations

Action Python Code
Retrieves character at position i string[i]
Retrieves the last character string[-1]
Retrieves characters in range i to j string[i:j]

Python List Operations

Action Python Code Cheat Sheet
Defines an empty list list=[]
Stores x with index i list[i]=x
Retrieves the item with index i list[i]
Retrieves the last item list[-1]
Retrieves items in the range i to j list[i:j]
Removes the item with index i del list[i]

Python Numeric Operations

Action Python Code Cheat Sheet
Addition +
Subtraction
Multiplication *
Division /
Exponent **
Modules %
Floor division //

Python Comparison Operations

Action Python Code Cheat Sheet
Equal ==
Different !=
Higher >
Lower <
Higher or equal >=
Lower or equal <=

Python Dictionary & Boolean Operations

Action Python Code
Defines an empty dictionary dict={}
Stores x associated with keys k dict[k] = x
Retrieves the item with keys k dict[k]
Removes the item with keys k del dict[k]
Logical AND and
Logical OR or
Logical NOT not

Python String Methods

Action Python Code
Converts to uppercase string.upper()
Converts to lowercase string.lower()
Counts how many times x appears string.count(x)
Position of the x first occurrence string.find(x)
Replaces x for y string.replace(x,y)
Returns a list of values delimited by x string.strip(x)
Returns a string with L values joined by a string string.join(L)
Returns a string that includes formatted x string.format(x)

Python List Methods

Action Python Code
Adds x to the end of the list list.append(x)
Appends L to the end of the list list.extend(L)
Inserts x at i position list.insert(i,x)
Removes the first list item whose value is x list.remove(x)
Removes the item at position i and returns its value list.pop(i)
Removes all items from the list list.clear()
Returns a list of values delimited by x list.index(x)
Returns a string with list values joined by an S list.count(x)

Python Dictionary Methods

Action Python Code
Returns a list of keys dict.keys()
Returns a list of values dict.values()
Returns a list of pairs dict.items()
Returns the value associated with the keys k dict.get(k)
Removes the item associated with the keys and returns its value dict.pop()
Adds keys-values to dictionary dict.update(D)
Removes all key values from the dictionary dict.clear()
Returns a copy of the dictionary dict.copy()

Download Python Cheat Sheet PDF

Download Python Cheat Sheet PDF

READ NEXT:

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button