0% complete

Python foundations · Lesson 1 of 3

What Python is and how a program runs

Understand Python source code, the interpreter, execution, and common use cases.

Lesson

1. Concept and mental model

Python is a general-purpose programming language used for web services, automation, data analysis, machine learning, scripting, and many other tasks. Its readable syntax helps beginners focus on the logic of a program, while its mature ecosystem provides libraries for larger applications. Python is dynamically typed: a name refers to an object, and the object has a type. This is different from saying that Python has no types; strings, integers, lists, and custom objects still have well-defined behavior.

2. How it works

A typical workflow is to write source code in a .py file, run it with a Python interpreter, read any output or error, and iterate. The interpreter executes statements in order and reports syntax or runtime problems with a traceback. Tracebacks identify the failing line and call path; read the final exception first, then work upward to locate the cause. Python is not automatically faster than compiled languages for every workload, so profile real programs before optimizing. Use Python when its productivity and libraries fit the problem.

3. Use it safely and effectively

Start with a small, observable program. print() displays values during learning, but production applications usually use a logger or return values to a caller. Keep each example reproducible: record the Python version and dependencies when behavior depends on them. Practice by changing one line at a time and predicting the result before running the program.

4. Syntax reference

language = 'Python'
print(f'Hello, {language}!')

5. Guided walkthrough

Save the file with a .py extension, run it with the same interpreter selected for the project, then compare the printed result with what you expected. If Python reports an error, start at the final exception line and inspect the referenced source line before changing code.

6. Common mistakes to avoid

  • Running a different Python installation than the one selected in the editor.
  • Treating a traceback as noise instead of using its exception type and line number to locate the failure.

Visual guide

From source to output

  1. Step 01 Write

    Save statements in a Python file.

  2. Step 02 Interpret

    Run the file with a Python interpreter.

  3. Step 03 Observe

    Read output or diagnose the traceback.

Code examples

Worked example
PYTHON
language = 'Python'
print(f'Hello, {language}!')
Expected output
Hello, Python!
Inspect the active interpreter
PYTHON
import sys

print(sys.version_info[:2])
print(sys.executable)
Expected output
Shows the active Python major/minor version and the interpreter path.
Separate reusable logic from startup
PYTHON
def greeting(name: str) -> str:
    return f'Hello, {name}!'

if __name__ == '__main__':
    print(greeting('Asha'))
Expected output
Hello, Asha!

Key points

  • Understand Python source code, the interpreter, execution, and common use cases.
  • Prefer explicit, testable behavior over hidden side effects.
  • Validate assumptions at system boundaries and handle failure deliberately.

Check your understanding

Practice

What executes a Python source file?

Your learning progress

Finish this lesson