Python Programming

Setting Up Python and Writing Your First Program

25 分

Python is readable, has a library for almost everything and runs web apps (PandaDev itself is Flask), automation, data science and AI. This track takes you from zero to writing tested, packaged programs.

Install

  • Windows: install from python.org and tick "Add python.exe to PATH". The python command in the Microsoft Store is a stub; prefer the python.org installer or winget install Python.Python.3.12.
  • macOS: brew install python@3.12.
  • Linux: usually present; sudo apt install python3 python3-venv.

Check it:

python --version      # or python3 --version

Virtual environments: one per project

Each project gets its own isolated set of packages, so projects never break each other.

python -m venv .venv
source .venv/bin/activate        # macOS/Linux
.venv\Scripts\activate           # Windows
pip install requests
pip freeze > requirements.txt    # record exact versions

Faster alternative: uv (uv venv, uv pip install -r requirements.txt). Never commit .venv to Git.

Your first program

hello.py:

name = input("What is your name? ")
print(f"Hello, {name}! Welcome to Python.")
python hello.py

The interactive shell (REPL)

Type python with no file to experiment line by line:

>>> 2 ** 10
1024
>>> "panda".upper()
'PANDA'
>>> help(str.split)

Exit with exit(). The REPL is the fastest way to check how something behaves.

Comments and style

# A comment explains WHY, not what.
TAX_RATE = 0.2  # constants in UPPER_CASE

def total_with_tax(amount):
    """Return amount plus tax."""  # a docstring
    return amount * (1 + TAX_RATE)

Python uses indentation (4 spaces) to mark blocks. Follow PEP 8 style; tools like ruff format apply it automatically.

An editor

VS Code with the Python extension gives you autocomplete, linting, a debugger and runs your venv's interpreter. Select it with "Python: Select Interpreter".

Exercise

Write age.py that asks for a birth year and prints how old the person turns this year. Hint: from datetime import date and date.today().year, and convert input with int().

Check yourself

  • Why use a virtual environment per project?
  • What does pip freeze > requirements.txt do?
  • How does Python mark the body of a function?

進捗はこのブラウザーにのみ保存されます。

レビューに提出(10 ポイント)