Python Cheatsheet
A quick reference guide for the Python programming language.
1. Variables
# Strings
first_name = 'Bukayo'
surname = 'Saka'
# Numbers
age = 24
height_cm = 178.00
2. Arithmetic Operators
# Addition
print(2 + 8)
# 10
# Subtraction
print(10 - 6)
# 4
# Multiplication
print(5 * 4)
# 20
# Division
print(12 / 3)
# 4.0
# Exponent
print(2 ** 5)
# 32
3. Comparison Operators
# Equal to
print(10 == 100)
# False
print('hello' == 'hello')
# True
print('hello' == 'Hello')
# False
# Not equal to
print(15 != 20)
# True
# Greater than
print(2 > 10)
# False
# Less than
print(10 < 100)
# True
# Greater than or equal to
print(10 >= 10)
# True
# Less than or equal to
print(20 <= 19)
# False
4. Control Flow
# If statements
age = int(input('Please enter your age: '))
if age >= 16:
print('You are allowed to vote in UK elections.')
else:
print('You are NOT allowed to vote in UK elections.')
# If-elif-else statements
test_score = int(input('Please enter your test score: '))
if test_score >= 90:
print('Grade A')
elif test_score >= 80:
print('Grade B')
elif test_score >= 70:
print('Grade C')
elif test_score >= 60:
print('Grade D')
else:
print('Fail')
5. Functions
# Example 1
def add(x, y):
sum = x + y
return sum
print(add(10, 20))
# 30
# Example 2
def to_fahrenheit(celsius):
return (celsius * 1.8) + 32
temperature_celsius = 28
temperature_fahrenheit = to_fahrenheit(temperature_celsius)
print(f'{temperature_celsius} degrees Celsius is equal to {temperature_fahrenheit} degrees Fahrenheit.')
# 28 degrees Celsius is equal to 82.4 degrees Fahrenheit.
6. Built-in Functions
# Print function
print('hello world')
# hello world
# Length function
print(len('hello world'))
# 11
# Input function
first_name = input('What is your first name? ')
print(f'Hi {first_name}!')
7. Data Structures
# Lists
shopping_list = ['apples', 'bananas', 'milk', 'cheese', 'rice']
print(shopping_list[0])
# apples
print(shopping_list[2])
# milk
print(shopping_list[-1])
# rice
print(shopping_list[-2])
# cheese
print(shopping_list[0:3])
# ['apples', 'bananas', 'milk']
print(len(shopping_list))
# 5
# Membership Operator
item_to_remember = 'bananas'
if item_to_remember in shopping_list:
print(f'{item_to_remember} are in my shopping list!')
# bananas are in my shopping list!
8. Loops
# For loops
shopping_list = ['apples', 'bananas', 'milk', 'cheese', 'rice', 'chocolate', 'bread']
for item in shopping_list:
print(item)
# apples
# bananas
# milk
# cheese
# rice
# chocolate
# bread
9. Formatted String Literals
# Format strings
first_name = 'Dennis'
surname = 'Bergkamp'
print(f'Hello {first_name} {surname}!')
# Hello Dennis Bergkamp!
10. String Manipulation
# Transform to upper case
greeting = 'Hello world!'
print(greeting.upper())
# HELLO WORLD!
# Transform to lower case
print(greeting.lower())
# hello world!
# Transform to title case
print(greeting.title())
# Hello World!
# Remove whitespace
original_sentence = ' Arsenal are the best football team in the world! '
clean_sentence = original_sentence.strip()
print(clean_sentence)
# Arsenal are the best football team in the world!
# Replace substrings
updated_sentence = clean_sentence.replace('football', 'sports')
print(updated_sentence)
# Arsenal are the best sports team in the world!
# Count substring occurrences
tongue_twister = 'How much wood would a woodchuck chuck if a woodchuck could chuck wood?'
print(tongue_twister.count('wood'))
# 4