Free visual Python lesson

Python Syntax — Explained Visually

Syntax is the set of writing rules Python understands. Press Play to see how a simple program creates variables in memory.

Python code

1x = 10
2y = 3
3x = 20
4total = x + y
5print(total)

Variables / Memory

Memory is empty.

Execution

STEP 1 / 7Step 1 of 7. Program starts. Memory is empty.

Program starts. Memory is empty.

Output

Flow chart

RanNowTrueFalseChecked, not taken◇ decision · ▱ input/output
StartStartx = 10x = 10y = 3y = 3x = 20x = 20total = x + ytotal = x + yprint(total)print(total)EndEnd

Variables

name = "Ali"
age = 16
print(name, age)

No type keyword is needed. Python works out the type from the value.

Indentation and colons

Python uses indentation (usually 4 spaces) instead of curly brackets to group code. Lines that start a block — if, for, while, def, class — end with a colon.

if age >= 18:
    print("Adult")
else:
    print("Student")

Comments, print() and input()

# This is a comment — Python ignores it
city = input("Your city: ")
print("Hello from", city)

input() always returns text. Use int(input()) to get a number.

Common syntax errors

  • SyntaxError: a missing colon, bracket or quote.
  • IndentationError: lines in a block are not lined up.
  • NameError: a misspelled variable name (Python is case-sensitive).

Practice exercises

Try each one yourself first, then open it in the Visualizer to watch every line run.

Exercise 1

Introduce yourself

Store your name and age in variables, then print one sentence using both.

name = "Ali"
age = 15
# print: Ali is 15 years old
Hint

Use commas inside print() to join a text and a number.

Show solution and explanation
name = "Ali"
age = 15
print(name, "is", age, "years old")

Each variable is a labelled box in memory. print() puts spaces between the items separated by commas, so no + or str() is needed.

Watch the solution step by step

Exercise 2

Fix the indentation

This program has an indentation error. Fix it so it prints both lines.

score = 8
if score > 5:
print("Good job")
print("Finished")
Hint

Lines that belong to the if must be pushed in by 4 spaces.

Show solution and explanation
score = 8
if score > 5:
    print("Good job")
print("Finished")

The colon starts a block, and the indented lines form that block. "Finished" is not indented, so it runs whether the condition is true or false.

Watch the solution step by step

Exercise 3

Update a variable

Start with coins = 10, add 5, then double it and print the result.

coins = 10
# add 5, then double
print(coins)
Hint

Write coins = coins + 5 and then coins = coins * 2.

Show solution and explanation
coins = 10
coins = coins + 5
coins = coins * 2
print(coins)  # 30

Python works out the right side first using the old value, then stores the result back in the same box. Watch coins change 10 → 15 → 30.

Watch the solution step by step

Try your own code

Paste any code into the free visualizer, or sign up free for all visual lessons with quizzes and progress tracking.