Python Cheat Sheet
Python Cheat Sheet
Variable syntax: A variable is created the moment you ‘def’ is a keyword defining that this is a function. Negative Indexing: Negative indexing means
first assign a value to it. Variables do not need to be beginning from the end.
declared with any particular type and can even change There are no return types for functions. my_list[-1] # Returns 5
type after they have been set. You can add a return statement and return any value.
variableName = value Looping through a list:
Function call: for x in my_list:
Data types: function_name() print(x)
Typ Description Examples
e Example: List slicing:
my_list[1:3] # Returns [2, 3]
int Integer (whole 103, - 12, def add_numbers(number1, number2): my_list[:-1] # Returns [1, 2, 3, 4]
result = number1 + number2
numbers)
return result Check if an item is in a list:
float 32 bit decimals (7 digits) 3.14 if 3 in my_list:
def greeting(): # Code for if condition is true
bool Boolean true/false print("Good morning!”)
Adding an item to a list:
str String "Hello" Working with Strings my_list.append(6) # Adds 6 to the end of my_list