Exercise

Answer the Questions

  • What is the meaning of negative indices?

  • What is the meaning of c1, c2, c3 = "bla"?

  • How does arithmetic work for sequences?

  • What is the meaning of 'y' in word for some string word?

  • What is the meaning of step in a slice?

  • What is the result of s[:i] + s[i:] for some sequence s?

  • What is the length of s[n:m]?

  • Is (1) == (1,)?

  • When are tuples equal? When are they identical?

  • Can lists be an element of a list?

  • Which of the following types are mutable and which immutable: float, string, list, tuple, dictionary, complex, integer?

  • Which of the following expressions are tuples and which are lists:

    • (1, 2, 3)

    • [1, 2, 3]

    • [(1, 3), 'a', 45]

  • What is the difference between squares + [36, 49] and squares.extend([36, 49])?

  • Why is

a = [1, 2, 3]
b = a
a is b == True

but

a = [1, 2, 3]
b = [1, 2, 3]
a is b == False

Coding exercises

  • Assign the string 'Python' to the variable word and then extract the first 2 characters.

  • From the previously created variable word, print

    • the last two characters

    • every second character

    • all characters, but in reversed order.

  • Try to build the string "Gooooooooooooooooal" out of the string "Goal"

  • Create a list that contains three book titles

  • Create a list that consists of the squares of all numbers between 0 and 5. Then manipulate this list by doing the following

    1. remove the last element of the list

    2. set the second element to 2

    3. add three additional elements of your choice to the end of the list

    4. add one element to the beginning of the list

    5. print the number of elements that have a value of 0

  • Create a dictionary and fill it with content of your choice. Try out the methods available for dictionaries.

  • Assign the string "Foo" to variable s. Now try to change the first character of s to P. Explain the error message you get.