Python: Numbers
Python Numbers
1 Overview
📘 Numbers in Python
Python has built-in support for numbers, including integers, floating-point numbers, and complex numbers. This module focuses on working with numeric data types and operations in Python, covering everything from basic arithmetic to more advanced mathematical functions.
2 Numbers
Numbers are a “simple” way to describe the world:
- How many apples you sold.
- How much money you earned.
- How many minutes are in a year.
- Even a picture on your phone — it’s just millions of numbers for colors.
3 Numbers
So when we teach computers to work with numbers, we’re giving them the building blocks to represent reality.
Once we have numbers, we can automate represent and manipulate anything algebraic: sales, costs, scores, time, even images and music.
4 Supermarket Scale Cost
- Automate cost calculation in a supermarket scale.
- The same code can be reused every day by changing the variables.
5 Tomato Stall
- Automate net income (before IRPF) calculation in a market’s day.
tomatoes_kg_sold = 30
price_per_kg = 1.5
production_costs_day = 0.5 # Daily cost for water, fertilizer, pesticides...
stall_rent_per_day = 20
social_security_day = 200 / 30
iva = 0.1 # %
daily_revenue = tomatoes_kg_sold * price_per_kg
expenses = production_costs_day + stall_rent_per_day + social_security_day
income = daily_revenue - expenses
net_b4_irpf = income / (1 + iva)
print("Today's net (before IRPF):", net_b4_irpf, "e")
6 Numbers in Python
- int (integers)
- float (floating-point)
DecimalComplex
7 Integers
Whole numbers without decimal points:
- positive
- negative
- zero
<class ‘int’>
<class ‘int’>
Basic integer operations: addition, subtraction, multiplication, floor division, modulus, and exponentiation.
Integer division (or floor division) rounds down the result to the nearest whole number.
3
In other languages, a common operator for exponentiation is
^
, but this is not the case in Python!1000
8 Floats
Numbers with decimal points (floating-point numbers).
- Limitations due to the way computers represent decimal numbers.
- Ints and floats can be mixed, giving the most restrictive type as a result:
- There can be rounding errors:
9 Round
round()
function rounds to the nearest whole number.
- If specified, rounds to a specific number of decimal places.
10 Underscore
- Underscores
_
can be used as visual separator in numbers. - Improves readability of big numbers.
- Has no impact on the value of the number.
11 Type conversion
- int to float:
- float to int:
12 Operation precedence
()
<!– * **
-x –>
*, /, //, % (same precedence, evaluated from left to right)
+, - (same precedence, evaluated from left to right)
Result?
Result?
13 Booleans
- Data type for representing true and false statements.
Conversion to bool:
bool(0)
→False
(True
for any number different than 0).bool('')
→False
(True
for any other string).
- Internally,
True
is the integer1
andFalse
the integer0
.
14 Logical operators
- Used for manipulating boolean values.
- Python keywords:
and
,or
,not
.
15 and
- Is true when all the elements are true, and false otherwise.
16 or
- Is false when all the elements are false, and true otherwise.
17 not
- Negates the element.
- We can chain as many as we want:
18 Precedence
Logical operators have lower precedence than arithmetic operators.
The precedence, highest to lowest:
not
and
or
Logical operators have lower precedence than arithmetic operators.
The precedence, highest to lowest:
not
and
or
Is the following expression true or false?
False or True and not True
- The expression above is evaluated as if written like:
False or (True and (not True))
19 Comparison operators
Evaluate relationship between values or expressions with True
or False
.
Equal to (
==
)Not equal to (
!=
)Less than (
<
)Greater than (
>
)Less than or equal to (
<=
)Greater than or equal to (
>=
)
- Comparison operators can be combined with logical operators:
- They can be chained with no limit:
20 Built-in functions
21 Imports
- Used to bring external code into your current program.
# Example of importing a single function from a module:
# from math import sqrt
# print(sqrt(9))
# Example of importing multiple functions:
# from math import sqrt, pi
# print(sqrt(9))
# print(pi)
# Example of importing all functions (not recommended):
# from math import *
# print(sqrt(9))
# print(pi)
# Example of importing with aliases:
# import math as m
# print(m.sqrt(9))
#
# from math import sqrt as square_root
# print(square_root(9))
Python Standard Library:
math
,random
,statistics
, etc.random
example:
22 Example
Write a program in which the user enters his/her weight (in kg) and height (in m). The system then calculates the Body Mass Index as weight divided by height squared.
23 Code readability
- Essential for making your code easier to understand and maintain.
- Start practicing with good coding as soon as possible.
- First tips:
- Choose meaningful variable names. E.g.
user_name
. - Use white spaces effectively. E.g.
x = x + 1
(instead ofx=x+1
).
- Choose meaningful variable names. E.g.