O'Reilly logo

Python Projects by Alan Gauld, Laura Cassell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

A Answers to Exercises

Chapter 1 Solutions

  1. How do you convert between the different Python data types? What data quality issues arise when converting data types?

    You convert between types using the type functions. Thus, to convert a float or string to an integer, you use the int() type function. To convert an object to a string, you use the str() type function. And so on.

    When making the conversion, it is possible that you might lose some data in the process. For example, converting a floating-point number to an integer loses the decimal part of the number (for example, int(2.3) results in 2). If it’s important to retain the detail, you must retain a copy of the original as well.

  2. Which of the Python collection types can be used as the key in a dictionary? Which of the Python data types can be used as a value in a dictionary?

    Dictionary keys must be immutable. That means that of the basic Python types, integers, booleans, floats, strings, and tuples can all be used as keys (although floats are not recommended due to their imprecision, especially if you will be computing the key value rather than just storing it). Other custom types, such as frozenset, can also be used as keys provided they are immutable.

    Dictionary values can be of any type regardless of mutability.

  3. Write an example program using an if/elif chain involving at least four different selection expressions.

    You could use any number of choices here. This example uses colors. Your if/elif/else code should look ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required