Skip to lesson content

PYTHON / LESSON 2 OF 18

Introduction to Python

Read the explanations, work through the examples, and complete the student tasks.

Learning goals

Run a script, explain Python's main strengths, and distinguish an interactive interpreter from a saved program.

Explanation

Python is a high level, general purpose language used for automation, web applications, data analysis, testing, and many other tasks. Indentation makes blocks visible and readable. Its standard library includes tools for files, dates, text, and more. Additional libraries extend it.

The image describes Python as interpreted. More precisely, CPython normally compiles source into bytecode and executes it in its interpreter. You usually do not perform a separate compilation step yourself. Cross platform does not mean every operating system command or path is portable.

Setup and running code

Install Python 3 from https://www.python.org/downloads/ using the instructions for your operating system. On Windows, try the following in PowerShell or Command Prompt:

py --version
py hello.py

On macOS or Linux, the command is commonly python3 instead of py. If python already points to Python 3, that command works too. Verify the version before continuing. A command not found error means the command or installation needs attention; it is not a problem in hello.py.

Example 1 Your first saved program

Create hello.py in a folder you can find again:

print("Hello, QA Insights!")
print("I am learning Python.")
print("One week of practice:", 7 * 30, "minutes")

Expected output:

Hello, QA Insights!
I am learning Python.
One week of practice: 210 minutes

Open a terminal in the file's folder and run the appropriate command. Python reads the saved file; unsaved editor changes are not included.

Example 2 Interactive exploration

Start Python with py or python3 and type 5 * 6 at the >>> prompt. The interpreter displays 30. In a saved script, use print(5 * 6) to display the same result. The prompt characters >>> are not part of your code. Use exit() to leave the interactive interpreter.

Common mistakes

A filename such as hello.py.txt is not the intended Python script. Also avoid naming a file after a library you will import, such as random.py or json.py, because it can hide the real module.

Student tasks

1. Create welcome.py that prints your name, city, and why you want to learn Python, on separate lines.

2. Run the same expression interactively and in a saved script. Describe why print is useful in the script.

3. Create study_time.py to calculate the minutes spent studying for 5 days at 40 minutes per day. Expected total: 200.

Completion check: show the version command and the output of both saved scripts.