This article is Part 4 in a 14-Part Python Beginner's Tutorial Series.
- Part 1 - Python Beginner: Hello Python!
- Part 2 - Python Beginner: Variables in Python
- Part 3 - Python Beginner: Strings in Python
- Part 4 - > Python Beginner: Numbers in Python
- Part 5 - Python Beginner: Lists in Python
- Part 6 - Python Beginner: Dictionaries in Python
- Part 7 - Python Beginner: If-Else in Python
- Part 8 - Python Beginner: For loop in Python
- Part 9 - Python Beginner: Function in Python
- Part 10 - Python Beginner: Classes in Python
- Part 11 - Python Beginner: Using built-in libraries in Python
- Part 12 - Python Beginner: Using 3rd party libraries in Python
- Part 13 - Python Beginner: Virtual environments in Python
- Part 14 - Python Beginner: Python Project structure
Computers are number crunching machines so its no surprise that any programming supports dealing with numbers. I will discuss two basic numeric data types: integers and floats which should cover almost all the use cases you’ll have.
Integer
Integers are whole numbers and their negative counterparts i.e. -1, -100, 0, 1, 10 and these are represented by int data type in Python. You can add, subtract, divide, multiply them as you usually do and also use other mathematical functions on them.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
age = 20
print(type(age)) # prints <class 'int'>
x = 10
y = 5
# add two numbers
print("x+y = ", x+y)
# subtract two numbers
print("x-y = ", x-y)
# multiply two numbers
print("x*y =", x*y)
# divide two numbers
print("x/y = ", x/y)
# "integer" divide two numbers
print("x//y = ", x//y)
# find the remainder when y divides x
print("x%y = ", x%y)
The code above prints the following
1
2
3
4
5
6
7
<class 'int'>
x+y = 15
x-y = 5
x*y = 50
x/y = 2.0
x//y = 2
x%y = 0
The results should be as expected but notice that when we divide x by y we got 2.0. While for us 2 and 2.0 are same for a computer it is not. 2 is an integer but 2.0 is a decimal (also called floats). So when we divide and integer by another integer, we get a float. But using //
operator, also called integer division, you will get an integer back. But what happens when you do integer division if the numerator is not perfectly divisible? For example, if you did print(7//2)
you will get 3 as an answer.
If you are wondering about x%y
, then it gives you remainder when y divides x. This is also called a modulo operator. In this case, 5 perfectly divides 10 so the result is 0 but if you did print(3%2)
you will get 1. This is also called “floor division” since the actual result is 1.5 but Python will discard any decimal places and give you only the “integer part”.
Float
Real numbers like 1.0, -0.4, 555.035 etc are represented using float data type in Python.
1
2
unit_price = 10.99
print(type(unit_price)) # prints <class 'float'>
Similar to integers, you can perform mathematical operations like adding, subtracting etc. The syntax is same.
Order of operations
Python will use a rule called operator precedence to determine which one to compute first. For example when we say print(2+2*3)
it will first multiply 2 and 3 because multiplication has higher precedence. and then add the result of that to 2. So the final result will be 8. But if you instead did print((2+2)*3)
, then 2+2 inside the brackets will be evaluated first resulting in 4 and then will multiply this with 3 to produce 12.
Be careful when you apply mathematical operators on more than two numbers.
2+2*3
is not same as(2+2)*3
. The order in which these operators are applied is important. If not, then look up PEDMAS/BEDMAS/BODMAS rule. For example, BODMAS rule means Brackets, Order, Division, Multiplication, Addition and Subtraction and it denotes the order in which the operators will be applied.
If you want to know more about operator precedence then visit its official documentation which lists not only the mathematical operators but also other operators.
Result of operations
The following table summarizes what you will get when you apply operators on ints and floats.
x | y | operator(s) | output type | remarks |
---|---|---|---|---|
int | int | +,-,*,% | int | when applying any of those for two integers you get an integer back |
int | int | / | float | when you divide and integer by integer, it will give you a float even though it is perfectly divisible |
int | int | // | int | using “integer division” or “floor division” the output will be an integer |
float | float | +,-,*,%,/ | float | when applying any of those for two floats you get a float back |
int | float | +,-,*,%,/ | float | when one of the operands i.e. x or y is a float then the output is float |
Exercise
Get familiar with what the output looks like when you add, subtract, divide and multiply numbers. Also try using more than two numbers of different data types at a time like print(2+3+5.99999)
. Get familiar with order in which the operation happens.
This article is Part 4 in a 14-Part Python Beginner's Tutorial Series.
- Part 1 - Python Beginner: Hello Python!
- Part 2 - Python Beginner: Variables in Python
- Part 3 - Python Beginner: Strings in Python
- Part 4 - > Python Beginner: Numbers in Python
- Part 5 - Python Beginner: Lists in Python
- Part 6 - Python Beginner: Dictionaries in Python
- Part 7 - Python Beginner: If-Else in Python
- Part 8 - Python Beginner: For loop in Python
- Part 9 - Python Beginner: Function in Python
- Part 10 - Python Beginner: Classes in Python
- Part 11 - Python Beginner: Using built-in libraries in Python
- Part 12 - Python Beginner: Using 3rd party libraries in Python
- Part 13 - Python Beginner: Virtual environments in Python
- Part 14 - Python Beginner: Python Project structure
Comments