Python · Beginner

Python Syntax and Your First Program

8 min readUpdated September 24, 2026Every example verified

In short: A Python program is a plain text file whose statements run from top to bottom, one per line. Blocks are marked by indentation instead of braces, lines need no semicolon, comments start with #, print() writes a line to the screen and input() reads one.

How Python reads a program

Python is an interpreted language: you save statements in a file ending in .py and the interpreter reads it from the first line to the last, carrying out each statement before moving on. There is no separate compile step to wait for and no required wrapper such as a main function. The shortest useful Python program is a single line.

Three habits of the language shape how every program looks. First, one statement usually occupies one line, and the end of the line ends the statement, so there is no semicolon to remember. Second, when several statements belong together as a block (the body of an if, a loop or a function) Python groups them by indentation: every line in the block starts with the same number of spaces, four by convention, and the block ends where the indentation returns to the previous level. Other languages use braces for this and leave indentation as a matter of taste; in Python the indentation is the structure, which is why two Python programs by different people tend to look alike.

Third, a # starts a comment that runs to the end of the line. The interpreter ignores it; it exists for the person reading the code. Names are case sensitive, so total and Total are different names, and print must be written in lower case.

Two built-in functions carry most first programs. print() writes its arguments to standard output, separating them with a space and ending the line with a newline. input() pauses, reads one line typed by the user (or one line of the program's standard input), and returns it as text without the trailing newline.

Syntax

 Python · syntax
# a comment: ignored by Python
print("text", 42)          # writes: text 42
line = input()             # reads one line of input as a string

if condition:              # a colon opens a block
    statement_inside_block # indented by four spaces
    another_statement
statement_after_block      # back at the left margin

Anything after # on a line is a comment. A colon at the end of a line means an indented block follows on the next line, and the block ends where the indentation goes back to the previous level.

A program of print statements

Watch how print() joins several arguments with a single space and how each call ends the line.

 Python
# Shop window notice
print("Rosewood Bakery")
print("Open from 7 to 3")
loaves = 48
print("Loaves baked today:", loaves)
print("Sourdough", "rye", "spelt", sep=" | ")

Output

Rosewood Bakery
Open from 7 to 3
Loaves baked today: 48
Sourdough | rye | spelt

Each print() call writes one line. When you pass several values, Python converts each to text and inserts one space between them, which is why 48 appears after the colon with a space and without quotes. The optional sep argument replaces that space; here it puts a bar between the bread names. The comment on the first line produces no output at all.

Reading a line of input

The program below is run with the single input line Priya.

 Python
name = input()
print("Welcome,", name)
print("Your badge has", len(name), "letters")

Input given to the program: Priya

Output

Welcome, Priya
Your badge has 5 letters

input() returns exactly what was typed, as a string, with the line break removed, and the assignment stores it under the name name. len() counts the characters in that string. Because print() takes any number of arguments, the number 5 can sit between two pieces of text without being converted by hand. Every exercise on this site works this way: the program reads its data from standard input and writes its answer to standard output.

Indentation marks a block

Two lines are indented under the if; one is not.

 Python
stock = 3
if stock > 0:
    print("In stock")
    print("Units:", stock)
print("Report finished")

Output

In stock
Units: 3
Report finished

The two indented lines belong to the if; they run only because stock > 0 is true. The last print is back at the margin, so it is outside the block and runs regardless. Change stock to 0 and the two indented lines disappear from the output while the last one stays. The conditions lesson covers if in full; for now the point is that indentation decides which lines belong together.

Common mistakes

  • Indenting a line that is not inside a block

    Why it goes wrong: Python treats unexpected indentation as a structural error, not a style choice. A print() indented at the top of a file raises IndentationError: unexpected indent before anything runs.

    Fix: Start every top-level statement at the left margin and indent only after a line that ends in a colon.

     Python · fix
    print("first")
    print("second")
  • Mixing tabs and spaces for indentation

    Why it goes wrong: Python 3 refuses a block whose lines are indented inconsistently with tabs and spaces (TabError). Editors display both as blank space, so the problem is invisible on screen.

    Fix: Configure your editor to insert four spaces when you press Tab, and use the same setting in every file.

  • Leaving off the colon before a block

    Why it goes wrong: if stock > 0 without a colon is a SyntaxError. The colon is what tells the interpreter that an indented block follows.

    Fix: End every if, elif, else, for, while and def line with a colon.

     Python · fix
    if stock > 0:
        print("In stock")
  • Writing print without parentheses

    Why it goes wrong: In Python 3, print is an ordinary function, so print "hi" is a syntax error. Code written for Python 2 used the statement form, and the interpreter's error message even hints at the missing parentheses.

    Fix: Always call it: print("hi").

Punctuation Python does not need

JobMany other languagesPython
End a statementsemicolon ;the end of the line
Group a blockbraces { }a colon, then indentation
Write a comment// or /* */#
Create a variableint x = 5;x = 5

Where you use this

Almost every automation script starts as exactly this shape: read a value or two, compute something, print the result. A script that turns a number of minutes typed by a colleague into hours and minutes, or that prints a checklist for the day, needs nothing beyond print(), input() and a few variables. Because a Python file runs directly with python3 script.py, such tools can be written and used within minutes. The same input()/print() convention is how every exercise here is checked: your program reads standard input and writes standard output, and the checker compares what you printed with the expected text.

 Python · in practice
minutes = int(input())
print(minutes // 60, "h", minutes % 60, "min")

Key points

  • Statements run top to bottom, one per line, with no semicolons.
  • A colon opens a block and consistent indentation (four spaces) marks its lines.
  • # starts a comment; comments are for readers, not for the interpreter.
  • print() writes one line and separates its arguments with a space.
  • input() returns one line of input as a string, without the newline.
  • Names are case sensitive: Total and total are different names.

Try it yourself

The program reads a city name and prints one line. Add a second line that prints Letters: followed by the number of characters in the name, using len().

Your program
city = input()
print("City:", city)
Input the program receives: Marseille
Expected output: City: Marseille Letters: 9

Practise this

Exercises for this lesson are in the Python practice set.

Open the Python playground

Frequently asked questions

Do I need a main function in Python?

No. Statements at the left margin of the file run as soon as the interpreter reaches them, so a complete program can be a single print() call. Larger programs often put their code inside functions and call them from a block such as if __name__ == "__main__":, but that is a convention for organising reusable modules, not a requirement of the language.

How many spaces should I indent in Python?

Any consistent amount works as long as every line of a block uses the same indentation, but the convention followed by the standard library and by the PEP 8 style guide is four spaces per level, with no tabs. Sticking to four spaces avoids TabError and keeps your code looking like everyone else's.

Why does input() give me text when I typed a number?

input() always returns a string, because it cannot know whether the line you typed is meant as a number, a name or a code. Convert it yourself with int() or float() when you need arithmetic; the data types lesson shows how and what goes wrong if you forget.

Progress is stored only in this browser.

How this page was checked. Every program on it was run with CPython 3.11 at build time; runs in your browser on CPython 3.14 (Pyodide) by the publishing checks, and the output shown is what it printed. The Run buttons execute the same code on your device; nothing you type is sent anywhere.