Skip to content
Topic 1 of 6

Hello World & Syntax

Write your first program and learn how Python reads lines, comments and indented blocks.

15 min read 4 pages 5 practice games Jump to practice ↓

Page 1

Your first program

Python is a programming language known for code that reads almost like English. A Python program is a text file of statements. The interpreter runs the statements one at a time, from top to bottom.

The classic first program prints a greeting:

python
print("Hello, World!")

print() is a built-in function: it writes the values you give it to the screen, followed by a newline. Text between quotes is a string. You can use single quotes or double quotes, as long as they match, so 'hi' and "hi" are the same string.

print() can take several values separated by commas. It puts a space between them by default:

python
print("Total:", 3 + 4)         # Total: 7
print("a", "b", "c", sep="-")  # a-b-c
print("Loading", end="...")    # Loading... (and no newline)

The sep parameter sets the text placed between values, and the end parameter sets what is printed after the last value (a newline by default). print() also has a file parameter, which chooses where the output goes (the screen by default), and a flush parameter, which forces the output to appear immediately.

Page 2

Comments and statements

A comment starts with a hash sign (#) and runs to the end of the line. Python ignores comments completely: they are notes for the people who read your code.

python
# This whole line is a comment
price = 20  # a comment can also follow code

Python has no special syntax for multi-line comments. To write a longer note, start each line with a hash sign. A string in triple quotes is sometimes used for notes, but it is a string, not a comment.

A statement is one instruction, such as an assignment or a call to print(). Each statement usually sits on its own line, and you don't need a semicolon at the end of the line. A semicolon can put two statements on one line, but PEP 8, Python's official style guide, discourages it.

A long expression can continue onto the next line while it is inside parentheses:

python
total = (1 + 2 + 3
         + 4 + 5)
print(total)  # 15

Page 3

Indentation and blocks

Python uses indentation to group statements into blocks. Many other languages use curly braces for this; Python uses the whitespace at the start of each line.

A line that ends with a colon (:) starts a block. Lines that start a block begin with keywords such as if, elif, else, for, while, def, class and try. Every line inside the block must be indented by the same amount, and the block ends at the first line that is indented less.

An if statement is written as: the keyword if, then a condition, then a colon, then an indented block on the following lines.

python
temperature = 30
if temperature > 25:
    print("It's hot")      # inside the block
    print("Drink water")   # inside the block
print("Done")              # outside the block: always runs

PEP 8 recommends 4 spaces per indentation level. Indenting a line when no block is open raises an IndentationError. Mixing tabs and spaces inconsistently raises a TabError, which is a kind of IndentationError.

The pass statement does nothing. Use it as a placeholder for a block that must exist but has no code yet:

python
if temperature < 0:
    pass  # handle freezing weather later

Page 4

Case sensitivity and errors

Python is case-sensitive: name, Name and NAME are three different names. print works, but Print raises a NameError, because Python doesn't know any name called Print. A NameError means the code uses a name that hasn't been defined.

Keywords are reserved words with a fixed meaning, such as if, else, True, False and None. They must be spelled exactly, capitals included: true (lowercase) is not a keyword, just an undefined name.

A SyntaxError means the code breaks Python's grammar rules, for example a missing closing quote or parenthesis. Python checks the grammar before it runs anything, so a program with a SyntaxError doesn't start at all. In Python 3, print needs parentheses: print "hi" was Python 2 syntax and is a SyntaxError in Python 3.

Finished reading?

Mark it as read for +20 XP. Optional: practice is open either way.

Practice

Pass any one game to unlock Topic 2: Variables & Types.

Sign in to play, save your best and unlock Topics.
  • Reach −1,500 m
  • Reach 150 km
  • 7/10 without falling
  • Clear both boards
  • 150 points

Learn more