← Back to Programming

Getting started with Python

Python is one of the most popular programming languages in the world, prized for readable code. This lesson covers running Python, values, variables, and simple input and output.

Why Python

Python was created by Guido van Rossum and first released in 1991. Its design emphasises readability: code uses plain words and indentation instead of many symbols. It is used for teaching, web servers, automation, data science, and machine learning, and has a vast collection of free libraries.

Running Python

You can run Python interactively in a REPL (read-evaluate-print loop), typing a line and seeing the result immediately, or save code in a file ending in .py and run it with a command such as python hello.py. Editors such as VS Code and notebooks such as Jupyter are popular ways to write it.

Your first program

The classic first program is print("Hello, world!"). print is a built-in function that displays its arguments. Text in quotes is a string. Python is case-sensitive, so Print with a capital P would cause an error. Lines starting with # are comments, ignored by Python but useful for explaining code to people.

Values and types

Every value has a type. Integers (int) are whole numbers such as 42; floats are decimals such as 3.14; strings (str) are text such as "cat"; Booleans (bool) are True or False. The built-in function type() reports a value's type: type(3.0) is float.

Variables

A variable is a name that refers to a value, created by assignment: age = 15. Later, age = age + 1 makes age refer to 16. Names may contain letters, digits, and underscores but cannot start with a digit; by convention Python uses lowercase words joined by underscores, such as total_score.

Arithmetic

Python supports + (add), - (subtract), * (multiply), and / (divide, which always gives a float, so 7 / 2 is 3.5). // is floor division (7 // 2 is 3), % gives the remainder (7 % 2 is 1), and ** is exponent (2 ** 10 is 1024). Normal precedence applies: multiplication before addition, with brackets to override.

Input and conversion

input() pauses, shows a prompt, and returns what the user types as a string. To do arithmetic, convert it: age = int(input("Age? ")). Converting text that is not a number, such as int("ten"), raises a ValueError. str() converts numbers to text, and float() converts to decimals.

Worked example

A tip calculator: bill = float(input("Bill: ")); tip = bill * 0.15; print("Tip:", round(tip, 2)); print("Total:", round(bill + tip, 2)). If the user types 40, the tip is 6.0 and the total 46.0. round(x, 2) rounds to two decimal places. Using an f-string, print(f"Total: {bill + tip:.2f}") shows 46.00.

Key takeaways

Python, released in 1991 by Guido van Rossum, is readable and widely used. Run it in a REPL or from .py files. print() displays output; # starts comments; Python is case-sensitive. Core types are int, float, str, and bool. Variables are created by assignment. / always gives a float; // is floor division; % is remainder; ** is power. input() returns a string, so convert with int() or float(). f-strings format output neatly.

Flashcard

Python

Readable language released in 1991

Guido van Rossum.

Check your understanding

What does the expression 17 % 5 evaluate to in Python?

Next lesson →