Computer programs work with many different kinds of data, such as text, numbers, dates, pictures, movies, and music. Numbers are perhaps the most important, because your computer understands everything in terms of numbers. You see pictures and movies, but underneath the hood, they’re just numbers to your computer.
Learning Goals
In this post, we will learn how to work with numbers in Python.
To do this, you will need to know how to create files and use Python to run them.
Each code file that you create should have a descriptive name that lets you know what it does. For this post, we’re going to do several programs with numbers. We will name them sequentially, using the convention numbers_part1.py, numbers_part2.py, numbers_part3.py, etc.
Note: We use underscores ___ in the name instead of spaces. Spaces can make it hard to work with files in some operating systems.
Number Types
There are two main types of numbers in Python. The first is an integer. These are your traditional counting (whole) numbers such as 1, 2, 3, etc.
The second type of number is called a floating point number. This is equivalent to a fractional number in math, such as 1.25.
When you are coding in Python, it will make its best guess as to the appropriate number type to use. But sometimes it helps to be explicit. Open up the interactive shell (type python at the command prompt) and try the following:
1 2 3 4 5 6 7 8 |
>>> print(1) 1 >>> print(int(1)) 1 >>> print(float(1)) 1.0 >>> print(int(1.5)) 1 |
In this way you can use int() and float() to convert numbers from integers to floating point and back. Just keep in mind that when you convert from floating point to int, Python will truncate your decimal places!
Basic Mathematical Expressions
Open a new file in Sublime text editor, and write this code:
1 |
print(5 + 6 + 7 + 8 + 9 + 10) |
Then, save your file, open a terminal and run your file in python. For example, if you saved your file as “numbers_part1.py”, run:
1 |
python numbers_part1.py |
Your program should print the output: 45
What happened? Python saw a bunch of numbers being added together and decided to act as a calculator!
We can do another example for practice. Create another new file, and write this code:
1 |
print(4.5 / 2.25) |
Save this program and run it in the terminal. For example, if you saved this file as “numbers_part2.py”, run:
1 |
python numbers_part2.py |
Your program should print the output: 2.0
The Python interpreter program works by looking at each line of code, figuring out what that line of code is instructing the computer to do, and executing the instruction. One of the things the interpreter recognizes is a mathematical expression. For now, you can think of a mathematical expression as anything you could enter into a calculator.
When Python sees a mathematical expression, it attempts to calculate the value of the expression, like a calculator. So if it sees “32 – 5” in code, it does subtraction to get the value 27.
In the examples above, we also use the word print. Print tells Python to write things to the terminal. So the way Python interprets print(4.5 / 2.25) is:
- Print the following calculation: 4.5 / 2.25.
- But first, act as a calculator to find the value of 4.5 / 2.25.
- The value is 2.0.
- So print “2.0”.
Mathematical Operators
Mathematical expressions in Python are specified using a set of predefined operators such as plus, minus, multiply and divide symbols, as well as a few others. For example, there is a ** symbol for exponents.
Function | Operator | Example | Result |
---|---|---|---|
Addition | + | 5 + 4 | 9 |
Subtraction | – | 5 – 4 | 1 |
Multiplication | * | 5 * 4 | 20 |
Division | / | 5 / 4 | 1.25 |
Power (Exponent) | ** | 5 ** 4 | 625 |
Modulo (Calculate Remainder) | % | 5 % 4 | 1 |
Create another new file, and write this code:
1 2 3 |
print(5 ** 2) print(5 ** 3) print(5 ** 4) |
Save this program and run it in the terminal. For example, if you saved this file as “numbers_part3.py”, run:
1 |
python numbers_part3.py |
Your program should print the output:
25
125
625
This program has three commands on three lines! Python will execute the first line, then the second, then the third.
In line 1, 5 ** 2 is interpreted as “5 squared”, which is 25.
In line 2, 5 ** 3 is interpreted as “5 cubed”, which is 5 * 5 * 5 = 125.
In line 3, 5 ** 4 is interpreted as “5 to the 4th power”, which is 5 * 5 * 5 *5 = 625.
Square Roots using Exponent
We can also use the ** operator to take square roots. In math, raising something to the 0.5th power is the same as taking the square root of it.
Create another new file, and write this code:
1 |
print(36 ** 0.5) |
Save this program and run it in the terminal. For example, if you saved this file as “numbers_part4.py”, run:
1 |
python numbers_part4.py |
Your program should print the output: 6.0
36 ** 0.5 is interpreted as “36 to the 0.5th power” which is the same as taking the square root of 36, which is 6.
Built In Mathematical Functions
Function | Usage | Example | Result |
---|---|---|---|
abs(x) | Absolute Value | abs(-2) | 2 |
math.floor(x) | Round Down | math.floor(7.8) | 7 |
math.ceil(x) | Round Up | math.ceil(2.2) | 3 |
pow(x, y) | Power (Exponent) | pow(5, 4) | 625 |
math.sqrt(x) | Square Root | math.sqrt(36) | 6.0 |
Notice some of these math functions have the word math in front of them. This means they are part of the Python math math module. To access these in your program, you will need to include the math module in your code.
Create another new file and try out some of these functions. Write this code:
1 2 3 4 5 |
import math print(math.floor(5.6)) print(abs(-3)) print(math.sqrt(256)) |
Save this program and run it in the terminal. For example, if you saved this file as “numbers_part5.py”, run:
1 |
python numbers_part5.py |
Your program should print the output:
5
3
16.0
Note how we had to use a bunch of parenthesis in order to use both print, int, and abs. The print, int, and abs commands are functions, and parentheses are part of the syntax (language rules) for using functions.
The floor function rounds a decimal number (like 5.6) down to an integer (like 5). Likewise, ceil rounds a decimal number up. The abs function takes any number (positive or negative) and returns the positive version, so -3 is turned into 3.
More than one way to do things
You probably noticed the sqrt and pow functions as well. Previously we calculated these by hand using the ** operator. Python has built in libraries to perform these same calculations in a more readable manner. For example, you can find the square root of 36 by typing 36 ** 0.5, but that can be awkward to look at. Instead its much nicer to see math.sqrt(36).
The math module has many other built in mathematical functions as well! You can find more information on them here.
Summary
You should have a good understanding now of integer and decimal (floating point) numbers in Python and how to convert between the two. We also covered how to calculate basic mathematical expressions, as well as utilize built in mathematical functions. We even discussed some alternative ways to accomplish the same goal!
Next time, we will explain functions more in depth, and and look at how to put together our own.