Tip
Launch the Interactive Shell by running the python3.12
command in your terminal.
Enter 2 + 2
into the prompt to have Python do some simple math.
After pressing Enter it will display the result 4
and create a new prompt >>>
for further inputs.
>>> 2 + 2
4
>>>
In Python, 2 + 2
is called an expression, which is the most basic kind of programming instruction.
Expressions always evaluate (that is, reduce) down to a single value.
In the previous example, 2 + 2
is evaluated down to a single value, 4
.
The simplest type of an expression is a literal. A literal only consists of a single value and always evaluates to itself.
>>> 2
2
The expression 2 + 2
we evaluated earlier is an arithmetic expression.
An arithmetic expression consists of an operator and either one ("unary") or two ("binary") operands.
The table below shows the most commonly used arithmetic operators with x
and y
representing the operands1.
Expression | Operation | Example | Result |
---|---|---|---|
x + y |
sum of x and y | 3 + 4 |
7 |
x - y |
difference of x and y | 5 - 2 |
3 |
x * y |
product of x and y | 2 * 5 |
10 |
x / y |
quotient of x and y | 10 / 4 |
2.5 |
x // y |
floored quotient of x and y | 10 // 4 |
2 |
x % y |
remainder of x / y | 10 % 3 |
1 |
-x |
x negated | -5 |
-5 |
+x |
x unchanged | +5 |
5 |
x ** y |
x to the power of y | 2 ** 3 |
8 |
Operands can not only be literal values, but any expression that evaluates to a number. That means we can combine arithmetic expressions however we like. The order of operations (also called precedence) of math operators applies.
>>> 2 + 3 * 6
20
You may use parantheses to override the usual precedence.
>>> (2 + 3) * 6
30
For complex expressions Python will keep evaluating parts of the expression until it becomes a single value.
>>> (5 - 1) * ((7 + 1) / (3 - 1))
16.0
Remember that expressions always evaluate down to a single value. A data type is a category for values, and every value belongs to exactly one data type. Let's start with the most essential data types: integer, floating-point number and strings.
Integers are whole numbers.
For example the literal expressions 5
, -2
, 0
, 9999
and the arithmetic expressions 2 + 2
, 5 - 2
, 2 * 5
, 10 // 4
, 10 % 3
, 2 ** 3
all represent a value of the integer data type.
You can also insert underscores _
into integer (and floating-point number) literals to improve readability without changing the value.
This is useful for very large numbers, like 1_000_000
, which is equivalent to 1000000
.
Numbers with a decimal point are called floating-point numbers (or floats).
Some literal expressions representing floats are -1.25
, 0.5
, 0.0
, 0.000
, 1.0
, +4.8
, 9999.99
.
Tip
Even though the value 42
is an integer, the value 42.0
is a floating-point number.
Float literals also support scientific notation, using either a lowercase e
or capital E
to denote the exponent, e.g. 1e100
, 3.14e-10
, 0e0
, -999E99
2.
Arithmetic expressions work the same way for floats as they do for integers.
>>> 2.0 + 2.0
4.0
Tip
Division (/
) always evaluates to a float, even if both operands are integers.
>>> 10 / 2
5.0
To make our lifes easier Python allows us to mix integer and floats in arithmetic expressions. In this case the expression will always evaluate to a float.
>>> 10.0 // 4.0
2.0
>>> 10.0 // 4
2.0
>>> 10 // 4.0
2.0
>>> 10 // 4
2 # <- this is an integer!
A String is a sequence of letters and any other character defined in the Unicode Standard. This also includes spaces, numbers and even emojis. Think of a string as a piece of text.
In general a string literal is surrounded in quotes so Python knows where the string begins and ends:
Example | |
---|---|
Single Quotes | 'aababab' , 'Alice' , 'Hello World!' , 'allows embedded "double" quotes' |
Double Quotes | "aababab" , "Alice" , "Hello World!" , "allows embedded 'single' quotes" |
Three Single Quotes | '''aababab''' , '''Alice''' , '''Hello World!''' |
Three Double Quotes | """aababab""" , """Alice""" , """Hello World!""" |
You can even have a string with no characters in it, called a blank string or an empty string:
''
, ""
, ''''''
, """"""
.
Since strings are not numeric like integers and floats, you cannot use arithmetic operations. Here, we use sequence operations to combine strings instead3.
Expression | Operation | Example | Result |
---|---|---|---|
s + t |
The concatenation of s and t |
'hello' + 'world' |
'helloworld' |
s * n or n * s |
Equivalent to adding s to itself n times |
'abc' * 3 |
'abcabcabc' |
s[i] |
i-th item of s , starting at 0 |
'hello'[1] |
'e' |
s[i:j] |
Slice of s from i to j |
'hello'[1:3] |
'el' |
s[i:j:k] |
Slice of s from i to j with step k |
'abcdefghij'[1:6:2] |
'bdf' |
len(s) |
Length of s |
len('hello') |
5 |
s.index(x) |
Index of the first occurrence of x in s |
'hello'.index('l') |
3 |
s.index(x, i) |
Index of the first occurrence of x in s at or after index i |
'hello'.index('l', 3) |
3 |
s.index(x, i, j) |
Index of the first occurrence of x in s at or after index i and before index j |
'hello'.index('l', 3, 5) |
3 |
s.count(x) |
Total number of occurrences of x in s |
'hello'.count('l') |
2 |
In the table above, s
, t
are strings and i
, j
, k
, n
are integers.
Tip
Remember that s
and t
don't have to be literals.
They can be any expression that evaluates to the string data type.
The same applies to the integers i
, j
, k
, n
respectively.
>>> ('knock' * (12 // 4)) + 'penny'
'knockknockknockpenny'
For better understanding of the slice and index operations it's worth mentioning that every character in a string, including spaces, is assigned to an integer.
The first character in the string is assigned to the number 0
, the second to 1
and so on.
These numbers are called index (pl. indices).
For the string literal 'Hello World!'
the indices are as follows.
Character | H |
e |
l |
l |
o |
W |
o |
r |
l |
d |
! |
|
---|---|---|---|---|---|---|---|---|---|---|---|---|
Index | 0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
So far we have only created literals and combined them using arithmetic and sequence operations. We need some way of storing any information and manipulate them as well. This is where variables come into the picture. Variables are exactly what the name implies: Their value can vary, i.e., you can store anything using a variable. Variables are just parts of your computer's memory where you store some information.
Unlike an expression, which always evaluates to a single value, a statement describes an instruction that only does something but not is something.
The first statement you will use frequently is the assignment statement and consists of a variable name (called an identifier ), the assignment operator =
, and an expression whose result to store.
For example we create a variable named x
and store the value 5
in it:
>>> x = 8
Here, x
is the identifier, and 5
is a literal expression.
Likewise, we create another variabled with the identifier y
, where we store the result of the arithmetic expression 2 + 2
.
>>> y = 2 + 2
If we assign something to a variable that already exists, the value that's currently being stored gets overwritten by the new one.
For example after executing the following two assignment statements, x
contains the value 5
:
>>> x = 8
>>> x = 5
Identifiers are not limited to single characters. You can choose any identifier as long as you adhere to the following rules:
- The first character of the identifier must be a letter of the alphabet or an underscore (
_
). - The rest of the identifier name can consist of letters, underscores (
_
) or digits (0
–9
).
Tip
Identifier names are case-sensitive. For example, myname
and myName
are not the same.
For example x
, Name
, name_2_3
are all valid identifiers.
Invalid identifiers are e.g. 2things
, this is spaced out
, my-name
and >a1b2_c3
.
To get the result of a variable we introduce a new type of expression: Variable Expressions. Once a variable is created and given an identifier, that very same identifier becomes a valid expression and evaluates to the value stored in the variable.
We can use these in the interactive shell to read the values stored in the variables x
and y
we created earlier.
>>> x
5
>>> y
4
>>> x + y
9
Note
Pop Quiz: What gets displayed after executing the following?
>>> a = 8
>>> b = 2 * 3
>>> b = b * 2
>>> a + b
?