Python Introduction
Python is a high-level, general-purpose programming language designed to be easy to read and quick to write. It powers everything from simple scripts to massive web platforms, data pipelines, and machine learning systems. Because its syntax reads almost like plain English, Python is often the very first language people learn — and, at the same time, it stays powerful enough for professional software engineers to rely on every day.
In this lesson you will learn what Python actually is, how a Python program gets executed by your computer, what its basic building blocks look like, and how to avoid the mistakes almost every newcomer makes in their first week.
Overview: What Is Python and How It Works
Python was created by Guido van Rossum and first released in 1991. Its name comes from the British comedy show Monty Python’s Flying Circus, not the snake — a small detail that hints at the language’s famously friendly, approachable culture. Python is maintained today by the Python Software Foundation and a huge open-source community, and it follows a design philosophy captured in a short document called The Zen of Python, which you can read at any time by typing import this into a Python session. Two of its most important lines are “Readability counts” and “There should be one — and preferably only one — obvious way to do it.” That philosophy shapes every part of the language you will learn in this course.
Technically, Python is a high-level, interpreted, dynamically-typed, garbage-collected language. Each of those words matters:
- High-level means you work with concepts close to human thinking — lists, dictionaries, objects — instead of memory addresses or CPU registers.
- Interpreted means your source code is read and executed by another program (the Python interpreter) rather than being compiled directly into a standalone machine-code executable ahead of time, the way C or Rust programs are.
- Dynamically-typed means a variable’s type is determined at runtime based on the value it currently holds, and it can change: the same name can refer to an integer at one moment and a string later.
- Garbage-collected means you never manually free memory — Python’s runtime tracks how many places refer to each object and reclaims memory automatically once nothing uses it anymore.
Under the hood, everything you create in Python — a number, a string, a function, even a class — is an object living in memory, and a variable is simply a name bound to that object (a reference), not a labeled box that stores the value directly. This is why, for example, assigning one list to a second variable name makes both names point at the same underlying list rather than creating a copy. Understanding “variables are references to objects” early will save you from confusion in almost every later lesson.
Python is used across an enormous range of domains, which is part of why it remains one of the most popular languages in the world:
| Domain | Typical Use |
|---|---|
| Web development | Backend servers and APIs with frameworks like Django and Flask |
| Data science & analytics | Cleaning, analyzing, and visualizing data with pandas, NumPy, and Matplotlib |
| Machine learning & AI | Building and training models with libraries like PyTorch and scikit-learn |
| Automation & scripting | Automating repetitive tasks, file processing, and system administration |
| Education | Teaching programming fundamentals due to its readable syntax |
Syntax: Your First Look at Python Code
Python programs are plain text files, typically saved with a .py extension, that you run with a command such as python3 my_script.py. Unlike many other languages, Python does not use curly braces { } to mark blocks of code — it uses indentation (whitespace at the start of a line) instead. This is not just a style choice; it is part of the language’s grammar, and inconsistent indentation will cause your program to fail.
| Element | Description | Example |
|---|---|---|
| Statement | A single instruction Python executes | print("Hi") |
| Comment | Text ignored by the interpreter, starts with # |
# this explains the code |
| Indentation | Spaces that define a block (4 spaces is the convention) | the body of a function or if statement |
| Variable | A name bound to a value/object | age = 28 |
| Function call | Running a reusable block of code by name | len("Python") |
The general shape of a simple Python program looks like this:
# comments start with a hash symbol
variable_name = value # create a variable
def function_name(parameter): # define a function
return parameter * 2 # indented body
result = function_name(variable_name)
print(result) # display output
Examples
Example 1: Your first program
Every language tutorial starts somewhere, and in Python that starting point is refreshingly short. The built-in print() function writes text to the screen (technically, to “standard output”).
print("Hello, World!")
print("Welcome to Python programming.")
Output:
Hello, World!
Welcome to Python programming.
Each call to print() outputs its argument followed by a newline, which is why the two messages appear on separate lines. Notice there is no semicolon at the end of the line and no curly braces anywhere — Python statements simply end at the end of the line.
Example 2: Variables, types, and f-strings
Python variables do not need a declared type; the type is inferred from the value that gets assigned. You can inspect any object’s type with the built-in type() function, and you can embed variables directly inside strings using an f-string (a string literal prefixed with f).
name = "Ada"
age = 28
height_m = 1.65
print(f"{name} is {age} years old and {height_m} m tall.")
print(type(name), type(age), type(height_m))
Output:
Ada is 28 years old and 1.65 m tall.
Here name is bound to a str object, age to an int, and height_m to a float. The f-string automatically converts each value to its readable string form and inserts it in place of the { } placeholder — no manual string concatenation required.
Example 3: A small, realistic program
Real Python code usually combines several building blocks — functions, loops, and collections — to do something useful. This example calculates and filters a list of test scores.
def average(numbers: list[float]) -> float:
return sum(numbers) / len(numbers)
scores = [88, 92, 79, 95, 84]
passing = [s for s in scores if s >= 80]
print(f"All scores: {scores}")
print(f"Passing scores (>= 80): {passing}")
print(f"Average score: {average(scores):.2f}")
Output:
All scores: [88, 92, 79, 95, 84]
Passing scores (>= 80): [88, 92, 95, 84]
Average score: 87.60
The function average() uses two built-ins, sum() and len(), and a type hint (list[float] -> float) that documents its intent without changing runtime behavior — Python does not enforce type hints by default. The line [s for s in scores if s >= 80] is a list comprehension, a compact way to build a new list by filtering an existing one; it excludes 79 because 79 is less than 80. The :.2f inside the f-string formats the average to exactly two decimal places.
How Python Executes Your Code (Under the Hood)
When you run python3 my_script.py, several steps happen before your program’s output appears:
- 1. Parsing. The interpreter reads your source text and checks it follows Python’s grammar, producing an internal tree-like structure representing the program.
- 2. Compilation to bytecode. That structure is compiled into bytecode — a compact, platform-independent set of instructions (not the machine code your CPU runs directly). This is why Python is sometimes called “interpreted” even though a real compilation step happens; the output is bytecode, not native machine instructions.
- 3. Execution by the Python Virtual Machine (PVM). The reference implementation, CPython, runs this bytecode one instruction at a time inside a loop written in C, carrying out operations like arithmetic, function calls, and attribute lookups.
- 4. Caching. For imported modules, Python saves the compiled bytecode to disk in a
__pycache__folder (as.pycfiles) so it doesn’t have to re-parse and recompile unchanged files on the next run.
Because this all happens automatically and quickly, Python feels interactive — you write code and see results almost immediately, which is part of why it is popular for experimentation, data analysis, and learning. The tradeoff is that a native-compiled language like C or Rust is typically faster for raw computation, since it skips the bytecode-interpretation step entirely.
Common Mistakes
Mistake 1: Mixing tabs and spaces for indentation. Because indentation is meaningful in Python, mixing tab characters and space characters in the same block can produce a TabError or subtly misaligned code that runs differently than it looks. Configure your editor to insert spaces (4 per indent level) whenever you press Tab, and never hand-mix the two.
Mistake 2: Writing print as a statement instead of a function call. Newcomers who learned from older Python 2 material sometimes write code like this, which is no longer valid in Python 3:
print "Hello, World!"
In Python 3, print is a regular function, so its argument must be inside parentheses. The corrected version is:
print("Hello, World!")
Without the parentheses, Python 3 raises a SyntaxError because it interprets print followed by a string as two separate expressions rather than a function call with an argument.
Best Practices
- Follow PEP 8, Python’s official style guide: use 4 spaces per indentation level,
snake_casefor variable and function names, and keep lines under about 79-99 characters. - Give variables descriptive names (
passing_scoresrather thanps) — Python’s readability advantage only pays off if you write readable code. - Use a virtual environment (
python3 -m venv) for every project so each project’s dependencies stay isolated from your system Python. - Add short comments to explain why code does something non-obvious, not what it does — well-named code should already say what.
- Use the interactive REPL (just type
python3in a terminal) to quickly test small snippets before putting them in a file. - Prefer the standard library and well-known, actively maintained third-party packages over writing complex functionality from scratch.
Practice Exercises
- Exercise 1: Write a program that stores your name, your age, and your favorite programming topic in three variables, then prints a single sentence containing all three using an f-string.
- Exercise 2: Write a program that creates a list of five integers of your choice, prints the list, then prints only the even numbers from it using a list comprehension.
- Exercise 3: Without running it, predict what this snippet prints, then check your reasoning:
x = 10,y = x,x = 20,print(y). (Hint: think about whetherywas bound to the object10or somehow “linked” to the variablex.)
Summary
- Python is a high-level, interpreted, dynamically-typed language designed for readability, created by Guido van Rossum and released in 1991.
- Variables in Python are names bound to objects in memory, not fixed-type storage boxes — a name’s type comes from whatever value it currently references.
- Python code uses indentation, not braces, to define blocks, which makes consistent whitespace a functional requirement, not just a style preference.
- Running a script involves parsing your source, compiling it to bytecode, and executing that bytecode inside the CPython virtual machine.
- Common beginner errors include mixed tabs/spaces and using outdated Python 2 syntax like a bare
printstatement. - Following PEP 8 and using virtual environments from the start will save you significant pain as your programs grow.
