Python Cheatsheet
Print Control Flow
print('Hello World!')
if grade >= 90:
print(1000)
print('A')
print(3.14)
elif grade >= 80:
print(True)
print('B')
elif grade >= 70:
print('C')
else:
Comments print('D')
# I’m a comment!
print('Gabby') # I’m also one T.T Relational Operators
a == b # equal to
a != b # not equal to
Variables a >b # greater than
a <b # less than
secret_num = 42 # int
a >= b # greater than or equal to
gravity = 9.81 # float
a <= b # less than or equal to
username = '@snoopdogg' # str
earth_is_flat = False # bool
Random Number
Arithmetic Operations import random
num = random.randint(1, 9)
sum = 23 + 18
difference = 30 - 8
product = 10 * 2.5
quotient = 81 / 9
remainder = 76 % 4
Logical Operators
exponent = 2 ** 3
a and b # True if both are true
a or b # True if at least one is True
not a # True if a is false
User Input
username = input('Enter your name: ')
age = int(input('Enter your age: '))
Loops
# While loop
while coffee < 1:
print('Tired of Python ')
String Interpolation
# For loop
print(f'The square of {i} is {i*i}')
for i in range(10):
print(i)
Codédex