1 Numeric types

 

In Python, we have 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).

It’s hard to describe how often programs will use numbers. Whether you’re calculating salaries, bank interest, or cellular frequencies, it’s hard to imagine a program that doesn’t use numbers in some way or another.

Numbers are not only fundamental to programming, but they 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. While working with numbers is fairly basic and straightforward, the conversions between them, and the integration with other data types, can sometimes take some time to get used to.

1.1  Useful references

1.2  Number guessing game

1.2.1  Solution

1.2.2  Discussion

1.2.3  Beyond the exercise

1.3  Summing numbers

1.3.1  Solution

1.3.2  Discussion

1.3.3  Beyond the exercise

1.4  Run timing

1.4.1  Solution

1.4.2  Discussion

1.4.3  Beyond the exercise

1.5  Hexadecimal output

1.5.1  Solution

1.5.2  Discussion

1.5.3  Beyond the exercise

1.6  Summary