Python Basics Cheat Sheet
Python Variable
Python
Copy
myVar = "Hello" my_var = "Hello World"
Python Comments
Python
Copy
# This is a comment # This is a multi line comment my_var = "Hello World" # this is inline comment def foo(): """ This is a function docstring """
Python Print Function
Python
Copy
Print("Hello World") # Output : Hello World my_num = 5 print("My Number is ", my_num) # Output : My Number is 5 # Print with sep keyword print("Sunday", "Monday", "Tuesday", sep=',') # Output : Sunday, Monday, Tuesday
Python Input Function
Python
Copy
print("Enter your name") your_name = input() print("Hello, ", my_name) # Output : # Enter your name # Hello, your name print("Hello, {}".format(my_name)) # Output : # Hello, your name my_name = input("Enter your name") print(f"Hello, {my_name}") # Output : # Enter your name # Hello, your name
Python Length
Python
Copy
# Length of a string len("Welcome") # Output : # 7 # Length of a list len([1,2,3]) # Output : # 3
Python Conversion Function
Python
Copy
# Convert to string str(5) # '5' # Convert to integer int('10') # 10 # Convert to float float(12) # 12.0
Python Math Operators
Operators | Operation | Example |
---|---|---|
** | Exponent | 2 ** 4 = 16 |
// | Integer Division | 22 // 8 = 2 |
(*) | Multiplication | 3 * 3 = 9 |
(/) | Division | 6 / 3 = 2.0 |
(%) | Modulus / Reminder | 9 % 2 = 1 |
(+) | Addition | 2 + 2 = 4 |
(-) | Substraction | 4 - 2 = 2 |