import randomBack to Basics: Refresh Your Python Skills
Is the yearly dilemma back again? The new academic year starts and we’ve forgotten the basics of a skill that felt natural before. Has this happened with your Python programming? No problem! In this session, we’ll refresh your Python skills and give you a good start to the academic year. We cover the technical basics (basic syntax, how to run scripts, and how to install and load packages) and core Python concepts (data types, lists, sets, dictionaries, basic operations, control flow, loops) to help you start working with data.
The technical basics
Remembering the basics might take a while after holidays, especially of you know how to program in other programming languages as well. Let’s first rehearse how to run scripts and install and load packages:
Running scripts
To run a script, you can either use the command line or the “play” button in your IDE. If you prefer to use the IDE, you can refer to our IDE Guide. If you want to use the command line instead, you can run your code using the python command and the name of the file (e.g., python myscript.py).
Install and load packages
Depending on your IDE, installing and loading packages can be managed with in-built package overviews (see our IDE Guide. If you do not want to rely on your IDE, you can install a package using python3 -m pip install PACKAGE in the console. After installing a package, you need to import it in your Python script. You can do this using import PACKAGE in the first line of your script.
Core concepts
Data types
In general, we differentiate between four types of data: Integer, Float, Boolean, and String.
- An integer is a number without a decimal point.
- A float is a floating-point number, meaning it has a decimal place.
- A boolean has two possible values: true and false. It is used for logical operations.
- A string is a sequence of characters, primarily used for handling text. A string is indicated by quotation marks.
We can use the built-in function type() to check the type of objects.
type(1)int
type(2.5)float
type(True)bool
type("Hello world!")str
Variables
Variables allow us to bind a name to an object, meaning we can associate names with our values. This can be done with all types.
# Store a text in the variable txt
txt = "This is a text."
print(txt)This is a text.
Lists
Lists offer a lot of flexibility. They can store data of any type and even multiple instances of the same value. To create a list, we can either use square brackets or the list() method or split a string using split().
participated = list()
print(participated)[]
# If we create a new list, we can also directly fill it with items
participated = ["participant_001", "participant_002", "participant_005"]
print(participated)['participant_001', 'participant_002', 'participant_005']
# You can also use split() to create a list
list_part = "peter,anna,katrin,sara,max".split(',')
print(list_part)['peter', 'anna', 'katrin', 'sara', 'max']
A very handy feature is that you can select individual items from a list using their index – the number assigned to them in the sequence of items. To do this, you use square brackets. In Python, the first element has the index number 0.
first = participated[0]
print(first)participant_001
Tuples
Another collection data type is tuples. The biggest difference between lists and tuples is that tuple elements cannot be changed.
participated = ["participant_001", "participant_005", "participant_002"]
# if we want to transform a list into a tuple or create a new tuple, we can use tuple()
part_tuple = tuple(participated)
print(part_tuple)('participant_001', 'participant_005', 'participant_002')
Sets
All values in a set are unique and unordered. Unlike other collection types, sets do not support indexing. You may have encountered sets in your high school math class.
# To look at the intersection of two sets, you can either use the ampersand & or a.intersection(b) (with a one set, and b the other)
female = {'Sue','Mary','Kate','Lee'}
male = set(['Lee','Eric','Tom','Adam'])
print(female & male){'Lee'}
# To look at the union of two sets, you can either use | or a.union(b) (with a one set, and b the other)
print(female | male){'Mary', 'Eric', 'Tom', 'Kate', 'Lee', 'Sue', 'Adam'}
# To look at the difference between the two sets, you can either use - or a.difference(b) (with a one set, and b the other)
print(female - male){'Sue', 'Mary', 'Kate'}
Dictionaries
Dictionaries function like a real dictionary, meaning they have keys and values. Using this type, we can build the following structure:
participant_name: Peter participant_age: 50 participant_city: Rotterdam
In this dictionary, participant_name, participant_age, and participant_city are the keys, while Peter, 50, and Rotterdam are the values. To create a dictionary, we use curly brackets.
data = {"participant_name": "Peter", "participant_age": 50, "participant_city": "Rotterdam"}
print(data){'participant_name': 'Peter', 'participant_age': 50, 'participant_city': 'Rotterdam'}
{'participant_name': 'Peter', 'participant_age': 50, 'participant_city': 'Rotterdam'}
# To retrieve the values saved in a dictionary, you can use the keys
print(data["participant_age"])50
Basic operations
Control flow – if, else, and elif
In real-life scenarios, you may want to exclude certain participants or apply different tests based on their attributes. For example, you could say:
If the age is higher than 65, print ‘senior’. Else if the age is lower than 18, print ‘junior’. Else, print ‘adult’.
We can implement similar logic in Python using if, elif, and else statements.
age = 10
if age > 65:
print('senior')
elif age < 18:
print('junior')
else:
print('adult')junior
Loops
Typically, we want to perform actions multiple times, which means we need to repeat them. For this purpose, we use loops.
We differentiate between two types of loops: while and for. - In a while loop, an action is performed as long as a certain condition is True. - In a for loop, an action is repeated a preset number of times.
while
ages = [11,12,13,14,18,25,26,32,45,50,57,61,65,70,78]
no_iteration = 0
while no_iteration < len(ages):
if ages[no_iteration] > 65:
print('senior')
elif ages[no_iteration] < 18:
print('junior')
else:
print('adult')
no_iteration += 1junior
junior
junior
junior
adult
adult
adult
adult
adult
adult
adult
adult
adult
senior
senior
for
Another approach to iterating over items in a list or dictionary is the for loop. It repeats an action for a specified number of times. In prose, this would be: For every age in the list of ages, print the appropriate text.
ages = [11,12,13,14,18,25,26,32,45,50,57,61,65,70,78]
for age in ages:
if age > 65:
print('senior')
elif age < 18:
print('junior')
else:
print('adult')junior
junior
junior
junior
adult
adult
adult
adult
adult
adult
adult
adult
adult
senior
senior
Functions
If we write snippets of code that we want to reuse in the future, it can be helpful to use functions. Functions are typically defined once and serve to group related functionality. They can be called (used) multiple times throughout your code. Additionally, functions have their own local variables, which are separate from the rest of the program.
To define a function, we use def.
# Define function say_hello()
def say_hello():
print("Hello, how are you?")
say_hello()Hello, how are you?
Work with data
Write a file
There are multiple packages/modules available to create and read files (e.g., the csv module and json module). However, you can also create files without using modules by making use of the function open() in the writing mode w.
with open('test.txt', mode = 'w') as file_out:
print("This is a test!", file = file_out)Read a file
Similar to writing files, you can also use the csv and json modules to read files. open() in the reading mode r also allows us to read the content.
with open('test.txt', mode = 'r') as file_in:
content = file_in.read()
print(content)This is a test!
Basic pandas functions
Pandas is a fast, powerful, flexible, and user-friendly open-source tool for data analysis and manipulation, built on top of the Python programming language.
With pandas, we can read files, process them, and perform statistical analyses on the data.
After importing pandas, we can use the function read_csv() to read our list of participants. We give the name of the csv file and the data type (in this case: string) as arguments.
import pandas
dtype = {"name":str, "age":int, "group":str, "temperature":int}
data = pandas.read_csv("material/example.csv", dtype = dtype)
print(data) name age group temperature
0 Peter 11 junior 37
1 Alex 25 adult 41
2 Sandra 43 adult 35
3 Eva 55 adult 38
4 Adam 66 senior 39
5 Marijn 7 junior 41
6 Indy 78 senior 38
7 Ruth 63 adult 35
8 Dani 69 senior 37
9 Jentje 63 adult 41
10 Indy-Senna 20 adult 36
11 Ruth-Marijn 14 junior 38
12 Bo 69 senior 36
13 Willy 20 adult 36
14 Luca 20 adult 38
15 Jaimy 12 junior 41
16 Henny 13 junior 41
17 Nicky 57 adult 38
18 Bo-Jos 63 adult 41
19 Sam 13 junior 37
20 Eliza 78 senior 35
21 Jos-Dominique 20 adult 36
22 Marijn-Jamie 12 junior 41
23 Ali 63 adult 36
24 Jamie-Dominique 54 adult 39
25 Luca-Indy 1 junior 40
26 Jos-Dani 14 junior 38
27 Indy-Dani 20 adult 36
28 Luca-Jentje 12 junior 39
29 Jamie 54 adult 40
30 Robin 1 junior 40
31 Indy-Ali 59 adult 38
32 Sam-Indy 78 senior 35
33 Willy-Jamie 57 adult 38
34 Dani-Indy 78 senior 41
35 Jos 45 adult 35
36 Ruth-Jentje 9 junior 36
37 Senna-Jamie 14 junior 35
38 Henny-Dani 59 adult 38
39 Jaimy-Henny 54 adult 40
40 Jamie-Ali 63 adult 40
41 Dominique 12 junior 37
42 Jaimy-Sam 12 junior 40
43 Robin-Jaimy 69 senior 37
After importing your data, you can use pandas to get insights. describe() gives you some statistics.
print(data.describe())
# This also works when limiting us to one group
print("\n",data.group.describe()) age temperature
count 44.000000 44.000000
mean 39.068182 38.045455
std 26.100341 2.079295
min 1.000000 35.000000
25% 13.000000 36.000000
50% 44.000000 38.000000
75% 63.000000 40.000000
max 78.000000 41.000000
count 44
unique 3
top adult
freq 21
Name: group, dtype: object
While describe() gives us an overview, we can also assess the information separately.
print("mean:", data.temperature.mean())
print("median:", data.temperature.median())
print("standard deviation:", data.temperature.std())
print("minimum:", data.temperature.min())
print("maximum:", data.temperature.max())mean: 38.04545454545455
median: 38.0
standard deviation: 2.079294887803069
minimum: 35
maximum: 41
We can also group our results and look at it while keeping categorical in mind.
age_groups = data.groupby("group")
age_groups.describe()| age | temperature | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| count | mean | std | min | 25% | 50% | 75% | max | count | mean | std | min | 25% | 50% | 75% | max | |
| group | ||||||||||||||||
| adult | 21.0 | 46.523810 | 17.497483 | 20.0 | 25.0 | 54.0 | 59.0 | 63.0 | 21.0 | 37.857143 | 2.080522 | 35.0 | 36.00 | 38.0 | 40.00 | 41.0 |
| junior | 15.0 | 10.466667 | 4.273952 | 1.0 | 10.0 | 12.0 | 13.0 | 14.0 | 15.0 | 38.733333 | 2.016598 | 35.0 | 37.00 | 39.0 | 40.50 | 41.0 |
| senior | 8.0 | 73.125000 | 5.303301 | 66.0 | 69.0 | 73.5 | 78.0 | 78.0 | 8.0 | 37.250000 | 2.052873 | 35.0 | 35.75 | 37.0 | 38.25 | 41.0 |
We can also check whether we have any double-entries of full lines…
data.value_counts()name age group temperature
Adam 66 senior 39 1
Alex 25 adult 41 1
Jos-Dani 14 junior 38 1
Jos-Dominique 20 adult 36 1
Luca 20 adult 38 1
Luca-Indy 1 junior 40 1
Luca-Jentje 12 junior 39 1
Marijn 7 junior 41 1
Marijn-Jamie 12 junior 41 1
Nicky 57 adult 38 1
Peter 11 junior 37 1
Robin 1 junior 40 1
Robin-Jaimy 69 senior 37 1
Ruth 63 adult 35 1
Ruth-Jentje 9 junior 36 1
Ruth-Marijn 14 junior 38 1
Sam 13 junior 37 1
Sam-Indy 78 senior 35 1
Sandra 43 adult 35 1
Senna-Jamie 14 junior 35 1
Willy 20 adult 36 1
Jos 45 adult 35 1
Jentje 63 adult 41 1
Jamie-Dominique 54 adult 39 1
Henny 13 junior 41 1
Ali 63 adult 36 1
Bo 69 senior 36 1
Bo-Jos 63 adult 41 1
Dani 69 senior 37 1
Dani-Indy 78 senior 41 1
Dominique 12 junior 37 1
Eliza 78 senior 35 1
Eva 55 adult 38 1
Henny-Dani 59 adult 38 1
Jamie-Ali 63 adult 40 1
Indy 78 senior 38 1
Indy-Ali 59 adult 38 1
Indy-Dani 20 adult 36 1
Indy-Senna 20 adult 36 1
Jaimy 12 junior 41 1
Jaimy-Henny 54 adult 40 1
Jaimy-Sam 12 junior 40 1
Jamie 54 adult 40 1
Willy-Jamie 57 adult 38 1
Name: count, dtype: int64
…and how often certain combinations (or categories) are present.
data.group.value_counts()group
adult 21
junior 15
senior 8
Name: count, dtype: int64
Pandas also offers other functionalities, a few are documented in the Python course overview here.