Python Variables

Variables & Dynamic Typing

x = 5
name = "Alex"  
print(x)
print(name)
5
Alex

Rules for Naming Variables

Valid Identifiers:

age = 21
_colour = "lilac"
total_score = 90

Assigning Values & Object Reference

Try it yourself

Experiment with variables and dynamic typing:

Variables Assessment

A question is generated for each topic below. Answer it and get instant AI feedback, or generate a new one.

Variables & Dynamic Typing

Rules for Naming Variables

Assigning Values & Object Reference

Python Operators

Operators are special symbols or keywords used to perform specific mathematical, relational, or logical computations on values and variables.

Arithmetic Operators

Used to perform standard mathematical calculations.

print(10 / 3)   # Output: 3.3333333333333335
print(10 // 3)  # Output: 3
print(2 ** 3)   # Output: 8

Comparison (Relational) Operators

Used to compare values, yielding a boolean outcome (True or False).

Logical Operators

Used to combine conditional evaluations.

x = 5
print(x > 3 and x < 10)  # Output: True

Try it yourself

Experiment with operators:

Operators Assessment

A question is generated for each topic below. Answer it and get instant AI feedback, or generate a new one.

Arithmetic Operators

Comparison (Relational) Operators

Logical Operators

Python Control Flow

Control flow lets your program make decisions and run different code based on conditions.

Boolean Expressions

Comparison operators compare values and produce a boolean result (True or False).

age = 18
print(age >= 18)   # Output: True
print(age == 21)   # Output: False
True
False

Logical Operators

score = 85
print(score > 50 and score < 100)  # Output: True
print(not score > 90)              # Output: True

if / elif / else

Python evaluates conditions top to bottom and runs the first block whose condition is true.

temperature = 30
if temperature > 35:
    print("Very hot")
elif temperature > 20:
    print("Warm")
else:
    print("Cool")
Warm

Indentation & Code Blocks

Nested Conditionals

You can place an if inside another if to check multiple levels of conditions.

age = 25
has_id = True
if age >= 18:
    if has_id:
        print("Entry allowed")
    else:
        print("ID required")
Entry allowed

Truthiness

In conditions, some values are treated as False (falsy) and most others as True (truthy).

name = ""
if name:
    print("Hello, " + name)
else:
    print("No name given")
No name given

Try it yourself

Experiment with conditionals:

Control Flow Assessment

A question is generated for each topic below. Answer it and get instant AI feedback, or generate a new one.

Boolean Expressions

Logical Operators

if / elif / else

Indentation & Code Blocks

Nested Conditionals

Truthiness

Python Loops

Loops let you repeat a block of code many times, which is essential for working with collections and repetitive tasks.

for loops

A for loop iterates over each item in a sequence (a range, list, or string).

for fruit in ["apple", "banana", "cherry"]:
    print(fruit)
apple
banana
cherry

range()

range() produces a sequence of numbers to loop over.

for i in range(3):
    print(i)          # 0 1 2

for i in range(1, 6, 2):
    print(i)          # 1 3 5

while loops

A while loop repeats as long as its condition is true. Be careful to update the condition so it eventually becomes false.

count = 0
while count < 3:
    print(count)
    count += 1
0
1
2

break / continue / pass

for i in range(10):
    if i == 3:
        continue   # skip 3
    if i == 6:
        break      # stop at 6
    print(i)       # 0 1 2 4 5

Nested loops

You can place a loop inside another loop. The inner loop runs fully for each iteration of the outer loop.

for row in range(2):
    for col in range(3):
        print(row, col)
0 0
0 1
0 2
1 0
1 1
1 2

Common loop patterns

total = 0
for n in [2, 4, 6]:
    total += n
print(total)   # Output: 12

for idx, val in enumerate(["a", "b"]):
    print(idx, val)   # 0 a / 1 b

Try it yourself

Experiment with loops:

Loops Assessment

A question is generated for each topic below. Answer it and get instant AI feedback, or generate a new one.

for loops

range()

while loops

break / continue / pass

Nested loops

Common loop patterns