1. What is Python?
Python is a high-level, interpreted, and general-purpose programming language.
- High-level: You write code closer to human language, and the computer handles the complex, low-level details.
- Interpreted: Python code is executed line-by-line by an interpreter program, which makes development fast and debugging easier.
- General-purpose: It can be used for almost anything, from web development and data science to scripting and automation.
2. Installing the Python Interpreter
To run Python code, you need the official Python Interpreter installed on your system.
- Download: Go to the official website:
python.org/downloads. - Installer: Download the latest stable version for your operating system (Windows, macOS, or Linux).
- Crucial Step (Windows): During installation, make sure you check the box that says:
“Add python.exe to PATH” This allows you to run Python commands directly from any location in your system’s terminal. - Verification: Open your command prompt or terminal and type:
python --version
You should see the installed version number (e.g., Python 3.12.0).
3. Choosing a Code Editor
A good Integrated Development Environment (IDE) or code editor is essential for writing Python efficiently.
- Recommendation: Visual Studio Code (VS Code)
- It’s free, fast, and highly customizable.
- Install the Python Extension Pack to enable features like syntax highlighting, code completion (IntelliSense), and debugging.
- Alternative: PyCharm Community Edition (specifically designed for Python, but heavier).
4. Running Python Code
There are two primary ways to execute your Python code:
A. The Python Shell (REPL)
The Read-Eval-Print Loop (REPL), or Python Shell, is an interactive environment perfect for testing small snippets of code immediately.
- Open your terminal/command prompt.
- Type
pythonand press Enter. - The prompt changes to
>>>. You can now type Python code directly:
>>> print("Hello, Python!")
Hello, Python!
>>> 2 + 3
5
Type exit() or press Ctrl+Z (Windows) or Ctrl+D (macOS/Linux) to exit.
B. Executing a Script
For actual programs, you write the code in a file ending with the .py extension (a Python Script) and run it using the interpreter.
- Create a file named
hello.pyand save it. - Write your code in the file:
# hello.py
name = "Alice"
print(f"Hello, {name}!")
- Open your terminal, navigate to the directory where you saved the file, and run:Bash
python hello.pyThe output will be:
Hello, Alice!
