Data Types#

As you probably know from MATLAB: there are several types of data we want and can store in variables: numbers, logical values, text, lists…

One thing in advance: We can check for the type of a variable by type(VARIABLE)

Integer#

Integers embody whole numbers and can be added together, subtracted and so on…

a = 1
b = 2

print(a-b)
print(type(a))
-1
<class 'int'>

Float#

Floats are “floating point” numbers. So they include decimals.

a = 1.3
b = 22.0

print(a*b)
print(type(b))
print(type(2/3))
28.6
<class 'float'>
<class 'float'>

String#

Strings are chains of characters and can be created by using single or double quotes. There are several operations that can be performed on strings (for example attaching them to each other by using +).

a = "This is a "
b = "string"

print(a+b)
print(type(a))
This is a string
<class 'str'>

Boolean#

Booleans are logical values. They can be either True or False. Using them, we can perform logical operations. For example check if a number is smaller or greater than another number. We will explore them more when dealing with conditions.

a = True
b = False

print(a)
print(type(b))
True
<class 'bool'>

List#

Lists are a collection of items. They can be of different types and can be changed. They are created by using square brackets. To access an item by its index, we use square brackets as well. IMPORTANT: In Python our index starts at 0. You can also use negative indices to access the list from the end. Strings can be accessed in the same way.

a = [1,2,3,4,5]
b = ["a","b","c","d","e"]

c = a+b

print(a)
print(type(b))
print(c)
print(c[0]) # first element
print(c[-1]) # last element
[1, 2, 3, 4, 5]
<class 'list'>
[1, 2, 3, 4, 5, 'a', 'b', 'c', 'd', 'e']
1
e

Do you notice something about indexing from the end? For example to get the second to last element of a list, we can use c[-2]. In MATLAB we would use c(end-1). But to get the second element of a list, we would use c[1] in Python and c(2) in MATLAB.

Getting a number of elements from a list is called slicing. We can use the colon operator to get a range of elements.

print(c[2:5]) # elements from index 2 to 4
print(c[2:]) # elements from index 2 to the end
print(c[:1]) # elements from the beginning to index 1
[3, 4, 5]
[3, 4, 5, 'a', 'b', 'c', 'd', 'e']
[1]

To get the length of a list, we can use the len() function.

print(len(c))
10

Lists can be changed by assigning a new value to an index.

a[0] = 6

print(a)
[6, 2, 3, 4, 5]

Lists can be extended by using the append() function. This function adds an item to the end of the list.

a = [1,2,3,4,5]

a.append(6)

print(a)
[1, 2, 3, 4, 5, 6]

Imagine we want to add a value to a specific index without deleting the value that is already there. We can use the insert() function.

a.insert(2, 7) # insert 7 at index 2

print(a)
[1, 2, 7, 3, 4, 5, 6]

Tuple#

Tuples are similar to lists but they cannot be changed. They are created by using round brackets.

a = (1,2,3,4,5)

print(a)
print(type(a))
(1, 2, 3, 4, 5)
<class 'tuple'>

If we try to change a value in a tuple, we will get an error. Try it out!

Dictionary#

Dictionaries are a collection of key-value pairs. They are created by using curly brackets. We can use the key to access the corresponding value. To access the value, we use square brackets. The type of the value can be anything.

a = {"name":"Alex", "age":30, "city":"Koblenz"}

print(a)
print(a["name"])
print(type(a))
{'name': 'Alex', 'age': 30, 'city': 'Koblenz'}
Alex
<class 'dict'>

Of course the type of the value in the dictionary stays the same.

print(type(a["age"]))
<class 'int'>

We can also change the value of a key.

a["age"] = 31

Or add a new key by using the update() function.

a.update({"hobby": "programming"})

print(a)
{'name': 'Alex', 'age': 31, 'city': 'Koblenz', 'hobby': 'programming'}

Something that will come in handy later on is that we can store lists in dictionaries.

data = {"vpID": [1, 2, 3, 4], "age": [30, 30, 21, 19], "mean_RT": [0.5, 0.6, 0.7, 0.8]}

print(data)
{'vpID': [1, 2, 3, 4], 'age': [30, 30, 21, 19], 'mean_RT': [0.5, 0.6, 0.7, 0.8]}

Type Conversion#

This is your task! Find out how to convert between the different types. Google is your friend!

Can you imagine a use case for this?