Understand basic data types in Python (num, string)
To get the data type of any Python variable, we use the type()
function.
x = 10
datatypeofx = type(x)
print(datatypeofx)
To set the data type of any Python variable, we do not need to use an explicit function. Python is commonly termed a dynamically typed language. What this implies is that the data type of the variable is not set during variable assignment, but rather determined when an operation is performed on the variable.
For example, try to analyse the following code snippet.
x = 'Hello World'
y = ', how u doin?'
z = x + y
print(z)
print('Data type of z: ', type(z))
Now, try this out:
x = 4
y = 5
z = x + y
print(z)
print('Data type of z: ', type(z))
As you will notice, Python automatically detects the data types of x
and y
, which further determines the data type of z
. Now, what do you expect to observe when you run the following code snippet?
x = 4
y = ', how u doin?'
z = x + y
print(z)
print('Data type of z: ', type(z))
Note: if you do want to explicitly set the data types of certain variables, perhaps to ensure that they never change, you can use what are known as constructor functions.
To learn more about Python strings and operations that can be performed on them, please watch the following video.
Numbers can be represented in Python either as an int
, float
or complex
. Further, several operations can be performed on them as shown in the table below.
To learn more about Python data types, in particular numeric data types and the operations that can be performed on them, please watch the following video.
There are two basic sequence types: lists and tuples. A Python list is used to store multiple items in a single variable. A tuple is similar to a python list, but a key difference is that a list can be modified once created, but a tuple can't. In Pythonic terms, a list is mutable, whereas a tuple is inmutable.
There are also slight differences in how they are declared and assigned.
cities_list = ["Breda", "Tilburg", "Amsterdam", "Rotterdam", "Groningen"]
print(cities_list)
cities_tuple = ("Breda", "Tilburg", "Amsterdam", "Rotterdam", "Groningen")
print(cities_tuple)
Please create your own lists and observe what happends when you apply the following list and tuple operations on your lists.
Remember that Python is dynamically typed.
One of the coolest features about Python and it's lists are list comprehensions! In general there is a very common for loop structure that looks like this:
new_list = []
for x in y:
new_element = transform(x)
new_list.append(new_element)
List comprehension rearranges the for loop (minus the append) in a single line that makes your code more readable and "pythonic".
new_list = [transform(x) for x in y]
Please watch the following video to understand list comprehensions.
A Python dictionary is a data type which is a collection of variables. A simple example of a dictionary data type is your phone contacts. It contains a a key which is your contacts' name, and values such as surname, phone number, address, birthday etc. Dictionaries in Python are also represented using key:value
pairs.
A pair of braces creates an empty dictionary: {}. Placing a comma-separated list of key:value
pairs within the braces adds initial key:value
pairs to the dictionary; this is also the way dictionaries are written on output.
For example, one can represent a contact list in Python using the following syntax.
my_contacts = {"Jan": {"name": "Jan Peters Jones", "address": "2 Breda", "phone": "0678456732"},
"Marcella": {"name": "Marcella Huizinga", "address": "31 Groningen", "phone": "08 957834321"}
}
To retrieve the details for Jan
, we run the following code
print(my_contacts['Jan'])
If we further only want to retrieve Jan's phone number, then we can further run
print(my_contacts['Jan']['phone'])
Please watch the following brief video to review concepts learned.
There is no clear distinction between a data type and a data structure in Python. For example a list is a data structure that can contain variables of multiple data types. However, we can also declare nested lists, a list containing atleast one other list as a variable. However, it is important to note that there are certain operations that you can perform on data structures that you cannot perform on data types.
1.
list_of_squares = []
for x in range(10):
list_of_squares.append(x ** 2)
2.
names = ['Mike', 'Michelle', 'Amelie', 'Ophelia', 'Indy', 'Ben', 'Ursula']
vowel_names = []
for name in names:
if name[0] in "AEIOU":
vowel_names.append(name)
Write a Python program to create a list of squares of even numbers in the range(1,100). Use list comprehension.
Please continue with the Learning Python 3
module in Code Academy which you can find here. In particular, complete the course until the topic - dictionary.
Please review the topic Data Structures in the official Python tutorial, which you can find [here] (https://docs.python.org/3/tutorial/datastructures.html)
We will recap today's concepts and discuss the assignments at the Q&A, see you at 4pm!