Let’s start our exploration of data types in Python with the numeric types. Numbers are very important in Python.
Table of Contents
Built-in Numeric Types:
There are three basic built-in numeric types:
– integers (ints)
– floating-point numbers (floats)
– complex numbers (not discussed here)
Let’s look at the first two types one at a time.
Integers
Integers are positive or negative whole numbers.
Integers can be of almost unlimited size. The only limit is the memory available in your computer.
>>> 7 ** 825
16065084409103133855112989859903289770338528954824827
58840759759203069455502416719373515169656809026095560
86373724357572672071822227957186075952143689893782354
64619509452320476969944429815152337565208997410755238
90425176263248198830626296012080784139889531549840957
43955527628423649552726534379270580580487151901711017
82171315550207742948532558654929607215981515460085477
24498550385321057557860010249859529704140457064112099
22494943210272502051574653056507596191464221807185208
07807586448588691354965057963818303251183346411767823
89729043545429719255388175483680359074750594195062967
60153396389470449647690182278581884841223531824348740
03496527312011725585406820449994991586923031386304329
120260807
>>>
Floating-Point Numbers
Floats are real numbers. They are written with a decimal point separating the integer part from the fractional part.
Example:
>>> 4.5 * 3.0 + 17.14254
30.64254
Numbers are widely used in arithmetic operations. Let’s explore the main operators we can use to this end.
Arithmetic Operators
Addition, subtraction and multiplication are pretty straightforward:
>>> 3 + 8
11
>>> 6.2 - 1.3
4.9
>>> 4 * 10
40
There are two kinds of division operators:
1) true division /
2) floor division //
In true division the result of dividing two integers is a float:
>>> 20 / 5
4.0
>>> 8 / 3
2.6666666666666665
In floor division the result is truncated down, so the result is the floor – the largest integer number smaller than the result of the true division:
>>> 20 // 5
4
>>> 8 // 3
2
We use the **
operator for powers:
>>> 7 ** 2
49
>>> 10 ** -2
0.01
>>> 64 ** 0.5
8.0
We use the %
character for the modulus operator which returns the remainder of a division:
>>> 10 % 2 # 10 / 2 = 5, remainder = 0
0
>>> 10 % 3 # 10 / 3 = 3, remainder = 1
1
>>> 5 % 17 # 5 / 17 = 0, remainder = 5
5
The modulus operator is often used to check whether a given number is divisible by another number. It is if the remainder is 0:
>>> if 100 % 15 == 0:
... print("100 is divisible by 15.")
... else:
... print("100 is not divisible by 15.")
...
There are lots of mathematical functions we can use for calculations.
Mathematical Functions
Python offers us quite a bunch of ready-made mathematical functions that we can use to perform all the basic mathematical operations:
abs(x)
– the absolute value of x
The absolute value of x is the distance between x and 0. It’s always positive. abs(x)
works with ints, floats and complex numbers:
>>> abs(7)
7
>>> abs(-30.26)
30.26
max(x, y, z, ...)
– the maximum value
This functions returns the greatest of its arguments:
>>> max(3, 5, 9, 4, 0, 6)
9
>>> max(-265.14, -57.47, 847.41, -1254.74)
847.41
min(x, y, z, ...)
– the minimum value
This functions returns the smallest of its arguments:
>>> min(45, -11.64, 71, -11.63999, 9.2e2)
-11.64
>>> a = 14
>>> min(a, 20, 30, 54)
14
round(x, n)
– x rounded to n decimal places
The parameter n is optional. Its default value is 0, so round(x) has the same meaning as round(x, 0):
>>> round(2.247854555, 4)
2.2479
>>> round(2.247854555)
2
and with negative numbers:
>>> round(-6.32441333547541)
-6
>>> round(-6.32441333547541, 1)
-6.3
Next, let’s have a look at how numbers can be assigned.
Assignment Operators
The basic assignment operator is ‘=’. It assigns the value of the operand on the right to the operand on the left:
>>> a = 5
>>> a
5
Sometimes we need to change the value of a variable by performing an operation on it and assigning the result back to it:
>>> a = 3
>>> a = a + 1
>>> a
4
In such cases we can use the augmented assignment statements which combine arithmetic operators with the basic assignment operator:
>>> a = 3
>>> a += 1
>>> a
4
By analogy we have:
>>> a = 3
>>> a -= 10
>>> a
-7
>>> a *= -3
>>> a
21
>>> a /= 4
>>> a
5.25
>>> a //= 2
>>> a
2.0
>>> a **= 5
>>> a
32.0
>>> a %= 10
>>> a
2.0
Here’s a summary:

