A datatype defines how different types of information/data are stored and behave. Examples of the behavior can be found on the pages Operators, Built-in Functions & Methods, …
For almost every datatype in Python, there is a function with the same (or similar) name that is used to create an object of that datatype or convert something from a different datatype.
Name | Function | Example | Explanation |
---|---|---|---|
Integer | int() | 0 , 11 , -3 | whole number |
Float | float() | 1.5 , 1e-4 | rational/floating-point number |
Boolean | bool() | True , False | truth value, can only be True or False |
String | str() | 'abc' , f"x:\n{x}" | text, can include, numbers, special characters, … |
List | list() | [1, 1, 2, 3] | mutable collection of any datatype |
Tuple | tuple() | (1, 1, 2, 3 ) | unmutable collection of any datatype |
Set | set() | {2, 1, 3} | mutable collection of any datatype without order, indexing or duplicates |
Dictionary | dict() | {"a": 1, "c": "5"} | 1 to 1 mapping of not mutable keys to mutable values of any datatype |
None | None | None means that there is no value | |
Complex | complex() | 1j , 1.2-0.5j | complex number with real and imaginary part |
Range | range() | range(8) , range(2, 99, 3) | range of numbers |
not exam relevant
These functions/methods are mostly not exam relevant.
There are a few built-in functions/methods to check if something is a specific type of data. These are helpful to check input, potentially before converting it.
- String Methods:
.isdigit()
,.isalpha()
,.isalnum()
,.isdecimal()
,.isnumeric()
,.isspace()
. - Type Checking:
isinstance(obj, type)
,type(obj)
,callable(obj)
. - Regular Expressions
Basic Datatypes
Integer
The datatype Integer can hold any whole number including negatives and zero. You can use the function int()
to convert a value to an Integer, or create one with the value 0
if no argument is given.
1 4 -2 0 -4 962 -123456789 int("2")
Float
The datatype Float can hold any rational/floating-point number including negatives and zero. You can use the function float()
to convert a value to a Float or create one with the value 0.0
if no argument is given.
1.5 -0.5 0.0 3.3333 1e-4 2.5e3 -5e6 float("1.3")
Scientific Number Notation
not exam relevant
This topic is not exam relevant.
Python supports the scientific notation of writing numbers as a power of 10 for integers and floats. To do this you can give numbers in the format “number
epower
”. So for example 1e3 == 1 * 10**3 == 1000
or -1.5e-2 == -1.5 * 10**-2 == -0.015
.
Boolean
The datatype Boolean can hold a truth value. There are only two possible options for this value True or False (can also be thought of as 1/0 Yes/No On/Off). This datatype is very important for Control Flow like Conditions because it is usually based on many “Yes/No Questions”, which are answered with a Boolean value. You can use the function bool()
to convert a value to a Boolean, or create one with value False
if no argument is given.
True False bool(1)
Collection Datatypes
Collection datatypes are a combination of multiple basic datatypes.
General
Below are some key properties of the collection datatypes available in Python.
Name | Mutable | Iterable | Indexing | Slicing | Common Use Case |
---|---|---|---|---|---|
String | No | Yes | Yes | Yes | Text processing and manipulation |
List | Yes | Yes | Yes | Yes | Storing and manipulating mutable sequences |
Tuple | No | Yes | Yes | Yes | Fixed/Constant collections of items |
Set | Yes | Yes (but no order) | No | No | Ensuring unique elements in a collection, Set Logic |
Dictionary | Yes | keys, values, or pairs | Keys | No | Key-value pair storage and lookups |
Range | No | Yes | Yes | Yes | Generating sequences of numbers |
Generator | No | Yes | No | No | Efficient iteration over large datasets |
Mutable
A mutable object can be modified after it is created, a immutable object can not. This means you can change, add, or remove parts or all of its content without creating a new object. For example, you can change the value of the second item in a list, but you can not change the value of the second item in a tuple or string. All basic datatypes are immutable. So when it seems like the value changes, it is actually a completely new object. You can find the difference in behavior between mutable and immutable variables here.
Iterable
An iterable is an object that can return its elements one at a time, allowing it to be used in a loop (e.g., for
loops).
Indexing
Indexing refers to accessing individual elements of a sequence using their position (index). Python uses zero-based indexing, meaning the first element has an index of 0
, the second one has an index of 1
… You can also start indexing from the end of the sequence using negative numbers starting from -1, so the last element has an index of -1
, the second last has an index of -2
…
[3, 1, 7, 4, 5, 2] # list of values
0 1 2 3 4 5 # positive indexes
-6 -5 -4 -3 -2 -1 # negative indexes
Indexing can be used to get the value of an element or also to set/change it. To use it you first put the sequence and then the index inside square brackets sequence[index]
.
listName = [3, 1, 7, 4, 5, 2] # Create list and save in variable listName
print(listName[2]) # get and print the third element of the list -> 7
listName[0] = -1 # Set the first element of the list to -1
print(listName) # print the whole list -> [-1, 1, 7, 4, 5, 2]
A dictionary does not have classic ordered indexing with numbers. Instead, each element is a combination of key and value, and you use the key instead of an index
Slicing
partly exam relevant
Basics are relevant, advanced usage is not necessary.
Slicing is used to select a subsequence/portion of a sequence, instead of only one element. You can specify a start, stop, and step.
sequence[start:stop:step]
start
: The starting index (inclusive). Defaults to0
(first element) if omitted. Negative values work the same way as with indexing.stop
: The ending index (exclusive). Because it is exclusive, the element with this index is not included, to include the last element you have to use one more than its index. Defaults to the last element if omitted. Negative values work the same way as with indexing.step
: The interval between elements. Defaults to1
(every element without skipping). Negative values reverse the order of the sequence.
If you get elements of a sequence using slicing, it will create a shallow copy of the sequence and give it to you.
listName = [3, 1, 7, 4, 5, 2, 9] # Create a list and save it in variable listName
# Index 0 1 2 3 4 5 6
print(listName[1:3]) # elements from the 2nd (inclusive) to the 4th (exclusive) -> [1, 7]
print(listName[:4]) # elements from the 1st to the 5th -> [3, 1, 7, 4]
print(listName[2:]) # elements from the 3rd to the last (inclusive) -> [7, 4, 5, 2, 9]
print(listName[:]) # all elements -> [3, 1, 7, 4, 5, 2, 9]
print(listName[0:5:2]) # every second element from the 1st to the 6th -> [3, 7, 5]
print(listName[::3]) # every third element from the whole sequence -> [3, 4, 9]
print(listName[-5:-1:1]) # elements from the 5th last (inclusive) to the last (exclusive) -> [7, 4, 5, 2]
print(listName[::-1]) # all elements in reverse order -> [9, 2, 5, 4, 7, 1, 3]
print(listName[6:1:-2]) # every second element from the 7th to the 2nd in reverse order -> [9, 5, 7]
print("abcde"[::-1]) # all elements in reverse order -> edcba
not exam relevant
Not exam relevant, rarely used, but sometimes helpful.
You can also set multiple values at the same time using slicing. If you don’t give the correct numbers of values to set/replace, you might get unexpected behaviour or an error.
listName = [3, 1, 7, 4, 5, 2, 9] # create a list and save it in variable listName
listName[1:4] = [10, 20, 30] # replace elements from the 2nd to the 5th -> [3, 10, 20, 30, 5, 2, 9]
listName[-2:] = [400, 500] # replace elements from the 2nd last to the last -> [3, 10, 20, 30, 5, 400, 500]
listName[::2] = [1, 2, 3, 4] # replace every second element -> [1, 10, 2, 30, 3, 400, 4]
listName[:0] = [-1, -2] # insert two new elements at the start of the sequence -> [-1, -2, 1, 10, 2, 30, 3, 400, 4]
listName[2:5] = [] # remove the elements from the 3rd to the 6th -> [-1, -2, 30, 3, 400, 4]
String
The datatype String can hold any text including numbers and special characters. In Python, all strings have to be within quotes single 'string'
or double "string"
quotes, using triple quotes has a special behavior. You can use the function str()
to convert a value to a String or create one with the value ""
if no argument is given. Strings also supports indexing, slicing and some list functions/methods. But it is impossible to change individual elements/characters of a string because it is immutable.
"text" 'string' "some long message" "line 1\nline 2" """triple quotes string"""
You might think of a string as a base datatype, but it actually behaves very similar to a tuple of individual characters. So especially in low-level languages you will find character
as a base datatype and the string is just a collection of characters.
Special Characters
partly exam relevant
Basics are relevant, more advanced usage is not necessary. The first 5 rows of the table are relevant, the others are not.
Python strings support many special characters. To use a special character first write a backslash \
to escape the string and inform Python that what follows is a special character. After the backslash, you can put one of the supported special characters.
Character | Description | Example |
---|---|---|
\n | Newline character. Moves the cursor to the next line. | "Hello\nWorld" → Hello World |
\t | Horizontal tab. Adds a tab space. | "Hello\tWorld" → Hello World |
\\ | Backslash. Escapes the backslash itself. | "Path: C:\\Users" → Path: C:\Users |
\' or '' | Escapes a single quote within single-quoted strings. | 'It\'s a test' → It's a test |
\" or "" | Escapes a double quote within double-quoted strings. | "She said ""Hello\"" → She said "Hello" |
\r | Carriage return. Moves the cursor to the beginning of the line and overwrites characters. | "123456789\rWorld" → World6789 |
\b | Backspace. Removes the character before it. | "Hello\bWorld" → HellWorld |
\x** | Character with a with a 2-digit hex code. | "\x48" → H |
\u**** | Unicode character with a 4-digit hex code. | "\u2764" → ❤ |
\U******** | Unicode character with an 8-digit hex code. | "\U0001F600" → 😀 |
If you want to prevent special characters from being interpreted you can use raw strings r"string"
.
print("\tline1\nline2") # normal string
print(r"\tline1\nstill line1") # raw string
print(r"Test\of\raw\string") # raw string
line1
line2
\tline1\nstill line1
Test\of\raw\string
Formatted String
partly exam relevant
Basics are relevant, advanced usage is not necessary.
Formatted strings in Python provide an easy way to insert values into a string. To make a formatted string put an f
in front of the the starting string quotes. Inside a formatted string, all the values within curly brackets {value}
will be inserted into the string. The datatype will automatically be converted and there is also support for operators, functions, and special formatting. If you want to print a curly bracket within an f-string, you have to put a double curly bracket {{
.
b = 5
pi = 3.14159
name = "Andreas"
print(f"My name is {name} and I am {19} years old.") # -> My name is Andreas and I am 19 years old.
print(f"The sum of {a} and {b} is {a+b}") # -> The sum of 1 and 5 is 6
print(f"The user inputted: {input()}") # -> The user inputted: {?}
# There are many advanced formatting options
print(f"Pi with two decimal places: {pi:.2f}") # -> Pi with two decimal places: 3.14
print(f"Documenting operations: {1 + 3 * 2 = }") # -> Documenting operations: 1 + 3 * 2 = 7
Multiple Line String
less exam relevant
Less exam relevant, but still good/helpful to know.
Instead of using single or double quotes at the start and end of a string, you can also use them three times ('''
or """
) to create a multiline string. So within triple quotes, you can have multiple lines of text and it will keep the format, linebreaks, and spacing. You can also combine this with Formatted String.
print("""This is a
multiline string
indentation is also
as in the code""")
print(f"""Below you can see an operation
{1 + 1 = }""")
This is a
multiline string
indentation is also
as in the code
Below you can see an operation
1 + 1 = 2
List
The datatype List can hold a sequence of any datatypes. It is also possible to put collection datatypes inside a list. All list elements are within square brackets []
and separated by commas ,
. You can use the function list()
to convert an Iterable value to a list or create an empty list []
if no arguments are given. Lists also support indexing, slicing, and many built-in functions/methods. You can add, change or remove elements, because it is mutable.
[1, 1, 2, 3] [] [1, "hello", 3.14, True] [[1,2,3], (-1,-2,-3), {10,20}, {"a": 1, "c": "5"}]
Tuple
You can think of a tuple like a List where it is impossible to add, change or remove elements. The reason for this is, that a tuple is |immutable. The tuple elements are within parentheses/round brackets ()
and separated by commas ,
. You can use the function tuple()
to convert an Iterable value to a tuple or create an empty tuple ()
if no arguments are given.
(1, 1, 2, 3) () (1, "hello", 3.14, True) ([1,2,3], (-1,-2,-3), {10,20}, {"a": 1, "c": "5"})
Set
not exam relevant
This topic is not exam relevant, but it is still good to know that this datatype exists and its general behavior.
A set is similar to a List but the elements are not stored in any specific order. Because of this, Indexing and Slicing is not possible. Iterating is possible, but there is no order defined for the elements. The set elements are within curly brackets {}
and separated by commas ,
. You can use the function set()
to convert an Iterable value to a set or create an empty set if no arguments are given. You can only create an empty set using the set()
function because a Dictionary also uses curly brackets. You can add or remove elements, because it is mutable. A set can only store immutable elements. A set does not allow for any duplicate elements, if you have duplicates only one of them will be stored. A few list operators, functions, and methods will work and there are multiple special ones for sets.
{1, 2, 3} set() {1, 3.14, "hello"} {"hello", True, 2, 3.14}
print({1, 2, 1, 1, 3, 2, 3, 4, 5}) # duplicates removed, no specific order -> {3, 5, 1, 4, 2}
Dictionary
The datatype Dictionary creates a 1 to 1 mapping between keys and values. So instead of using an index, the values are uniquely identified by their key. A value can be any datatype. A key has to be unique (every key can only exist once) and immutable. The dictionary elements are within curly brackets {}
and separated by commas ,
. One element is a combination of a key and a value separated by a colon :
like this key:value
.
{"a": 1, "c": "5"} {} {1: "test", "1": 123, "a": 1, (1,2): 1}
You can use the function dict()
to convert values to a dictionary or create an empty dictionary {}
if no arguments are given. You can use dict()
with no arguments, an Iterable of key and value pairs, or keyword arguments.
print(dict()) # empty dictionary -> {}
print(dict([("name", "Tom"), ("age", 25), ("city", "Linz")])) # dictionary from pairs -> {'name': 'Tom', 'age': 25, 'city': 'Linz'}
print(dict(name="John", age=30, country="USA")) # dictionary from keyword arguments -> {'name': 'John', 'age': 30, 'country': 'USA'}
You can get and set the value of a key the same way you would use normal indexing. You can add, change or remove elements, because it is mutable. If you want to add a value you can use assignment to the new key with the syntax dictName[newKey] = newValue
. If the key already exists it will change the value of the existing key.
person = {"name": "Tom", "age": 25, "city": "Linz"}
print(person["age"]) # print age of person -> 25
person["country"] = "Austria" # add a new element with key "country" and value "Austria"
person["age"] = 19 # change the value of age to 19
print(person) # -> {'name': 'Tom', 'age': 19, 'city': 'Linz', 'country': 'Austria'}
Special Datatypes
None
The datatype None represents the absence of a value or a “null” value. It is often used to indicate that a variable or function does not have any value. None can be used as a placeholder, for example when defining a variable before its actual value is known. Having None
is not an error it is just saying that there is no value/the value is nothing.
For example, if you try to get a variable value or list element that does not exist, you will get an error message. But storing/printing the value returned from a function that returns nothing is not an error, it just has the value None
.
x = print() # The print function does not return anything
print(x) # -> None
Complex
less exam relevant
Less exam relevant, rarely used.
The datatype Complex represents a complex number. It has a real and an imaginary part, represented as a + bj
, where a
is the real part and b
is the imaginary part. If you have a number with an imaginary part in Python, it will treat/save it as a complex number. You can use the function complex()
to convert a value to a Complex, or create one with value 0j
if no argument is given. This function can take two numbers, the real part a
and the complex part b
, as arguments complex(a, b)
.
1+3j 1j -3+1j 0.1-1.5j 1-20000j 1-2e4j -0.001-1j -1e-3-1j
You can access the real or imaginary part of a complex number individually using the attributes .real
and .imag
.
z = 3 + 4j
print(z.real) # -> 3.0
print(z.imag) # -> 4.0
Range
partly exam relevant
Basic usage for looping and creating number sequences is necessary. It is good to understand that this is different from list because it does not store all elements in memory and instead generates them on demand. Advanced usage and explanation are not necessary.
The datatype Range is created with the range()
function and generates a sequence of numbers. It might seem very similar to a list, but the key difference is that not all elements are saved in memory. Instead, they are generated only when they are actually used. It is commonly used in loops and is a memory-efficient Iterable. You can not add, remove, or change elements of a range because it is |immutable.
The range()
function generates a range of numbers. The arguments are similar to slicing and you can use range(stop)
, range(start, stop)
, or range(start, stop, step)
start
: The starting number (inclusive), default is0
.stop
: The ending value (exclusive). Because it is exclusive, this value is not included, to include the last value you have to use one more than it.step
: The interval between elements. Defaults is1
(every number without skipping). Negative values are also possible, but thenstart
should be bigger thanstop
.
print(list(range(5))) # -> [0, 1, 2, 3, 4]
print(list(range(1, 10))) # -> [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(list(range(-10, 10, 2))) # -> [-10, -8, -6, -4, -2, 0, 2, 4, 6, 8]
print(list(range(100, 0, -9))) # -> [100, 91, 82, 73, 64, 55, 46, 37, 28, 19, 10, 1]
not exam relevant
This is rarely used and not exam relevant.
Indexing is possible and returns a number. Slicing is possible and returns a new range.
print(list(range(20))) # -> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
print(range(20)[-5]) # -> 15
print(range(20)[2::-3]) # -> range(19, 5, -3)
print(list(range(20)[:5:-3])) # -> [19, 16, 13, 10, 7]
Generator
not exam relevant
This topic is not exam relevant. It is a bit advanced and rarely used.
The datatype Generator is very similar to Range in supporting memory efficient one by one iterating of elements. The key difference is that it supports all datatypes and you can define your own sequence generators. But they do not support indexing or slicing and it is |immutable.
OOP
only SDML and AECD
This topic is an advanced topic that is only relevant for the SDM and AECD departments. There will be a note only for this at the end.
You can basically define and use your very own datatypes using Object Oriented Programming. You can also import special datatypes created by others. First, you have to define all the attributes (=data) and methods (=behavior) in a class. Then you can create objects based on the class.
Internal Details
less exam relevant
Less exam relevant, rarely used.
Truthy and Falsy
In Python, any value can be evaluated (e.g. in a logical operator, condition, …) like a boolean. Python will treat it as truthy (True
) or falsy (False
) based on its type and value.
- Falsy Values:
False
,0
,0.0
,0j
,""
,[]
,()
,set()
,{}
,None
,range(0)
- Truthy Values: Everything else so not empty collections and not zero numeric values
print(bool(0-1j)) # True
print(bool([])) # False
print(bool(range(0))) # False
print(bool(0.000001)) # True
print(bool("")) # False
print(bool(" ")) # True
Additional not exam relevant topics for later
Contents
Converting between Datatypes
Implicite Conversions
Storage Size and Value Range
Binary
Building Datatypes from Binary
Questions
- What is a datatype and what is its purpose?
- Explain Integer in your own words.
- What is the difference between an Integer and a Float?
- Explain Float in your own words.
- Explain the scientific notation in Python.
- 3 Explain Boolean in your own words.
- What is the Boolean type in Python used for?
- Explain String in your own words.
- Explain special characters in your own words.
- Explain Formatted String in your own words.
- Explain Raw and Multiline String in your own words.
- Explain List in your own words.
- Explain Mutable in your own words with examples.
- Explain Iterable in your own words with examples.
- Explain Indexing and Slicing in your own words with examples.
- Can Lists store different data types in Python?
- Give a practical example of list inside a list and how you can index the inner list.
- Explain Tuple in your own words.
- What makes a Tuple different from a List?
- What are some use cases where you would prefer a Tuple compared to a List?
- Explain Set in your own words.
- What are the main characteristics of a Set in Python?
- How does a Set differ from a List or Tuple?
- What values can you store in s set and what is special about this?
- Explain Dictionary in your own words.
- How can you access values in a Dictionary?
- What datatypes can you use a Dictionary key?
- What datatypes can you use a Dictionary value?
- Explain None in your own words.
- Give examples where you will get or use a None value.
- Explain Complex in your own words.
- What is a Complex number in Mathematics and Python?
- How can you get the real and imaginary parts individually?
- Explain Range in your own words.
- How does a Range differ from other types like a List?
- Explain Generator in your own words.
- Explain truthy and falsy in your own words.