Lesson 2 – Python Syntax

Python syntax refers to the rules that define how Python code must be written. Just as English has grammar rules, Python has structure rules that ensure your program makes sense to the computer. In this lesson, you will learn indentation, comments and the print function—three essential parts of every Python program.

Understanding Indentation

Python relies on indentation to group related instructions. If you forget to indent when required, Python will not run the program.

Example 1: When a condition is true

if 10 > 3:
    print("This condition is true")
            

Explanation: The message prints only because the line is indented.

This condition is true

Example 2: Missing indentation

if 8 > 2:
print("Check indentation")
            

This raises an error. Python expects the print instruction to be indented under the condition.

Writing Comments in Python

Comments are notes inside your program that Python ignores. They help explain your code to yourself and others.

Example 3: Writing a comment

# This is a comment
print("Hello World")
            
Hello World

Example 4: Comment after code

print("Learning Python")  # This explains the instruction
            
Learning Python

Multi-line Comments

Python does not have a special multi-line comment symbol, but developers use triple quotation marks.

Example 5: Multi-line comment

"""
This is a multi-line comment.
Python ignores everything inside it.
"""
print("Comment example")
            
Comment example

Using the Print Function

The print function displays text or numbers on the screen.

Example 6: Printing text

print("Python syntax is easy to learn")
            
Python syntax is easy to learn

Example 7: Printing numbers

print(25)
print(7 + 3)
            
25
10
            

Putting It All Together

Example 8: Proper structure

# Checking a simple condition
if 15 > 10:
    print("The condition is true")
    print("Python runs these lines")
print("This line is outside the block")
            
The condition is true
Python runs these lines
This line is outside the block
            

Quiz: Test Your Understanding

Select one answer, then press the button to show the correct response.

  1. What does indentation represent in Python?



    Correct answer: Grouping of code blocks.
  2. Which symbol creates a comment in Python?



    Correct answer: #
  3. What is the result of print(6 + 4)?



    Correct answer: 10
  4. How do programmers write multi-line comments?



    Correct answer: """ comment """
  5. Which instruction prints text?



    Correct answer: print("Hello")
  6. Which part does Python ignore?



    Correct answer: Comments
  7. Why is syntax important?



    Correct answer: To define correct program structure.
  8. Which line prints a number?



    Correct answer: print(7)
  9. What happens if indentation is missing?



    Correct answer: Python stops with an error.
  10. Which code prints the result of three plus four?



    Correct answer: print(3 + 4)
Back to Top