Skip to contentSkip to navigation

Getting Started, cheat sheet

Use these building blocks to write small Python programs that are clear and easy to test.

Printing

print("Hello, World!")
Display text inside quotes.
print("Score:", 10)
Print several values with spaces between them.
print("red", "blue", sep=" | ")
Choose the separator between printed values.
print("Loading", end="... ")
print("done")
Change what print adds at the end.

Syntax and comments

# Explain why this code exists
print("Ready")
Python ignores text after # on a line.
if True:
    print("Indented")
Indent four spaces inside a code block.
total = (10 + 20 +
         30)
Parentheses let a long expression continue on another line.
"""A note that spans
multiple lines."""
Triple quotes create a multi-line string.

Variables

city = "Dhaka"
print(city)
Assign a value to a descriptive name.
level = 1
level = 2
A later assignment replaces the earlier value.
first_name, age = "Asha", 12
Assign several names and values at once.
left, right = right, left
Swap two existing values in one assignment.

Names and constants

player_score = 0
Use lowercase names with underscores for clarity.
MAX_LIVES = 3
ALL_CAPS signals a value that should not change.
name = "Mina"
A quoted value written directly in code is a literal.
score = 10
Names cannot start with a digit or use a dash.

Types and conversion

print(type(42))
Check the type of a value.
count = int("7")
Turn whole-number text into an integer.
price = float("2.5")
Turn numeric text with a decimal into a float.
label = str(7)
is_ready = bool(1)
Convert values to text or a Boolean when needed.
All cheat sheets