Python Setup and Your First Program

Setup and Your First Program

Before diving into Python syntax, let's get your development environment ready. All you need is Python installed and a text editor.

Installing Python

Windows

  1. Go to https://python.org and click Downloads
  2. Download the latest Python version (e.g., 3.13.x)
  3. Run the installer — make sure to check "Add Python to PATH"
  4. Open Command Prompt (cmd) and verify with python --version
C:\Users\YourName> python --version
Python 3.13.2

Mac

Mac comes with Python 2 (too old), so you need to install Python 3:

  1. Go to https://python.org and download the Mac installer
  2. Run the installer, or use Homebrew: brew install python3
  3. Verify in the terminal: python3 --version

Linux

Most Linux distributions already have Python 3 installed:

BASH
# Ubuntu/Debian
sudo apt install python3

# Verify
python3 --version

Hello World Program

Once installed, your first program is always "Hello, World!".

Method 1: Interactive Interpreter

Type python (Windows) or python3 (Mac/Linux) in your terminal to enter interactive mode:

PYTHON
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.

Method 2: Create a .py File

  1. Create a new file called hello.py
  2. Enter this code:
PYTHON
print("Hello, World!")
  1. Run it from the terminal:
BASH
python hello.py

Output:

Hello, World!

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.

VS Code Setup

  1. Download and install VS Code (code.visualstudio.com)
  2. Open VS Code, press Ctrl+Shift+X to open the Extensions panel
  3. Search for "Python" and install Microsoft's official Python extension
  4. Create a new file and save it with a .py extension
  5. Right-click in the editor → "Run Python File in Terminal"

Your First Complete Program

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:

What's your name? Alice
Hello, Alice! Welcome to Python!

Common Pitfalls

  1. PATH not set (Windows): You MUST check "Add Python to PATH" during installation, otherwise the terminal won't find the python command
  2. python vs python3: Windows uses python, Mac/Linux use python3
  3. 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

📖 Section Summary

📝 Homework

  1. Install Python and type python --version in the terminal — take a screenshot
  2. Write a hello.py file that prints "Hello, World!" and run it successfully
  3. In the interactive interpreter, calculate (5 + 3) * 2 and 10 / 3 — observe the results
  4. Modify the interactive program above by adding an age variable, and print "Your name is XX, you are XX years old"
100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