Python Beginner Cheat Sheet A4
Python Beginner Cheat Sheet A4
1. COMMON SYMBOLS
# Comment
() Parentheses (functions, conditions)
[] Square brackets (lists, indexing)
{} Curly braces (dictionaries, sets)
: Colon (after if, for, def, etc.)
; Semicolon (rarely used)
, Comma (separates items)
'' or "" Quotation marks (strings)
\n New line
\t Tab
3. OPERATORS
+ - * / # Math: Add, Subtract, Multiply, Divide
% ** // # Modulo, Power, Floor division
== != > < # Compare: equal, not equal, greater, less
>= <= # Compare: greater or equal, less or equal
and or not # Logical
= += -= # Assignment
4. STRINGS
s = 'Hello'
s.upper() # 'HELLO'
s[0] # 'H'
'lo' in s # True
len(s) # 5
6. IF / ELSE
age = 20
if age >= 18:
print('Adult')
else:
print('Minor')
7. LOOPS
for i in range(3):
print(i)
count = 0
while count < 3:
print(count)
count += 1
8. LISTS
nums = [1, 2, 3]
nums.append(4)
print(nums[0]) # 1
len(nums) # 4
9. DICTIONARIES
person = {'name': 'Tom', 'age': 25}
print(person['name'])
10. FUNCTIONS
def greet(name):
print('Hi', name)
greet('Leo')
11. MODULES
import math
print(math.sqrt(9))
import random
print(random.randint(1, 5))
12. COMMENTS
# This is a comment
'''This is
a multi-line comment'''