Syntax, print & Comments
The basic rules: printing output, comments, and Python’s famous indentation.
What you will learn
- Print output with print()
- Write comments
- Understand indentation
print() shows output
The print() function is how your program talks back to you — it shows text and numbers on the screen. Whatever you put inside the brackets gets displayed.
print("Hello")
print("Learning", "Python") # print can take several values
print(5 + 5)Line 1 prints the word Hello. Line 2 passes two values separated by a comma — Python prints them on one line with a space between, giving Learning Python. Line 3 has no quotes, so Python first works out the sum 5 + 5 and then prints the result, 10. Text in quotes is shown exactly; a sum without quotes is calculated first.
Note: Output: Hello Learning Python 10
Comments
A comment starts with #. Python ignores it — use it to explain your code.
# This is a comment
print("Visible") # comments can go at the end of a lineThe first line is only a comment, so Python skips it entirely — nothing prints from it. The second line runs the print() and shows Visible; the # comments can go… part after it is ignored. Comments are notes for humans reading the code, never instructions for the computer.
Note: Output: Visible
Indentation matters!
Unlike most languages, Python uses indentation (spaces at the start of a line) to group code — not curly braces. Code inside an if or loop must be indented (usually 4 spaces).
if 5 > 2:
print("Five is bigger") # indented = inside the if
print("Always runs") # not indented = outsideThe first line asks a question: is 5 greater than 2? Because it is true, the indented line below it runs and prints Five is bigger. The last line is not indented, so it sits outside the if and always runs — printing Always runs no matter what. The spaces at the start of a line are how Python knows which lines belong to the if.
Note: Output: Five is bigger Always runs
Watch out: Wrong indentation causes an IndentationError. Be consistent — use 4 spaces for each level. Do not mix tabs and spaces.
Q. How does Python know which code is inside an if statement?
✍️ Practice
- Print three lines about yourself.
- Write an
ifwith a correctly indented line inside it.
🏠 Homework
- Add comments explaining each line of a small script.