This page was generated from `source/notebooks/L1/3_datatypes.ipynb`_.
Data Types in Python¶
It’s time to look at different data types we may find useful in our course. Besides the number types mentioned previously, there are also other types like strings, lists, tuples and dictionaries.
Each of these data types has a number of connected methods
(functions) which allow to manipulate the data contained in a variable. If you want to know which methods are available for a certain object use the command dir
, e.g.
s="string"
dir(s)
The following few cells will give you a short introduction into each type.
Strings¶
Strings are lists of keyboard characters as well as other characters not on your keyboard. They are useful for printing results on the screen, during reading and writing of data.
[1]:
s='Hello' # string variable
s
[1]:
'Hello'
[2]:
t="world!"
String can be concatenated using the + operator.
[3]:
c=s+' '+t
[4]:
print(c)
Hello world!
As strings are lists, each character in a string can be accessed by addressing the position in the string (see Lists section)
[5]:
c[1]
[5]:
'e'
Strings can also be made out of numbers.
[6]:
"975"+"321"
[6]:
'975321'
If you want to obtain a number of a string, you can use what is known as type casting. Using type casting you may convert the string or any other data type into a different type if this is possible. To find out if a string is a pure number you may use the str.isnumeric
method. For the above string, we may want to do a conversion to the type int by typing:
[7]:
("975"+"321").isnumeric() # or you may use as well str.isnumeric("975"+"321")
[7]:
True
[8]:
int("975"+"321")
[8]:
975321
There are a number of methods connected to the string data type. Usually the relate to formatting or finding sub-strings. Formatting will be a topic in our next lecture. Here we just refer to one simple find example.
[9]:
t.find('ld') ## returns the index at which the sub string 'ld' starts in t
[9]:
3
[12]:
t.capitalize()
[12]:
'World!'
Lists¶
Lists have a variety of uses. They are useful, for example, in various bookkeeping tasks that arise in computer programming. Like arrays, they are sometimes used to store data. However, lists do not have the specialized properties and tools that make arrays so powerful for scientific computing. So in general, we prefer arrays to lists for working with scientific data. For other tasks, lists work just fine and can even be preferable to arrays.
[13]:
a = [0, 1, 1, 2, 3, 5, 8, 13]
[14]:
b = [5., "girl", 2+0j, "horse", 21]
Individual elements in a list can be accessed by the variable name and the number (index) of the list element put in square brackets. Note that the index for the elements start at 0 for the first element (left).
Note Indices in Python
The first element of a list or array is accessed by the index 0. If the array has N elements, the last entries index is N-1.
[17]:
b[0],b[1]
[17]:
(5.0, 'girl')
Elements may be also accessed from the back by nagative indices. b[-1] denotes the last element in the list and b[-2], the element before the last.
[19]:
b[-1]
[19]:
21
[20]:
b[-2]
[20]:
'horse'
The length of a list can be obtained by the len
command if you need the number of elements in the list for your calculations.
[21]:
len(b)
[21]:
5
There are powerful ways to iterate through a list and also through arrays in form of iterator. This is called list comprehension. We will talk about them later in more detail. Here is an example, which shows the powerful options you have in Python.
[25]:
[element for element in b if type(element)==str]
[25]:
['girl', 'horse']
Individual elements in a list can be replaced by assigning a new value to them
[26]:
b[-2]='cat'
[27]:
b
[27]:
[5.0, 'girl', (2+0j), 'cat', 21]
Lists can be concatanated by the + operator
[28]:
c=a+b
c
[28]:
[0, 1, 1, 2, 3, 5, 8, 13, 5.0, 'girl', (2+0j), 'cat', 21]
A very useful feature for lists in python is the slicing of lists. Slicing means, that we access only a range of elements in the list, i.e. element 3 to 7. This is done by inserting the starting and the ending element number separated by a colon (:) in the square brackets. The index numbers can be positive or negative again.
[30]:
c[3:7]
[30]:
[2, 3, 5, 8]
Inserting a second colon behind the ending element index together with a thrid number allows even to select only ever second or third element from a list.
[33]:
c[3:9:2]
[33]:
[2, 5, 13]
It is sometimes also useful to reverse a list. This can be easily done with the reverse command.
[34]:
c.reverse()
c
[34]:
[21, 'cat', (2+0j), 'girl', 5.0, 13, 8, 5, 3, 2, 1, 1, 0]
Lists may be created in different ways. An empty list can be created by assigning emtpy square brackets to a variable name. You can append elements to the list with the help of the append command which has to be added to the variable name as shown below. This way of adding a particular function, which is part of a certain variable class is part of object oriented programming.
[35]:
a=[]
[37]:
a
[37]:
['h']
A list of numbers can be easily created by the range()
command.
[39]:
list(range(10))
[39]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[40]:
list(range(3,10))
[40]:
[3, 4, 5, 6, 7, 8, 9]
[41]:
list(range(3,10,2))
[41]:
[3, 5, 7, 9]
Lists (and also tuples below) can be multidimensional as well, i.e. for an image. The individual elements may then be addressed by supplying two indices in two square brackets.
[42]:
a = [[3, 9], [8, 5], [11, 1]]
[43]:
a[1]
[43]:
[8, 5]
[45]:
a[1][1]
[45]:
5
Tuples¶
Tuples are also list, but immutable. That means, if a tuple has been once defined, it cannot be changed. Try to change an element to see the result.
[46]:
point = (10, 20)
print(point, type(point))
(10, 20) <class 'tuple'>
Tuples may be unpacked, e.g. its values may be assigned to normal variables in the following way
[47]:
x, y = point
print("x =", x)
print("y =", y)
x = 10
y = 20
Dictionaries¶
Dictionaries are like lists, but the elements of dictionaries are accessed in a different way than for lists. The elements of lists and arrays are numbered consecutively, and to access an element of a list or an array, you simply refer to the number corresponding to its position in the sequence. The elements of dictionaries are accessed by “keys”, which can be either strings or (arbitrary) integers (in no particular order). Dictionaries are an important part of core Python. However, we do not make much use of them in this introduction to scientific Python, so our discussion of them is limited.
[48]:
room = {"Ralf":422, "Frank":322, "Dekan":550}
[50]:
room['Frank']
[50]:
322
[51]:
room.keys()
[51]:
dict_keys(['Ralf', 'Frank', 'Dekan'])
[52]:
room.values()
[52]:
dict_values([422, 322, 550])