Introduction
Once Python is installed on your computer, the best way to start learning is by writing your first program. Almost every programming journey begins with the classic line:
Hello, World!
This simple program helps you understand how Python works, how to run code, and how Python syntax feels. It may look like one line, but it teaches important basics.
Writing First Python Program
Let’s start with the easiest possible example:
Example: Printing “Hello, World!” in Python
print("Hello, World!")
What does this mean?
- print() → A built-in Python function used to display output on the screen.
- “Hello, World!” → A string (text value) that we give to the print function.
- Python reads this line and prints the message exactly as it is.
When you run the code, your screen will show:
Hello, World!
This confirms that Python is installed correctly and your first program is working.
Understanding Python Syntax (Beginner-Friendly)
Python syntax refers to the set of rules that define how Python programs are written. Python is known for being clean and easy to understand, but it still has certain rules you need to follow.
Let’s look at them one by one.
Indentation (Very Important in Python)
Python uses indentation to define blocks of code.
This means the number of spaces at the beginning of a line matters.
✔ Example:
if 5 > 2:
print("Five is greater than two!")
- The space before the print statement is called indentation.
- Without indentation, Python will show an error.
✔ Why indentation matters?
In other languages, curly braces {} define code blocks.
But in Python, indentation is the structure.
So, writing:
if 5 > 2:
print("Hello")
will cause an error because the print statement is not indented.
✔ Recommended Indentation
Python developers generally use 4 spaces for indentation.
Avoid mixing tabs and spaces, as it may create errors.
Whitespace in Python
Python is very sensitive to whitespace.
Whitespace refers to:
- Spaces
- Tabs
- Newlines
If the spacing is inconsistent, Python may not understand the code.
✔ Good whitespace example:
name = "Harry"
age = 22
✔ Bad whitespace example:
name = "Harry"
age=22
While this may still work, it makes the code messy and hard to read.
Python Statements
A statement is a single line of code that Python can execute.
✔ Example:
x = 10
print(x)
Each line is a separate statement.
✔ Multiple statements on one line
You can write them using semicolons, but it is not recommended.
x = 10; y = 20; print(x + y)
Although this works, it reduces readability.
Python developers prefer writing one statement per line.
Comments in Python
Comments are notes that help explain code.
Python ignores comments—they are only for humans to read.
✔ Single-line comment
# This is a single-line comment
✔ Multi-line comment
You can use triple quotes:
'''
This is a multi-line comment.
You can write notes here.
'''
Or:
"""
Another way to write
multi-line comments.
"""
Comments are especially useful when explaining what your code does.