Killer Web Development

by Marco Laspe

6.4 Exercises

This time we will have a few more exercises, they will help you to check if you understood everything discussed in this chapter. Use the web2py console for all exercises.

  1. Create 3 variables, assign numeric values and strings; display them on the screen with and without the print statement.

  2. What will be the output of the following Python statement:

    print 4 - 2 * 2
    >>> print 9 / 3
    >>> print 9 % 3
    >>> print 10 % 3
    >>> print 10 ** 4
    >>> print 10 ** 2 * 2
    Try to guess the result and type it in you shell to prove your guess.

  3. Play around with integers and decimals. What happens if you divide an integer with a decimal? How do the results of exercise 2 change if you use decimals instead of integers.

  4. Create a while loop that counts from 1 to 100 and prints out every number in a row.

  5. Create a list with the first names of your family. What data type is best used for the names?

  6. Print out every name in this list with a loop. Which loop type do you choose? Why?

  7. Create a loop that checks if any of your family is called "John". Hint: You must loop through the list and check one element at a time.

  8. Create a dictionary, with the names of your relatives as values and your relationship to them as keys; print out its content. Which Python objects can you use as keys and which as values?

  9. You can also loop through a dictionary; actually you loop through the keys of a dictionary:

    for key in dic:
    print key # prints out the key

    Create a for loop, where you print out all keys of the dictionary you creates in exercise 8 and its corresponding values. The output should look like this:

    Mother: Penny
    Father: John

    The order is not important. This is a tough question; so take your time and don't give up.

Books often read by web2py and Python experts:


Leave a Reply

Required fields are marked *.