Finally, let’s talk about random numbers for a while.
Random Numbers
We can use the random module to generate pseudo-random numbers.
First let’s import all the functions from this module:
>>> from random import *
Then we can use the random()
function which generates a random float number between 0.0 and 1.0, the upper bound not being included:
>>> random()
0.36566547704416574
To generate a random integer between x and y, we use the function randint(x, y)
. Both x and y are included. The number is different each time:
>>> randint(4,50)
33
>>> randint(4,50)
4
>>> randint(4,50)
32
>>> randint(4,50)
43
We use the uniform(x, y)
function to generate a random floating point number between x and y:
>>> uniform(1, 5)
2.931931759714461
>>> uniform(1, 5)
1.4142745868470468
>>> uniform(1, 5)
4.092385577041799
QUIZ
1. Which of the following is a floating point number: |
A) -12 |
B) 12 |
C) 12.0 |
2. In true division the result of dividing two integers is: |
A) an integer |
B) a float |
C) a float or an integer if the first integer is divisible by the second |
3. The exponentiation operator is: |
A) ^ |
B) $ |
C) ** |
4. The modulus operator is: |
A) mod |
B) % |
C) $ |
5. We use the … function to generate a random floating point number between x and y. |
A) random(x, y) |
B) randint(x, y) |
C) uniform(x, y) |
TRUE OR FALSE?
1) Complex numbers is a built-in numeric type. |
2) Floats are real numbers. |
3) The exponent must be a positive integer. |
4) The modulus returns the remainder of a division. |
5) The modulus operator can be used to check whether a number is divisible by another number. |
6) The abs(x) function returns the absolute value of a number. |
7) The basic assignment operator is ‘== ’ |
8) Augmented assignment statements combine arithmetic operators with the basic assignment operator. |
9) a *= b stands for a = a * b |
10) The largest number the random() function can generate is 1.0. |
11) The second argument of the function randint(x, y) is not included. |
WHAT’S THE OUTPUT?
1) >>> 6 / 3 |
2) >>> 12 // 5 |
3) >>> 16 ** 0.5 |
4) >>> 20 % 6 |
5) >>> if 10 % 3 >= 0: |
6) >>> max(4, 2, 7, 1) + min(-2, 3, 0, 9, 5, 8) |
7) >>> round(-6.218, 2) |
8) >>> a = 3 |
9) >>> a, b = 15, 4 |
10) >>> a, b = 3, 5 |
11) >>> from random import * |
12) >>> round(uniform(2.2114, 2.2365), 1) |
SOLUTION
QUIZ
1. Which of the following is a floating point number: |
A) -12 |
B) 12 |
C) 12.0 |
2. In true division the result of dividing two integers is: |
A) an integer |
B) a float |
C) a float or an integer if the first integer is divisible by the second |
3. The exponentiation operator is: |
A) ^ |
B) $ |
C) ** |
4. The modulus operator is: |
A) mod |
B) % |
C) $ |
5. We use the … function to generate a random floating point number between x and y. |
A) random(x, y) |
B) randint(x, y) |
C) uniform(x, y) |
TRUE OR FALSE?
1) Complex numbers is a built-in numeric type. True |
2) Floats are real numbers. True |
3) The exponent must be a positive integer. False |
4) The modulus returns the remainder of a division. True |
5) The modulus operator can be used to check whether a number is divisible by another number. True |
6) The abs(x) function returns the absolute value of a number. True |
7) The basic assignment operator is ‘== ’ False |
8) Augmented assignment statements combine arithmetic operators with the basic assignment operator. True |
9) a *= b stands for a = a * b True |
10) The largest number the random() function can generate is 1.0. False |
11) The second argument of the function randint(x, y) is not included. False |
WHAT’S THE OUTPUT?
1) >>> 6 / 3 Output: |
2) >>> 12 // 5 Output: |
3) >>> 16 ** 0.5 Output: |
4) >>> 20 % 6 Output: |
5) >>> if 10 % 3 >= 0: Output: |
6) >>> max(4, 2, 7, 1) + min(-2, 3, 0, 9, 5, 8) Output: |
7) >>> round(-6.218, 2) Output: |
8) >>> a = 3 Output: |
9) >>> a, b = 15, 4 Output: |
10) >>> a, b = 3, 5 Output: |
11) >>> from random import * Output: True |
12) >>> round(uniform(2.2114, 2.2365), 1) Output: 2.2 |
PROJECT
'''
Numeric Types - Project
PAINT THE FENCE
__________________________________________________________
Follow the steps below. You can find the solution at the bottom of this
document.
Your task is to write a program that solves a simple math problem.
It should calculate how much paint you need to buy to paint a fence and
how much it's gonna cost. The user is supposed to enter the following data
necessary for your program to run:
- the length of the fence
- the height of the fence
- whether it should be painted on one side or on both
The assumptions are:
- 1 kg of paint can cover 10 square meters of a surface
- 1 kg of paint costs $8
And here's how you should write this program:
#1. Write code that asks the user to enter:
- first the length of the fence
- then the height of the fence
- finally whether it should be painted on both sides
and store user input in the following variables: length, height and
both_sides respectively. With length and height add comments telling
us that they are measured in meters.
Remember that what the user enters is always a string. You already know
how to convert it to an integer if necessary.
'''
# Write your code here:
# __________________________________________
# __________________________________________
'''
#2. Write code to calculate the area of the surface to be painted,
the amount of paint needed and its cost.
Make use of the assumptions above. Use the following variables to store
the data:
- area_per_kg - to store the area that can be covered by 1 kg of paint
- price_per_kg - to store the price of 1 kg of paint
- area - to store the area to be painted
- paint_amount - to store amount of paint that is necessary to cover
the whole area calculated above
- cost - to store the total cost of the paint that you need
Your calculations should take into consideration whether the fence
should be painted on both sides or just on one side.
First calculate the area for one side and then use a conditional
statement to cover the case for both sides. Use your area variable
in the expression on the right and assign the result back to area.
When everything is calculated, the program should print the following
two messages:
The total area to be painted is X m2.
You'll need Y kg of paint, which will cost you $Z.
Naturally there should be real value instead if X, Y and Z. And don't
forget to convert numbers to strings before concatenating them.
Comment your code.
'''
# Write your code here:
# __________________________________________
# __________________________________________
PROJECT SOLUTION
#1.
length = int(input("Enter the length of the fence: ")) # in meters
height = int(input("Enter the height of the fence: ")) # in meters
both_sides = input("Should the fence be painted on both sides (yes / no)? ")
#2.
area_per_kg = 10 # 1 kg of paint for 10 m2 of surface
price_per_kg = 8 # $8 for 1 kg of paint
area = length * height # if 1 side is to be painted
# if both sides are to be painted
if both_sides == "yes":
area = area * 2
paint_amount = area / area_per_kg # in kg
cost = paint_amount * price_per_kg # total cost
print("The total area to be painted is " + str(area) + " m2.")
print("You'll need " + str(paint_amount)
+ " kg of paint, which will cost you $" + str(cost) + ".")