Python Syntax

A colourful cartoon-style illustration shows a confused student with orange hair shrugging in front of a computer screen. The screen displays a Python function with a missing indentation error:

When you learn a new spoken language, you must follow its grammar rules so others can understand you. Programming languages are the same. Python has a set of rules, called syntax, which your code must follow. If you break the rules, Python won’t understand and will give you an error.

This guide explains Python’s main syntax rules with examples. After each section, there’s a short exercise so you can practise.

Indentation: Spaces at the Start of a Line

In Python, indentation is not just about neatness — it’s part of the language. Indentation shows which lines of code belong together. Most programmers use four spaces for indentation.

Example:

If you forget the spaces, Python will show an IndentationError.

Bad example:

Exercise:

  1. Write an if statement that checks if 10 > 5.
  2. Inside it, indent and print "Ten is greater".
  3. Add another print without indentation that says "This is outside".

Comments: Notes for Humans

Comments are ignored by Python but help humans understand the code. They are useful for explaining what your program does.

  • Use # for a single-line comment.
  • Place a comment on its own line or at the end of code.

Example:

Exercise:

  1. Write a program that prints "I love Python".
  2. Add a comment at the top explaining what the program does.

Strings: Text in Quotes

A string is text. Strings are written inside quotes so Python knows they’re text and not code.

  • Single quotes: 'Hello'
  • Double quotes: "Hello"
  • Triple quotes for multiple lines: """Hello over several lines"""

Examples:

Exercise:

  1. Print your name inside a string.
  2. Print a sentence that goes over two lines using triple quotes.

Keywords: Special Reserved Words

Python has certain words that it keeps for its own use. These are called keywords, and you cannot use them for variable names.

Here is the full list of keywords in Python 3.12:

Correct use:

Incorrect use:

Exercise:

  1. Try creating a variable with the name class.
  2. Notice that Python gives an error.
  3. Then create a variable called my_class = "Python" and print it.

Case Sensitivity: Uppercase vs Lowercase

Python treats uppercase and lowercase letters as different.

Example:

Exercise:

  1. Create a variable called city = "London".
  2. Try printing both city and City. What happens?

Colons: Marking the Start of a Block

A colon (:) at the end of a line tells Python that a new block of code will follow. That block must be indented.

Examples:

Exercise:

  1. Write a function called say_hello that prints "Hello!".
  2. Remember to put a colon after def say_hello().
  3. Run the function by typing say_hello().

Line Breaks: One Instruction per Line

Each line in Python is usually one instruction.

Example:

If a line is too long, you can split it with a backslash (\).

Example:

Exercise:

  1. Write three print statements, each on its own line.
  2. Then write one long sentence across two lines using a backslash.

Summary

Python syntax is like grammar for code. The main rules are:

  • Indentation to group code
  • Comments to explain code
  • Strings in quotes for text
  • Keywords reserved for Python’s own use
  • Case sensitivity (upper and lower case matter)
  • Colons to mark blocks
  • Line breaks to separate instructions

By practising these rules, your Python code will be clear, correct, and easy to read.

Main Topic

Python Fundamentals

A colourful cartoon-style landscape illustration shows a person sitting at a computer terminal, looking overwhelmed by the amount of information on the screen. Papers and symbols float around them, representing different programming concepts. The word “Fundamentals” is clearly written above the scene, highlighting the focus on beginner Python basics.

Beginner’s guide to Python fundamentals: strings, numbers, booleans, syntax, and text manipulation explained simply and clearly.

Other Tutorials in this Topic

A playful cartoon-style landscape illustration shows a large elephant on the left and a small cat on the right. Between them, a bold greater than ( > ) symbol points toward the cat, representing a Python comparison operator in a fun way.

Python Operators

Beginner’s guide to Python operators: arithmetic, comparison, logical, assignment, membership, and identity explained with examples.

A colourful cartoon-style landscape illustration shows a smiling robot holding up two placards: one clearly labelled True and the other False. The background is bright and cheerful, symbolising the concept of Boolean values in Python programming.

Python Booleans

Learn Python Booleans: True or False values, comparisons, logic operators, and decision-making with simple examples.

ChatGPT said: A colourful cartoon-style landscape illustration shows two large labelled containers: one marked Integers filled with whole numbers like 7, 0, and -3, and another marked Floats filled with decimal numbers like 3.14, -0.5, and 2.0. A cheerful character is sorting the numbers into the correct containers, visually explaining the difference between integers and floating-point numbers in Python.

Python Numbers

Learn Python numbers: integers and floats, arithmetic operations, conversions, rounding, precision issues, and practical exercises for beginners…

A colourful cartoon-style landscape illustration shows the text “python ” written across the scene. Above the gap, a labelled box with the word “strings” is tilted, pouring its contents into the space, visually representing a variable being used to fill in a placeholder.

Python Strings – Part 4

Discover Python string formatting: use f-strings, .format(), and number formatting to neatly combine variables and text into…

A colourful cartoon-style landscape illustration shows the words “python strings” in lowercase being fed into a large whimsical machine. On the other side, the text emerges transformed into “PYTHON STRINGS” in uppercase letters, symbolising string methods like .upper(). The machine has gears and pipes, giving a fun and playful feel.

Python Strings – Part 3

Learn Python string methods: change case, remove spaces, replace text, split and join words, and check content…

A digital landscape illustration shows a beige fabric banner with the words “Python Strings” written across it. The material is ripped down the centre, with large visible stitches crudely sewing the two halves back together, symbolising fixing or escaping characters in strings. The background is simple and muted, drawing attention to the torn fabric.

Python Strings – Part 2

Explore Python strings further: escape characters, multi-line strings, and slicing to extract parts of text, with simple…

A colourful cartoon-style landscape illustration shows festive bunting strung across the top, with triangular flags spelling out “Python Strings” in bright, playful letters. The background is light and cheerful, giving a fun and welcoming atmosphere.

Python Strings – Part 1

Learn Python strings: text in quotes, printing, joining with +, counting length, and accessing characters by position…

A cartoon-style illustration shows a person’s hands typing on a laptop running Visual Studio Code in dark mode. The screen displays a Python file with the code print("Hello, world!"), alongside a large, colourful Python logo. The background is bright blue, creating a vibrant and engaging learning atmosphere.

Start Coding Python with VS Code

Beginner’s guide to installing Python, setting up VS Code, and getting ready for coding.

Python Variables

Python Variables

Learn Python variables with simple explanations, real-world analogies, practice tasks to build confidence using data in programs.