Python First Program and Python Syntax
So far, we have learned how to install Python.
Here, we will learn how to write a Python program and how to run it.
Writing and Running Python Program
-
Write Program:
- Write the following code in Notepad or VS Code:
print("Welcome to Python World")
-
Save File:
- Save the file with the name
welcome.py.
- Save the file with the name
-
Run Program:
- Type the following command in Command Prompt:
python welcome.py
Output
Welcome to Python World
Welcome to Python World
Indentation in Python
Indentation is a very important concept in Python.
Indentation means spaces or tabs at the beginning of a line.
In other languages, braces { } are used to group statements, but in Python, indentation is used.
If indentation is incorrect, Python raises an IndentationError or TabError.
Rules of Indentation
- If indentation is incorrect, the Python program will not run.
- Statements inside a block (if, for, while, def) must be indented.
- All lines inside the same block must have the same number of spaces.
- Mixing tabs and spaces causes a TabError.
- At least one space is required for each indentation level; using 4 spaces is best practice.
- For nested blocks, an additional 4 spaces should be used.
# Example
x = 10
print(x)
y = 20
print(y)
- Running this code produces
IndentationError: unexpected indent. - This is because
y = 20andprint(y)have extra indentation. - These statements do not belong to any block, so indentation is not required.
- Indentation in Python is mandatory only to define block structure.
- Statements outside blocks should not be indented.
# Example
x = 10
print(x)
if (x == 10):
y = 20
print(y)
- This program runs successfully.
- Indentation is mandatory for statements inside the if block.
- Blocks such as if, for, while, and def must use indentation.
- Statements outside blocks do not require indentation.
Statements in Python
- A statement is an instruction given to the computer.
- A program is a collection of statements.
- In Python, semicolon (;) at the end of a statement is optional.
- Each statement normally ends on a new line.
- In languages like C, C++, and Java, semicolon is mandatory.
- In Python, semicolon is not required.
- To write multiple statements on the same line, semicolon can be used.
print(10); print(20) - If a statement is too long, it can be continued on the next line using backslash (\).
total = 1 + 2 + 3 + \
4 + 5 + 6
Example for Multiline Statement in Python
x = 10
y = 20
print(x); print(y)
total = 1 + 2 + 3 + 4 + \
5 + 6 + 7 + 8
print(total)
Output
10
20
36
10
20
36
In the above program:
- Semicolon (;):
- Semicolon is not mandatory in Python.
- It can be used to separate multiple statements in one line.
- Example:
print(x); print(y) - Here, two statements are written in the same line using semicolon.
- Multiline (\):
- Backslash (
\) allows long expressions to continue on the next line. - Example:
total = 1 + 2 + 3 + 4 + \ 5 + 6 + 7 + 8 - Python treats it as a single expression.
- Backslash (
Comments in Python
Comments are descriptive lines used to explain code.
They are not executed by Python.
Python supports both single-line and multi-line comments.
Single-Line Comment in Python
Single-line comments are written using the # symbol.
They can be written at the beginning of the line or after a statement.
# This line prints output
print("Welcome to Python World") # single-line comment after statement
Multi-Line Comment in Python
Multi-line comments can be written using three single quotes (''') or three double quotes (""").
'''
This is a multi-line comment
This is the second line
And so on...
'''
print("Welcome to Python World")
Example for Comments in Python
'''
This program creates two variables x and y
The addition result is assigned to sum
Finally, sum is printed
'''
x = 10
y = 20
# add x and y and assign to sum
sum = x + y
print(sum) # printing sum value
Output
30
30
In this program:
multi-line comments are used to describe the program,
and single-line comments are written at the beginning of the line
and after statements.
What We Have Learned
- Writing and Running Python Program
- Basic Syntax in Python
- Variables in Python will be covered in the next topic