print('Hello World') print(1000) print(True) input() Grab user input
username = input('Enter your name: ')
# Even expressions can be printed age = int(input('Enter your age: ')) print(2 + 2) # Output: 4
Control Flow Test condition for truth
Comments Used for documentation if grade >= 90: username = 'Lovelace' # I'm a comment print('A') elif grade >= 80: print('B') Variables Store data for later use elif grade >= 70: print('C') acceleration = 'Δ velocity/ Δ time' else: gravity = 9.81 print('D')
print(gravity) # Output: 9.81
Relational Operators Compare two values
Data Types Specify the type variable a == b # Equal to
a != b # Not equal to # Integer a < b # Less than secret_number = 42 a <= b # Less than or equal to a > b # Greater than # Float a >= b # Greater than or equal to pi = 3.14
# String Logical Operators Combine two conditions
greeting = 'Hi bestie!' a and b # True if both are True # Boolean a or b # True if at least one is True earth_is_flat = False not a # True if a is False
Random Generate a random number
Arithmetic Operations Everyday math operations import random # Addition num = random.randint(1, 9) sum = 10 + 12 # Subtraction difference = 30 - 8 Loops Run code over and over # Multiplication product = 24 * 0.75 # While loop # Division while(coffee < 1): quotient = 81 / 9 print('Tired') # Modulus remainder = 76 % 4 # For loop # Exponents for i in range(10): exponent = 2 ** 3 print(i)