Introduction to Python
Your first steps into the world's most popular programming language
Python is a high-level, interpreted programming language known for its clear syntax and readability. Created by Guido van Rossum and first released in 1991, Python has become the go-to language for data science, AI, web development, and automation.
In this chapter, you'll write your first Python program, understand how Python executes code, and learn about the interactive environment you'll use throughout the course.
What is Python?
Python is a general-purpose programming language. This means it can be used for virtually anything — from building websites to analysing data to controlling robots.
Key characteristics of Python:
- Interpreted — code runs line by line, making debugging easier
- Dynamically typed — you don't need to declare variable types
- Indentation-based — code blocks are defined by spaces, not braces
- Batteries included — rich standard library for common tasks
Installing Python
Before writing Python code on your own machine, you need to install the Python interpreter. Here's how:
- Go to python.org/downloads and click the big Download Python 3.x button — it auto-detects your OS.
- Windows: Run the installer and tick "Add Python to PATH" before clicking Install. This is important — without it, your terminal won't find Python.
- macOS: Run the
.pkginstaller. Alternatively, if you use Homebrew:brew install python. - Linux: Python 3 is usually pre-installed. Check with
python3 --version. If not:sudo apt install python3(Debian/Ubuntu).
After installation, open a terminal (Command Prompt on Windows, Terminal on Mac/Linux) and verify:
import sys # Run this in your terminal to check your installation:# python --version (Windows)# python3 --version (Mac/Linux) # Or run this script to see full details:print(f"Python version : {sys.version}")print(f"Python location: {sys.executable}")
Setting Up VS Code
Visual Studio Code (VS Code) is the most popular free editor for Python. It gives you syntax highlighting, auto-complete, debugging, and an integrated terminal.
Step 1 — Install VS Code: Download from code.visualstudio.com and run the installer.
Step 2 — Install the Python extension:
- Open VS Code and press Ctrl+Shift+X (Windows/Linux) or Cmd+Shift+X (Mac) to open Extensions.
- Search for Python (by Microsoft) and click Install.
- Reload VS Code when prompted.
Step 3 — Select your interpreter: Press Ctrl+Shift+P, type Python: Select Interpreter, and choose the Python 3.x version you installed.
Step 4 — Create and run your first file:
- Create a new file called
hello.py. - Type your code (see below) and save with Ctrl+S.
- Right-click in the editor and choose Run Python File in Terminal, or press F5.
# hello.py — your first Python script# Save this file and run it in VS Code print("Hello from VS Code!")print("Your Python environment is ready.")
Your First Python Program
The traditional first program in any language is one that prints "Hello, World!" to the screen. In Python, this is just one line:
print("Hello, World!")
Python as a Calculator
Python can be used as a powerful calculator. Try these arithmetic operations:
# Basic arithmeticprint(2 + 3) # Addition: 5print(10 - 4) # Subtraction: 6print(3 * 7) # Multiplication: 21print(15 / 4) # Division: 3.75print(15 // 4) # Floor division: 3print(2 ** 10) # Power: 1024print(17 % 5) # Modulo: 2
Practice Questions
Question 1
Which of the following correctly prints 'Hello, Python!' in Python?
Question 2
What does 2 ** 10 evaluate to in Python?