BeginnerLesson 2 of 478 min read4 runnable examples
Python is a general-purpose programming language known for code that is short and easy to read. This lesson explains what Python is, why it is so popular, and what happens when you run a Python program.
In this lesson
What Python is and who made it
The features that make Python popular
Where Python is used
How the interpreter runs your code
What is Python?
Python is a high-level, interpreted, general-purpose programming language.
High-level means you work with ideas such as lists, text and files, not with memory addresses.
Interpreted means you run the source file directly with the python command. There is no separate compile step to manage.
General-purpose means it is not tied to one job. The same language builds web servers, test frameworks and data reports.
Here is a complete Python program. It stores some test results and prints a summary:
You do not need to understand every line yet. Notice how little punctuation there is: no semicolons, no curly braces around blocks, no type declarations.
A short history
Year
Event
1991
Guido van Rossum publishes the first version of Python
2000
Python 2.0 adds list comprehensions and garbage collection of cycles
2008
Python 3.0 cleans up the language and is not fully backward compatible
2020
Python 2 reaches end of life on 1 January; all new code uses Python 3
The name comes from the comedy group Monty Python, not from the snake.
Key features of Python
Feature
What it means for you
Simple, readable syntax
Indentation shows the structure, so code looks the same everywhere
Dynamically typed
You do not declare types; a variable takes the type of its value
Batteries included
The standard library handles files, JSON, CSV, dates, regex, HTTP and more
Cross-platform
The same script runs on Windows, macOS and Linux
Multi-paradigm
Write procedural, object-oriented or functional code
Huge ecosystem
PyPI hosts packages such as pytest, Playwright, requests, NumPy and Pandas
Free and open source
Python is developed by the Python Software Foundation and the community
Dynamic typing in action: the same name can hold values of different types, and type() tells you what it holds right now.
Python’s design ideas are written down as a short poem. Run import this to print it:
Python
importthis
Output
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
Note
“Readability counts” and “Simple is better than complex” are the two lines you will feel most while learning. Python code is read far more often than it is written.
What is Python used for?
Test automation: pytest, Selenium, Playwright and requests for UI and API testing.
Web development: Django, Flask and FastAPI power web sites and REST APIs.
Data analysis: NumPy, Pandas and Matplotlib turn raw data into answers.
Machine learning and AI: scikit-learn, PyTorch and TensorFlow.
Scripting and automation: renaming files, sending reports, cleaning spreadsheets.
DevOps and cloud tooling: Ansible, AWS and Azure SDKs, CI scripts.
How Python runs your code
When you run python hello.py:
The interpreter reads your source code and checks the syntax.
It compiles the code to bytecode, a simple instruction format.
The Python virtual machine executes the bytecode line by line.
You can see which interpreter is running and where with the sys module:
The standard interpreter is called CPython because it is written in C. Other implementations exist, such as PyPy (faster for long-running code) and Pyodide, which runs Python in a browser and powers the Try it buttons on this site.
Check your understanding
Quick quiz
1. Who created Python?
Guido van Rossum released Python in 1991. James Gosling created Java and Dennis Ritchie created C.
2. What does “dynamically typed” mean?
In Python you write x = 5 and x holds an int; assign a string later and it holds a str.
3. Which version should new projects use?
Python 2 reached end of life in January 2020. Python 3 is the only supported version.
Program Practice
Ready to write code? The basic programs use only variables, arithmetic and print(), so they are a good first set.
40 Python Basic Programs
Every program has a step-by-step explanation, colour-coded code and its real output.
Both, in a sense. CPython compiles your source to bytecode and then interprets that bytecode. You only ever run one command, python file.py, so it feels interpreted.
Is Python good for beginners?
Yes. Its syntax is short and readable, error messages point to the exact line, and you can get useful results with very little code.
Is Python free?
Yes. Python is open source and free to use for personal and commercial projects.
What is the difference between Python and Java?
Java is statically typed and compiled to JVM bytecode ahead of time; Python is dynamically typed and run directly. Python programs are usually shorter, while Java catches more type errors before the program runs.
Prepare for your Python interview with free, timed Python MCQ tests written for QA engineers and developers. Pick a topic and answer 20 random questions in 20 minutes from a 200-question bank. You get your score, every correct answer with an explanation, a score split into QA and developer questions, and an interview readiness score that grows as you practise.
How the Python MCQ tests work
15 interview topics from Python basics to pytest, OOP and pandas. New topics open as their question banks are published.
20 questions per test, 200 per topic: questions you have not seen come first, so about 10 attempts cover a whole topic.
20-minute timer and live progress bar: see how many questions you have answered; the test submits itself when time runs out.
Real interview questions: predict-the-output code, common bugs, best practice and test-automation scenarios. Every code question was run on Python 3 to confirm the answer.
Instant review: pass mark 70%, the correct answer and a short explanation for every question, and a link to the lesson to revise.
Your Python learning progress: topics mastered, topics still to practise and what to study next, saved in your browser.
How interview readiness is calculated
Readiness combines how much you have practised with how well you score: readiness = (tests completed out of 20) × (accuracy). Accuracy is the average of your last three scores in each available topic; a topic you have not tried counts as 0%.
Yes. Every test is free and needs no sign-up. Your results and progress are stored only in your own browser.
Who are these Python MCQs for?
For QA and test automation engineers and for developers preparing for Python interviews. Each question is tagged as a QA or developer question, and your result shows your score in both.
How many questions are in each Python MCQ test?
Each test has 20 multiple-choice questions drawn at random from a 200-question bank for that topic, with a 20-minute time limit and a 70% pass mark.
Are the answers correct?
Every predict-the-output question was executed on Python 3 and its answer is the real output. Each question also has an explanation, so you learn why the answer is right.
Where can I learn the topics before testing?
Every topic links to its lesson in the free sqatools Python tutorial, and the result page shows a “Revise the lesson” button when you do not pass.
Will my progress be saved?
Progress is saved in your browser’s local storage on this device. Clearing site data or switching browsers starts a fresh history.
48 object-oriented programming exercises: constructors, instance and class variables, class and static methods, every kind of inheritance, overriding, abstract classes, encapsulation, properties and small real-world systems such as bank accounts, carts and courses.
48 set programs: creating sets, adding and removing items, union, intersection, difference, subsets and frozensets. Sets of strings have no fixed order, so these solutions print sorted(...) whenever the order would otherwise change from run to run.
Before you start
Read the Sets lesson first if the topic is new to you.
Write a Python program to sort the lines of a file by their length.
Output
x
short
medium line
a much longer line here
Approach
sorted(lines, key=len).
Solution
Python
withopen("story.txt","w")asf:f.write("medium line\nx\na much longer line here\nshort\n")withopen("story.txt")asf:lines=f.read().splitlines()forlineinsorted(lines,key=len):print(line)
Output
x
short
medium line
a much longer line here
Learn the concept
New to this topic? Read the lesson, then come back.