Python: Python Setup & First Program
Before diving into Python syntax, let's get your development environment ready. All you need is Python installed and a text editor.
1. Installing Python
(1) Windows
- Go to https://python.org and click Downloads
- Download the latest Python version (e.g., 3.13.x)
- Run the installer — make sure to check "Add Python to PATH"
- Open Command Prompt (cmd) and verify with
python --version
TEXT
📖 Display only
C:\Users\YourName> python --version
Python 3.13.2
(2) Mac
Mac comes with Python 2 (too old), so you need to install Python 3:
- Go to https://python.org and download the Mac installer
- Run the installer, or use Homebrew:
brew install python3 - Verify in the terminal:
python3 --version
(3) Linux
Most Linux distributions already have Python 3 installed:
BASH
# Ubuntu/Debian
sudo apt install python3
# Verify
python3 --version
2. Hello World Program
Once installed, your first program is always "Hello, World!".
(1) Method 1: Interactive Interpreter
Type python (Windows) or python3 (Mac/Linux) in your terminal to enter interactive mode:
TEXT
📖 Display only
Python 3.13.2 (main, Feb 4 2025, 14:51:09)
Type "help", "copyright", "credits" or "license" for more information.
>>> print("Hello, World!")
Hello, World!
>>> 1 + 2
3
>>> exit()
💡 The interactive interpreter is great for quickly testing code snippets, but not ideal for writing complete programs.
(2) Method 2: Create a .py File
- Create a new file called
hello.py - Enter this code:
PYTHON
print("Hello, World!")
- Run it from the terminal:
BASH
python hello.py
Output:
TEXT
📖 Display only
Hello, World!
3. Choosing an Editor
You don't need a complex IDE when you're just starting out. Here are some recommendations:
| Editor | Best For | Pros |
|---|---|---|
| VS Code | Beginners (recommended) | Free, lots of extensions, lightweight |
| PyCharm | Professional development | Most features, great autocomplete |
| IDLE | Absolute beginners | Comes with Python, ready out of the box |
| Jupyter Notebook | Data analysis | Run code in cells, great for interactive learning |
💡 We recommend VS Code with the Python extension — lightweight and powerful.
(1) VS Code Setup
- Download and install VS Code (code.visualstudio.com)
- Open VS Code, press
Ctrl+Shift+Xto open the Extensions panel - Search for "Python" and install Microsoft's official Python extension
- Create a new file and save it with a
.pyextension - Right-click in the editor → "Run Python File in Terminal"
▶ Example: Interactive Greeting Program (Difficulty ⭐)
Let's put it all together and write a small interactive program:
PYTHON
# First Python program
name = input("What's your name? ")
print("Hello, " + name + "! Welcome to Python!")
When you run it:
TEXT
📖 Display only
What's your name? Alice
Hello, Alice! Welcome to Python!
▶ Example: Your First Calculation Program (Difficulty ⭐)
PYTHON
# Simple calculations in Python
name = "Python Learner"
birth_year = 2000
current_year = 2026
age = current_year - birth_year
print(f"Hello, {name}!")
print(f"You are approximately {age} years old.")
print(f"Fun fact: you've lived about {age * 365} days!")
Output:
TEXT
📖 Display only
Hello, Python Learner!
You are approximately 26 years old.
Fun fact: you've lived about 9490 days!
▶ Example: Self-Introduction with Variables (Difficulty ⭐)
PYTHON
# Build a self-introduction using variables and f-strings
name = "Alice"
age = 25
hobby = "programming"
experience = 2
print("=" * 30)
print(" Self Introduction")
print("=" * 30)
print(f"Name: {name}")
print(f"Age: {age}")
print(f"Hobby: {hobby}")
print(f"Experience: {experience} year(s)")
print(f"Fun: I've written approximately {experience * 50} programs!")
print("=" * 30)
Output:
TEXT
📖 Display only
==============================
Self Introduction
==============================
Name: Alice
Age: 25
Hobby: programming
Experience: 2 year(s)
Fun: I've written approximately 100 programs!
==============================
4. Common Pitfalls
- PATH not set (Windows): You MUST check "Add Python to PATH" during installation, otherwise the terminal won't find the
pythoncommand - python vs python3: Windows uses
python, Mac/Linux usepython3 - Chinese encoding issues: Python 3 uses UTF-8 by default, so garbled text is rare. If you do run into encoding issues, add
# -*- coding: utf-8 -*-at the top of your file
❓ FAQ
Q What's the difference between an IDE, a text editor, and Python itself?
A Python is the language runtime — it executes your code. A text editor (Notepad, VS Code) is where you write code. An IDE (PyCharm, VS Code with extensions) bundles an editor with debugging, autocomplete, and other tools. For beginners, VS Code strikes the best balance — lightweight like an editor, extensible like an IDE.
Q I typed
pythonbut got 'python is not recognized'. What went wrong?A This is the #1 beginner issue — Python isn't on your system PATH. When you type a command in the terminal, the OS looks for it in a list of directories called PATH. Fix: reinstall Python and make sure to check "Add Python to PATH." Or manually add
C:\Python3xx\and C:\Python3xx\Scripts\to your system environment variables.Q When should I use the interactive interpreter vs a
.py file?A Use the interactive interpreter (
>>> prompt) for quick experiments — testing a single line, checking a function's behavior, or exploring a new module. Use .py files for anything you want to save, reuse, or share. A good rule: if it's more than 3 lines, put it in a file.Q My Python file runs but the window closes instantly. How do I see the output?
A This happens when you double-click a
.py file on Windows — the terminal opens, runs the code, and closes. Solutions: ① Open the terminal first, then run python myfile.py; ② Add input("Press Enter to exit...") at the end of your script; ③ Run from VS Code's built-in terminal instead of double-clicking.📖 Summary
- Download and install Python 3.x from python.org
- On Windows, check "Add Python to PATH" during installation
- Interactive interpreter: type
pythonin the terminal .pyfiles: write code in an editor, run withpython filename.py- VS Code + Python extension is the recommended development setup
📝 Exercises
- Install Python and type
python --versionin the terminal — take a screenshot - Write a
hello.pyfile that prints"Hello, World!"and run it successfully - In the interactive interpreter, calculate
(5 + 3) * 2and10 / 3— observe the results - Modify the interactive program above by adding an
agevariable, and print "Your name is XX, you are XX years old"