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.

1.1 Useful references

 
 

1.2 Exercise 1 ■ Number guessing game

 
 

1.2.1 Working it out

 
 
 
 

1.2.2 Solution

 
 

1.2.3 Beyond the exercise

 

1.2.4 Exercise 2 ■ Summing numbers

 
 
 
 

1.2.5 Working it out

 
 

1.2.6 Solution

 

1.2.7 Beyond the exercise

 
 
 

1.3 Exercise 3 ■ Run timing

 
 

1.3.1 Working it out

 
 

1.3.2 Solution

 
 

1.3.3 Beyond the exercise

 
 

1.4 Exercise 4 ■ Hexadecimal output

 
 
 
 

1.4.1 Working it out

 
 

1.4.2 Solution

 
 

1.4.3 Beyond the exercise

 
 
 

1.5 Summary

 
 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage