Guide Python Beginner

4.1 Introduction to Python Syntax

What Python syntax is, why it matters, how the interpreter reads a Python file top to bottom, and the difference between a syntax error and a logical error.

4 min read
Python Syntax

1.1 What Is Python Syntax?

Syntax means the rules for writing valid Python code. Just as a sentence needs correct grammar, a Python program needs correct syntax.

For example, this is valid Python:

print("Python is easy to read")

Python understands the print() function and the text inside the quotes.

1.2 Why Does Syntax Matter?

Python checks your syntax before it runs the program. If the syntax is incorrect, Python stops and shows an error.

print("Hello")

This code is valid. The following code is missing a closing parenthesis:

print("Hello"

Python reports a SyntaxError because it cannot understand the line.

1.3 Python Reads Code from Top to Bottom

Python normally runs statements in order, starting with the first line:

print("Step 1")
print("Step 2")
print("Step 3")

The output is:

Step 1
Step 2
Step 3

This makes the order of your code important.

flowchart TD A[Read first line] --> B[Read next line] B --> C[Run the instruction] C --> D{More lines?} D -->|Yes| B D -->|No| E[Program ends]

1.4 Python Uses Indentation

Indentation means spaces at the beginning of a line. Python uses indentation to show which lines belong inside a block.

age = 20

if age >= 18:
  print("You are an adult")

The indented print() line belongs to the if statement.

Use four spaces for each indentation level:

if True:
  print("This line is inside the if block")

Do not use random numbers of spaces:

if True:
  print("This may cause an indentation problem")

Most editors can insert four spaces automatically when you press Tab.

1.5 Colons Start Code Blocks

Statements such as if, for, while, and function definitions use a colon at the end of the first line:

if 10 > 5:
  print("The condition is true")

The colon tells Python that an indented block is coming next.

1.6 Comments

Comments are notes for people reading the code. Python ignores comments when it runs the program.

# Check whether the server is healthy
server_is_healthy = True
print(server_is_healthy)

Use comments to explain why something is done when the code is not obvious.

1.7 Names and Values

You can store a value in a variable by giving it a name:

server_name = "web-01"
server_port = 8080

print(server_name)
print(server_port)

Use clear names with lowercase letters and underscores:

retry_count = 3

Avoid unclear names:

x = 3

Python names cannot contain spaces and cannot start with a number.

1.8 Syntax Errors and Logic Errors

Syntax Error

A syntax error means Python cannot understand the code. The program stops before it starts:

if True
  print("Hello")

This code is missing a colon.

Logic Error

A logic error means the code is valid, but the result is wrong:

price = 100
discount = 20

final_price = price + discount  # The intended operation was probably subtraction.
print(final_price)

Python runs this code because the syntax is valid. It cannot know what result you intended.

1.9 Syntax Error and Runtime Error

A runtime error happens while the program is running:

server_count = 3
print(server_count + " servers")

Python cannot add a number and text, so it raises an error when it reaches that line.

Remember:

Error typeMeaning
SyntaxErrorPython cannot understand the code structure
Runtime errorA problem happens while the code is running
Logic errorThe code runs but gives the wrong result

1.10 Practice Exercise

Create a file named syntax-practice.py:

name = "Alex"
age = 25

if age >= 18:
  print(f"{name} is an adult")
else:
  print(f"{name} is a minor")

Run the file and then change the value of age. Observe how the output changes.

Interview Questions

  • What is syntax in Python?
  • Why is indentation important in Python?
  • What does a colon mean after an if statement?
  • What is the difference between a syntax error and a logic error?
  • What is the difference between a runtime error and a syntax error?
  • How should Python variables be named?

Quick Interview Answer

Python syntax is the set of rules used to write valid Python code. Python uses indentation to define code blocks, and statements such as if and for use a colon before an indented block. A syntax error stops the program before it runs, while a logic error allows the program to run but produces the wrong result.

Key Takeaways

  • Syntax is the grammar of Python code.
  • Python reads code from top to bottom.
  • Indentation defines blocks of code.
  • Colons start blocks after statements such as if and for.
  • Comments begin with #.
  • Syntax errors stop a program before it runs.
  • Logic errors produce incorrect results without necessarily showing an error.

Add More Questions to This Guide

Know a question that should be here? Share it and help the community!

Open Google Form