Lessons → Python Fundamentals → Lesson 03
BEGINNER

Python Syntax

Learn the basic rules of Python syntax, indentation, comments and statements.

📖 15 min   •   📝 7 Questions

What is Python Syntax?

Syntax means the rules that define how a program should be written.

Python has a simple and readable syntax. Unlike some programming languages, Python uses indentation to define blocks of code.

Remember:

Incorrect syntax can cause a SyntaxError.

Python Statements

A statement is an instruction that Python executes.

Python
name = "Sudhanshu"
print(name)

Here, the first line stores a value in a variable and the second line displays it.

Comments in Python

Comments are notes written inside the program. Python does not execute comments.

A single-line comment starts with the # symbol.

Python
# This is a comment
print("Hello Python")
Why use comments?

Comments help programmers understand what different parts of the code do.

Python Indentation

Indentation means adding spaces at the beginning of a line.

Python uses indentation to identify a block of code.

Correct Example
if 10 > 5:
    print("10 is greater")

The spaces before print() show that it belongs to the if block.

Another Example
age = 18

if age >= 18:
    print("You can vote")

Python is Case-Sensitive

Python treats uppercase and lowercase letters as different.

Python
name = "Alex"
Name = "John"

print(name)
print(Name)

Here, name and Name are two different variables.

Quick Quiz

Test your understanding of Python syntax.

1. Which symbol is used for a single-line comment?

Finished this lesson?