1 Numeric types

 

Whether you’re calculating salaries, bank interest, or cellular frequencies, it’s hard to imagine a program that doesn’t use numbers in one way or another. Python has three different numeric types: int, float, and complex. For most of us, it’s enough to know about (and work with) int (for whole numbers) and float (for numbers with a fractional component).

Numbers are not only fundamental to programming, but also give us a good introduction to how a programming language operates. Understanding how variable assignment and function arguments work with integers and floats will help you to reason about more complex types, such as strings, tuples, and dicts.

This chapter contains exercises that work with numbers, as inputs and as outputs. Although working with numbers can be fairly basic and straightforward, converting between them, and integrating them with other data types, can sometimes take time to get used to.

Useful references

 
 

Exercise 1 Number guessing game

 
 
 
 

Working it out

 
 
 

Solution

 
 
 

Screencast solution

 
 
 

Beyond the exercise

 
 
 
 

Exercise 2 Summing numbers

 
 

Working it out

 
 

Solution

 

Screencast solution

 
 
 
 

Beyond the exercise

 
 

Exercise 3 Run timing

 

Working it out

 
 
 
 

Solution

 

Screencast solution

 
 

Beyond the exercise

 

Exercise 4 Hexadecimal output

 
 
 

Working it out

 
 
 

Solution

 
 

Screencast solution

 
 

Beyond the exercise

 
 

Summary

 
 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage