Teach Your Kids To Code: A Parent-friendly Guide to Python Programming

(vip2019) #1

34 Chapter 3


Figure 3-1: A program with four variables and the output it creates

Numbers and Math in Python


The computer is great at remembering values. We can use the
same variable hundreds or thousands of times in the same pro-
gram, and the computer will always give us the right value as
long as we’ve programmed it correctly. Computers are also great
at performing calculations (addition, subtraction, and so on). Your
computer is able to perform over one billion (1,000,000,000, or a
thousand million) calculations every second!
That’s much faster than we can compute numbers in our
heads; although we’re still better than computers at some tasks,
fast math is a contest the computer will win every time. Python
gives you access to that mathematical computing power with two
main types of numbers, and it also lets you use a whole set of sym-
bols to do math with those numbers, from + to - and beyond.

Python Numbers


The two primary types of numbers in Python are called integers
(whole numbers, including negatives, like 7 , -9, or 0 ) and floating-
point numbers (numbers with decimals, like 1.0, 2.5, 0.999, or
3.14159265). There are two additional number types that we won’t
use much in this book. The first is Booleans, which hold true
or false values (sort of like the answers on a “true or false” test
at school), and the second is complex numbers, which hold even
imaginary number values (this might excite you if you know some
advanced algebra, but we’re keeping it real here—pun intended).
Integers, or whole numbers, are useful for counting (our vari-
able x in Chapter 2 counted the number of lines as we drew the
spiral) and for basic math (2 + 2 = 4). We usually state our age in
whole numbers, so when you say you’re 5 or 16 or 42, you’re using
an integer. When you count to 10, you’re using integers.

http://www.allitebooks.com
Free download pdf