Exercises

The purpose of the exercises here, and throughout the book, is to encourage you to try out Python, and from Part II onward, PyQt, to get some hands-on experience. The exercises are designed to require as little typing as possible, and they are graded from least to most challenging.

The exercises for this chapter can all be tried out directly in IDLE; from Chapter 2 onward, they are slightly longer and will need to be typed into files, as we will explain.

1. Run IDLE, and type in the following:

two = dict(india=9, golf=17, juliet=5, foxtrot=61, hotel=8) three = {11: "lima", 13: "kilo", 12: "mike"}

Try to predict what the len(), max(), min(), and sum() functions will produce for each of the three collections, and then apply the functions and see the results. Do they do what you expected?

2. Continuing in IDLE, assign a dictionary's keys to two variables, and then change one of them like this:

d = dict(november=11, oscar=12, papa=13, quebec=14) v1 = v2 = d.keys()

v1, v2 # This will show the contents of the lists v1[3] = "X"

After this, do you expect v1 and v2 to be the same or different? Why? Print out v1 and v2 to see. Now try assigning to v1 and v2 separately, and again change one:

Will v1 and v2 be the same as before? Print them out to see. If any of this is mysterious, try rereading the sidebar on page 34.

3. In the documentation, string-related methods and functions are covered in several places—find and read or skim these pages: "Sequence types", "String methods", "String formatting operations", and "String constants".

If you are comfortable with regular expressions, also look at the "Regular expression operations" pages.

Still in IDLE, create two floating-point values:

Based on your reading of the string-formatting documentation, create a single format string that when used with the % operator will produce the string < -34.81> when applied to f and <+723.13> when applied to g.

Solutions to the exercises, and all the source code for the examples, is available online from the author's Web site at http://www.qtrac.eu/pyqtbook.html. In the pyqtbook.zip file (and in the pyqtbook.tar.gz file) there are subdirectories, chap01, chap02, and so on, and in these are the relevant examples and answers. This chapter's answers are in chap01/answers.txt.

This page intentionally left blank

• Conditional Branching

Was this article helpful?

0 0
Tube Jacker

Tube Jacker

Download Tube Jacker And Discover Everything You Need To Know About Jacking Unlimited Traffic From The Video Giant. The drop dead easy way to create winning video campaigns that will FLOOD your website with unstoppable FREE traffic, all on complete and total autopilot. How to exploit a sneaky method of boosting exposure and getting your videos to the top of Google within 72 hours, guaranteed.

Get My Free Ebook


Post a comment