## Exercise 5: For Loops

Programs sometimes need to repeat actions. To repeat actions we can use a for loop.
A for loop is written inside the code. A for loop can have 1 or more instructions.

These instructions (loop) is repeated until a condition is met.

### Exercise

In the exercise below we will repeat actions on every item of a list. The first loop will repeat the print functionfor every item of the list. The second loop will do a calculation on every element of the list num and print the result.

Type the code below and run the program.

## Study drill

Try the exercise below:

1. Make a program that lists the countries in the set

## Exercise 6: While loop

A while loop repeats code until the condition is met. Unlike for loops, the number of iterations in it may be unknown.  A while loop always consists of a condition and a block of code.

A while loop ends if and only if the condition is true, in contrast to a for loop that always has a finite countable number of steps.

Related course: Complete Python Bootcamp: Go from zero to hero in Python

### Definition

The while loop below defines the condition (x < 10) and repeats the instructions until that condition is true. Type this code:

Executes the code below until the condition x < 10 is met.   Unlike a for loop, the iterator i is increased in the loop.

### Study drill

Try the exercise below:

1. Make a program that lists the countries in the set below using a while loop.

## Exercise 7: Functions

To group sets of code you can use functions. Functions are small parts of repeatable code.
A function accepts parameters.

Without functions we only have a long list of instructions. Functions can help you organize code. Functions can also be reused, often they are included in modules.

Related course: Complete Python Bootcamp: Go from zero to hero in Python

### Parameters

In the example below we have parameter x and y. Type this program and save it as summation.py

In this example we have two functions: f(x,y) and print(). The function f(x,y) passed its output to the print function using the return keyword.

Output can also be stored into a variable:

## Study drill

Try the exercises below

After completing these continue with the next exercise.

## Exercise 8: Lists

In Python you define a list using brackets.  An empty list would simply be list = [].  A list can have any number of elements. To access a list  add the id between the brackets, such as list[0], list[1] and so on.

Type the code below and run it:

## Exercise 9: Dictionary

Python supports a data structure known as dictionary:  a set which contains key, value pairs. For every key in a dictionary,  there is a value. Unlike lists, dictionaries do not have an specific order.

Type the code below and run it:

In Python you reading files is part of the standard library.  To read files, you can use the readlines() function. This will read a file line by line and store it into a list:

Type the code below, save it as file.py and run it.

Alternative:
You may not always want to read a file line by line. Take for example, if your file does not have newlines or is a binary file. To read a file and store into a string, use the read() function instead:

## Exercise 11: Write file

Creating new files
To create new files, you can use this code:

The ‘\n’ character adds  a new line. If the file already exists, it is replaced. If you use the “w” parameter, the existing contents of the file will be deleted.

Appending to files
To add text to the end of a file, use the “a” parameter.

## Exercise 13: Nested loops

A loop can contain one or more other loops. This principle is known as nested loops.

Lets do a simple example. If we have a list of persons who like to eat at restaurants, can we make
every one of them eat a certain restaurant?

### Study drill

Try the exercises below

1. Given a tic-tac-toe board of 3×3, print every position

2. Create a program where every person meets the other
persons = [ “John”, “Marissa”, “Pete”, “Dayton” ]

3. If a normal for loop finishes in n steps O(n), how many steps has a nested loop?

After completing these continue with the next exercise.

## Exercise 14: Slices

A slice can be taken from a string or list, just as you can take a slice from a pizza.
Python starts numbering of string and list elements from zero.

Slice Example:

### Study drill

Try the exercises below

1. Take a slice of the list below:
pizzas = [“Hawai”,”Pepperoni”,”Fromaggi”,”Napolitana”,”Diavoli”]

2. Given the text “Hello World”, take the slice “World”

After completing these continue with the next exercise.

## Exercise 15: Multiple return

Python functions can return multiple variables. These variables can be stored in variables directly. A function is not required to return a variable, it can return zero, one, two or more variables.

This is a unique property of Python, other programming languages such as C++ or Java do not support this by default.

Example:

### Study drill

Try the exercises below:

1. Create a function that returns a,b and a+b

After completing these continue with the next exercise.