Python is a powerful, easy-to-learn, and widely used programming language. This tutorial will guide you through installing Python, writing your first program, and understanding its basic structure.
Installing Python
Step 1: Download Python
- Go to the official Python website: https://www.python.org/downloads/
- Choose the latest stable version for your operating system (Windows, macOS, or Linux)
- Click “Download” and run the installer
Step 2: Install Python
- Check the box “Add Python to PATH” before installing (important for running Python from the command line)
- Follow the installation steps and complete the setup
Step 3: Verify Installation
To check if Python is installed correctly, open a terminal or command prompt and type:
python --version
If installed correctly, it will display the Python version.
Running Python Code
Python programs can be executed in two modes:
Interactive Mode (Python Shell)
The interactive mode allows you to execute Python commands one at a time. Open a terminal and type:
python
You’ll see the Python prompt (>>>
). You can type Python commands directly:
>>> print("Hello, World!")
Hello, World!
To exit, type exit()
or press Ctrl + Z
(Windows) / Ctrl + D
(Mac/Linux).
Script Mode
In script mode, you write a Python program in a file and execute it. Follow these steps:
- Open a text editor (e.g., Notepad, VS Code, PyCharm)
- Write your Python code and save it with a
.py
extension, e.g.,hello.py
- Run the script in the terminal using:
python hello.py
Writing Your First Python Program
Python Code Example:
# This is a simple Python program
print("Welcome to Python Programming!")
Explanation:
#
starts a comment (ignored by Python)print()
is a built-in function to display output
When you run this script, it prints:
Welcome to Python Programming!
Basic Syntax & Structure
Python Syntax Rules:
✔ Python uses indentation instead of braces {}
✔ Statements are written on separate lines ✔ Variables do not need explicit declaration
Example of Indentation in Python
if 5 > 2:
print("Five is greater than two!")
Python uses indentation to define blocks of code. Incorrect indentation results in an error.
Python Comments
Comments are ignored by Python but useful for explaining code.
Single-line Comment:
# This is a comment
print("Hello, Python!")
Multi-line Comment:
"""
This is a multi-line comment.
Python ignores it when running the code.
"""
print("Python is fun!")
Summary
✅ Installed Python and verified installation
✅ Learned how to run Python in interactive and script mode
✅ Wrote the first Python program
✅ Understood Python’s basic syntax, indentation, and comments