2.4 Exercises#

Exercise 1: Variables#

  1. Assign the value 42 to a variable named my_var and print it.

  2. Assign the string "Hello, World!" to a variable named greeting and print it.

  3. Create two variables, a and b, assign them values 10 and 20, and save their sum to a variable called c. Print their sum.

  4. Write a comment for (3) explaining what your “algorithm” does.

# Exercise 1

Exercise 2: Types#

The Python built-in type() will return the type of the variable given to it.

  1. Create a variable x and assign it a float value. Check the type of x using the type() function.

  2. Create a variable y and assign it a string value. Print the type of y and its length using the len() function.

  3. Verify that a multiplication of two integers returns another int, while a division returns a float.

  4. Why do do you think that Python changes the result of a division into type float?

# Exercise 2

Exercise 3: Strings#

You previously learned about functions like .lower() or .replace() which allow you to modify strings. But what about counting? Lets for example take the word Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch, which is a town in Wales. Can you find a function that lets you count the number of times the combination of two subsequent L’s (e.g. “ll”) appear in that word? See if you can guess how the function could be called and used. Use the internet for help if necessary.

  1. Count the number of time two subsequent L’s occur in the town name.

  2. Save the result to a new variable and print its value.

  3. The correct answer should be 5. If your result is different, check the string to see where you might have missed something.

town = "Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch"

Exercise 4: Booleans#

Some integer values are equivalent to Python Boolean values.

  1. Use the equality (==) operator to find the integer which is equivalent to True.

  2. Use the equality (==) operator to find the integer which is equivalent to False.

# Exercise 4

Exercise 5: Lists#

  1. Create a list named fruits containing "apple", "banana", and "cherry". Add "orange" to the list and print the result.

  2. Remove "banana" from the fruits list and print the updated list.

# Execise 5

Exercise 6: More lists#

You previously learned about the .append() function for adding a new item to the end of a list. You can also add two lists together by using the plus (+) operator. Given the lists provided below, can you:

  1. Create a new list which combines list1 and list2?

  2. Create a new list that has the items [6, 5, 1, 2, 3] by only using indexing operations and functions associated with lists?

Hint: Lists come with more functions like .extend() or .reverse(). You can find a full list in the Python documentation.

list1 = [1, 2, 3]
list2 = [4, 5, 6]

Exercise 7: Indexing#

You previousy learned how to access specific elements of a list by enclosing an index in square brackets (first_item = my_list[0]). A useful feature in Python is that you can also start indexing from the back of a list by using negative numbers (last_item = my_list[-1]). Experiment with this for a bit and then answer/implement the following questions:

  1. What is the negative number index for the last item in the list?

  2. What is the negative number index for the first item in the list?

  3. Can you write code that would use a negative number to index the first item in the list, without advance knowledge of its length? Hint: A function introduced in the Strings section of the might be useful here.

my_list = ["Hello", 1, 3.14, True, 5, 1234, "Goodbye"]

Exercise 8: Dictionaries#

Below you can see a dictionary containing the price of different fruits.

  1. Add another fruit to the dictionary. This fruit should have several different prices associated with it, organized as a list

  2. Access the second price in this list in a single line of code and save it to a variable called second_price

  3. Print the newly added fruit and its the second price like so: “The second price of <your_fruit> is <second_price> €”.

Hint: You can use the previously introduced f-strings for printing

fruit_prices = {
    "apple": 0.5,
    "banana": 0.2,
}

Voluntary Exercise 1: Lists and dictionaries combined#

Create a list of dictionaries where each dictionary represents a student. The keys should be "name", "age", and "grades" (a list of three integers). Then write code that prints the name of the student with the highest average grade.

# Voluntary exercise 1

Voluntary Exercise 2: Swapping values#

Can you swap the values of a and b without creating a temporary (thrird) variable?

a = 5
b = 10