1. Python Programming Introduction
What is Python?
Python is a high-level, versatile programming language known for its simplicity and readability. It’s widely used for web development, data analysis, artificial intelligence, automation, and more. Python’s syntax is clean and English-like, making it an excellent choice for beginners.
Why Learn Python?
- Easy to Learn: Python’s syntax is straightforward, so you can focus on programming concepts rather than complex syntax.
- Versatile: Used in web development, data science, machine learning, scripting, and more.
- Large Community: A huge community means plenty of resources, tutorials, and libraries to help you.
- Cross-Platform: Python works on Windows, macOS, Linux, and more.
Key Features of Python
- Readable and Simple: Code looks like plain English, reducing the learning curve.
- Interpreted Language: You don’t need to compile Python code; it runs line by line, making it easy to test and debug.
- Dynamically Typed: You don’t need to declare variable types (e.g., integer, string) upfront.
- Extensive Libraries: Python has a rich set of libraries (like NumPy, Pandas, and Django) for various tasks.
2. Get Started With Python
Step 1: Install Python
To start coding in Python, you need to install it on your computer. Here’s how:
Check if Python is Already Installed
- Open a terminal (Command Prompt on Windows, Terminal on macOS/Linux).
- Type the following command and press Enter:
python --version
or
python3 --version
- If Python is installed, you’ll see a version number (e.g.,
Python 3.11.4
). If not, you’ll get an error, and you need to install Python.
Download and Install Python
- Visit the Official Python Website: Go to python.org.
- Download the Latest Version: As of August 16, 2025, the latest version is likely Python 3.x (e.g., 3.12 or higher). Click the “Download Python” button.
- Run the Installer:
- Windows: Run the
.exe
file. Check the box “Add Python to PATH” during installation. - macOS: Run the
.pkg
file and follow the prompts. - Linux: Most Linux distributions come with Python pre-installed. If not, use your package manager (e.g.,
sudo apt install python3
on Ubuntu).
- Verify Installation: After installation, open a terminal and type
python3 --version
to confirm Python is installed.
Set Up a Code Editor
While you can write Python code in a simple text editor, a code editor or Integrated Development Environment (IDE) makes life easier. Popular options:
- VS Code: Lightweight, free, and highly customizable. Install the Python extension for better support.
- PyCharm: A powerful IDE for Python development (use the free Community edition).
- IDLE: Python’s built-in editor, simple for beginners.
- Jupyter Notebook: Great for data science and interactive coding.
To install VS Code:
- Download it from code.visualstudio.com.
- Install the Python extension by searching for “Python” in the Extensions marketplace.
3. Your First Python Program
Let’s write and run your first Python program: the classic “Hello, World!” program.
Step 1: Write the Code
- Open Your Editor:
- If using VS Code, open it and create a new file named
hello.py
(the.py
extension indicates a Python file). - If using IDLE, open it from your Python installation.
- Type the Following Code:
print("Hello, World!")
print()
is a Python function that outputs text to the console."Hello, World!"
is a string (text) that will be displayed.
- Save the File: Save it as
hello.py
in a folder of your choice (e.g.,C:\PythonProjects
or~/PythonProjects
).
Step 2: Run the Program
There are two main ways to run your Python program:
Method 1: Using the Terminal
- Open a terminal and navigate to the folder containing
hello.py
. For example:
cd C:\PythonProjects
or
cd ~/PythonProjects
- Run the program by typing:
python3 hello.py
- You should see the output:
Hello, World!
Method 2: Using an IDE or Editor
- VS Code: Open
hello.py
, click the “Run” button (triangle icon), or use the terminal within VS Code to runpython3 hello.py
. - IDLE: Open
hello.py
, pressF5
to run, and the output will appear in the IDLE shell. - PyCharm: Open
hello.py
, right-click, and select “Run ‘hello’”.
What Just Happened?
- The
print()
function told Python to display the text"Hello, World!"
in the console. - Python executed the code line by line, and you saw the output immediately.
4. Python Comments
Comments are lines in your code that Python ignores. They’re used to explain what your code does, making it easier to understand for you and others.
Why Use Comments?
- Clarity: Explain complex logic to make your code readable.
- Debugging: Temporarily disable parts of your code.
- Collaboration: Help teammates understand your code.
Types of Comments in Python
Single-Line Comments
- Use the
#
symbol to write a comment on a single line. - Everything after
#
on that line is ignored by Python. - Example:
# This is a single-line comment
print("Hello, World!") # This prints a greeting
- The first line is a comment and does nothing.
- The
#
afterprint()
explains what the line does.
Multi-Line Comments
- Python doesn’t have a dedicated syntax for multi-line comments, but you can use:
- Multiple
#
lines. - Triple quotes (
'''
or"""
) for multi-line strings, which act as comments if not assigned to a variable. - Examples:
# This is line 1 of the comment
# This is line 2 of the comment
print("Hello!")
Or using triple quotes:
"""
This is a multi-line comment.
It can span multiple lines.
Python ignores it unless assigned to a variable.
"""
print("Hello!")
- Both methods achieve the same result: the text is ignored by Python.
Best Practices for Comments
- Be Clear and Concise: Write comments that explain why the code exists, not just what it does.
# Calculate the total price with tax
total = price * 1.1
- Avoid Over-Commenting: Don’t comment obvious code.
# Bad: Too obvious
print("Hello") # Prints Hello
- Use Comments for Planning: Write comments to outline your code before writing it.
# Get user input for name
# Print personalized greeting
name = input("Enter your name: ")
print("Hi, " + name + "!")