By the end of this lesson, you will be able to install Python on your computer and execute a simple script from the command line.
What it is
Python is an interpreted, high-level programming language. Unlike compiled languages like C++ or Java, you do not need to compile Python code into machine code before running it. Instead, the Python interpreter reads your source code line by line and executes it immediately. This makes Python ideal for rapid development, scripting, and learning programming concepts.
The core components are:
- Interpreter: The program that runs your Python code (e.g.,
python.exe). - Script: A text file containing Python commands, typically ending with the
.pyextension. - REPL: Read-Eval-Print Loop, an interactive mode where you type commands one at a time.
Why it matters
- Immediate Feedback: You can test small pieces of logic instantly without complex build steps.
- Cross-Platform: The same script usually runs on Windows, macOS, and Linux.
- Industry Standard: Used heavily in data science, web development, automation, and AI.
- Simplicity: The syntax resembles plain English, reducing the barrier to entry for new programmers.
Syntax or steps
To run a Python program, follow these general steps:
- Install Python: Download the installer from python.org. On Windows, check "Add Python to PATH" during installation.
- Create a File: Open a text editor (like VS Code, Notepad++, or Sublime Text) and save a file with a
.pyextension. - Write Code: Enter valid Python statements.
- Run via Terminal: Open your command prompt or terminal, navigate to the file's directory, and execute the interpreter.
Example
Here is a complete workflow for creating and running a basic script.
# Step 1: Create a file named hello.py
# Content of hello.py:
print("Hello, World!")
name = input("Enter your name: ")
print(f"Nice to meet you, {name}")
To run this file, open your terminal or command prompt and type:
python hello.py
Explanation:
print(): Outputs text to the console.input(): Pauses execution and waits for the user to type something, storing it in the variablename.f-string: Thefprefix allows embedding variables directly inside strings using curly braces{}.python hello.py: Tells the operating system to launch the Python interpreter and passhello.pyas the script to execute.
Common mistakes
- "Command not found": Python is not installed or not added to the system PATH. Fix: Reinstall Python and ensure "Add to PATH" is checked, or use the full path to the executable.
- Indentation Errors: Python uses whitespace to define blocks of code. Mixing tabs and spaces causes errors. Fix: Configure your editor to insert spaces instead of tabs.
- File Extension: Saving the file as
hello.txtinstead ofhello.py. Fix: Ensure the file ends with.py. - Version Confusion: Using
pythonwhen onlypython3is installed (common on macOS/Linux). Fix: Try runningpython3 hello.py.
When to use it
Compare running scripts via the command line versus using an Integrated Development Environment (IDE).
| Method | Best For | Pros | Cons |
|---|---|---|---|
Command Line (python script.py) |
Automation, servers, quick tests | Lightweight, universal, fast startup | No visual debugging tools built-in |
| IDE (VS Code, PyCharm) | Large projects, learning | Auto-completion, error highlighting, debuggers | Heavier resource usage, setup required |
Practice
Guided Exercise: Write a script called math_test.py that asks the user for two numbers and prints their sum.
Hint: Use float(input()) to convert the string input into a number.
Challenge: Modify the script to also print the product of the two numbers.
Expected Output:
Enter first number: 5
Enter second number: 4
Sum: 9.0
Product: 20.0
Quick check
Question: Why must you include the .py extension when saving your Python file?
Answer: While the interpreter can technically read any text file, the .py extension signals to your operating system and editors that the file contains Python code, enabling proper syntax highlighting and default execution associations.
Summary
Getting started with Python involves installing the interpreter, writing code in a text file with a .py extension, and executing it via the command line. Mastering this basic workflow provides the foundation for all future Python development, whether you move toward web apps, data analysis, or automation.