Computer Science Researcher • Educator • Programming Instructor
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.
Python relies on indentation to group related instructions. If you forget to indent when required, Python will not run the program.
if 10 > 3:
print("This condition is true")
Explanation: The message prints only because the line is indented.
This condition is true
if 8 > 2:
print("Check indentation")
This raises an error. Python expects the print instruction to be indented under the condition.
Comments are notes inside your program that Python ignores. They help explain your code to yourself and others.
# This is a comment
print("Hello World")
Hello World
print("Learning Python") # This explains the instruction
Learning Python
Python does not have a special multi-line comment symbol, but developers use triple quotation marks.
"""
This is a multi-line comment.
Python ignores everything inside it.
"""
print("Comment example")
Comment example
The print function displays text or numbers on the screen.
print("Python syntax is easy to learn")
Python syntax is easy to learn
print(25)
print(7 + 3)
25
10
# 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
Select one answer, then press the button to show the correct response.