PedroGeoGISdev
  • Home
  • Linux OS
    • Linux: concepts
    • Linux: basic concepts
    • Linux: Bash
    • Linux: su and sudo
    • Linux: pipes
    • Linux: File System
    • Linux: Virtual Machines

    • Linux: distros
    • Linux Distros: Ubuntu
    • Linux Distros: Mint
    • Linux Distros: Debian
    • Linux Distros: openSuse
    • Linux Distros: Manjaro
    • Linux Distros: Red Hat Enterprise

    • Linux: laboratories
    • Linux Lab#LI01-1: Choose Linux
    • Linux Lab#LI01-2: Install at least three distributions
    • Linux Lab#LI01-3: Adjust user permissions
    • Linux Lab#LI02-1: Export env user with grep and pipe
    • Linux Lab#LI03-1: Manage users and groups
    • Linux Lab#LI03-2: Manage files
    • Linux Lab#LI03-3: Manage software
    • Linux Lab#LI03-4: Manage hardware
    • Linux Lab#LI04-1: Bash scripting, qtool
    • Linux Lab#LI04-2: Bash scripts as terminal tool
    • Linux Lab#LI04-3: Distribute the terminal app

    • Linux readings
    • Linux Resources
  • DevOps
    • What is DevOps
    • DevOps: Introduction
    • DevOps: Agile and Microservices
    • Infrastructure as code (IaC)
    • Immutable Infrastructure
    • Software Lifecycle

    • Documentation
    • How to document: Quarto and Obsidian

    • Network protocols
    • Network: Basics
    • Network: Client-server
    • Network Protocols
    • Network: DNS
    • Network: API Rest
    • Network: gRPC
    • Network: Websocket
    • Network: SMTP
    • Network: Ping
    • Network: UDP
    • Network: webhook
    • Network: SOAP
    • Network: graphQL

    • Version Control
    • Git
    • GitHub
    • Idea and GitHub 2023
    • Git and GitHub 2023 CLI

    • IDEs
    • IDE: Visual Code
    • IDE: IntellJIdea

    • DevOps tools
    • Amazon Web Services AWS
    • Docker
    • Jenkins pipelines
    • Kubernetes k8s
    • Digital Ocean
    • Nagios
    • Ansible

    • DevOps Laboratories
    • Lab 1: chat App
    • Lab 2: Spring Boot AWS AEB manually
    • Lab 3: Spring Boot and AWS S3 publisher
    • Lab 4: Spring Boot Docker/Jenkins
    • Lab 5: k8s on Digital Ocean
    • Lab 6: Spring Boot AWS codecommit

    • DevOps readings
    • DevOps Resources
  • MarkUp
    • MarkUp Languages
    • Introduction Markup
    • HTML Markup
    • Markdown Markup
    • Markdown and HTML working together, good idea?

    • Quarto Markdown
    • Quarto Markdown: basics
    • Quarto Markdown: creating
    • Quarto Markdown: publishing
    • Quarto Markdown: code & data
    • Quarto Markdown: api rest call
    • Quarto Markdown: OJS Cells
    • Quarto Markdown: cheat-sheet

    • Styling: CSS
    • Cascade Style Sheet
    • Cascade Style Sheet: Box Model and Containers
    • CSS: W3.css

    • MarkUp Languages Laboratories
    • Lab#MD01-1: Create and publish by Quarto

    • MarkUp Languages readings
    • MarkUp Languages Resources
  • Java SE
    • What is Java SE
    • Java Standard Edition: Basics
    • Java Standard Edition: Principles
    • Java MOOC Helsinki
    • Java MOOC Helsinki Syllabus

    • Java Create Project
    • Java SE: Maven
    • Java SE: Create Maven Project
    • Java SE: Project push GitHub
    • Java SE: JUnit and TDD

    • Java Concepts
    • Java SE: Class and Objects
    • Java SE: Scope
    • Java SE: static modifier
    • Java SE: Coupling and DDD
    • Java SE: Packages
    • Java SE: Abstract/Interface
    • Java SE: Java 8

    • Java Principles
    • Java SE: Encapsulation
    • Java SE: Abstraction
    • Java SE: Inherence
    • Java SE: Polymorphism

    • Java Design Patterns
    • Java Patterns: UML
    • Java Patterns: Types
    • Singleton
    • Factory
    • Abstract Factory
    • Builder
    • Facade
    • Bridge
    • Decorator
    • Composite
    • Observer
    • Strategy
    • State
    • Commander

    • Java SE Laboratories
    • Lab#SE00-1: Maven Person
    • Lab#SE00-2: Maven Clinic
    • Lab#SE00-3: Library Model
    • Lab#SE00-4: Abstract/Interface Human
    • Lab#SE01-1: Maven/Gradle Person and Account
    • Lab#SE01-2: Maven/Gradle Person and Account stored in JSON
    • Lab#SE02-1: Movie/Review, Model
    • Lab#SE02-2: Movie/Review, CRUD Operations
    • Lab#SE02-3: Movie/Review, factory
    • Lab#SE02-4: Movie/Review, interactivity and coupling
    • Lab#SE02-5: Movie/Review, simulate interactivity by console
    • Lab#SE03-1: Library/Book, Core-Model
    • Lab#SE03-2: Library/Book, Sprint Zero
    • Lab#SE03-3: Library/Book, Expand Model
    • Lab#SE04-1: healthyFood Restaurant, Core Model

    • Java SE readings
    • Java SE Resources
  • Python
    • Python Basics
    • Python: Basic Concepts
    • Python: Numbers
  • JavaScript
    • JavaScript Basics
    • JavaScript: Basic Concepts
    • JavaScript: Tips

    • React Framework

    • ReactJS: Principles
    • React JS: Introduction
    • React JS: render virtual DOM
    • React JS: Create a React project
    • React JS: Components
    • React JS: JSX
    • React JS: props and state

    • JavaScript: web scripting
    • JavaScript: basics
    • JavaScript: functions
    • JavaScript: objects
    • JavaScript: variables
    • JavaScript: flux control

    • ES6: ECMAScript 6
    • React JS ES6: arrow functions
    • React JS ES6: import modules
    • React JS ES6: array, data and key
    • React JS ES6: destructuring
    • React JS ES6: spread operator

    • ReacJS 18: Hooks
    • React JS: Rules of Hooks
    • ReactJS: useState
    • React JS: useReducer
    • React JS: useRef
    • React JS: useEffect
    • React JS: useContext
    • ReactJS: useMemo
    • ReactJS: custom hooks

    • ReactJS: Designing an App
    • React JS App: async
    • React JS App: events
    • React JS App: router
    • React JS App: conditional render
    • React JS App: styling

    • React JS: Laboratories
    • Lab#RE01-1: API Rest Axios
    • Lab#RE02-1: Router & Hooks
    • Lab#RE03-1: to-do app
    • Lab#RE03-2: HighCharts
    • Lab#RE03-3: API Rest Mono
    • Lab#RE03-4: API Rest Domains
    • Lab#RE03-5: data management
    • Lab#RE04-1: todo & server
    • Lab#RE04-2: Spring Boot & ReactJS
    • Lab#RE05-1: chat & websockets
    • Lab#RE05-2: chat: backend
    • Lab#RE05-3: chat & AWS
    • Lab#RE05-4: chat: test ws AWS
    • Lab#RE05-5: chat & front
    • Lab#RE05-6: chat & ws: front
    • Lab#RE06-1: healthyFood Restaurant
    • Lab#RE06-1-PR: create a pull request
    • Lab#RE07-1: traffic lights simulation

    • React JS readings
    • ReactJS Resources
  • Backend
    • Spring Legacy
    • Spring Framework
    • Spring MVC
    • Springs Servlets

    • Spring Boot Basics
    • Spring Boot: fundamentals
    • Spring Boot: create a Project
    • Spring Boot: H2 DB and Thymeleaf
    • Spring Boot: cycle

    • Spring Boot Concepts
    • Spring Boot: Dependency Injection
    • Spring Boot: Annotations
    • Spring Boot: Controller
    • Spring Boot: View
    • Spring Boot: Thymeleaf
    • Spring Boot: Vaadin Flow
    • Spring Boot: Vaadin Hilla
    • Spring Boot: Model
    • Spring Boot: Rest
    • Spring Boot: Data & DB
    • Spring Boot: JPA & DI
    • Spring Boot: JPA Mappings
    • Spring Boot: JPA Relationships
    • Spring Boot: JPA Queries
    • Spring Boot: JPA Inherence
    • Spring Boot: Scaling

    • Spring Boot Laboratories
    • Lab#SB00-1: Library UML
    • Lab#SB00-2: CRUD User
    • Lab#SB00-3: LibraryManagement
    • Lab#SB00-4: API Rest
    • Lab#SB00-5: Rest & JPA-H2
    • Lab#SB00-6: Rest & MongoDB
    • Lab#SB00-7: Styling
    • Lab#SB01-1: DataBase
    • Lab#SB02-1: JPA Relationships
    • Lab#SB03-1: APIs & cloud
    • Lab#SB04-1: JPA Inherence
    • Lab#SB05-1: API Rest
    • Lab#SB06-1: employeeCourse
    • Lab#SB07-1: monitor Book
    • Lab#SB08-1: Restaurant UML
    • Lab#SB08-2: Vaadin
    • Lab#SB08-3: H2 and API Rest
    • Lab#SB08-4: JPA
    • Lab#SB08-5: Test API Rest
    • Lab#SB09-1: SpringIO Conference

    • Spring Boot readings
    • Spring Boot Resources
  • Learning
    • Vocabulary
    • General Vocabulary
    • SCRUM Vocabulary
    • DevOps Vocabulary
    • Java SE Vocabulay
    • Spring Boot Vocabulary
    • DataBase Vocabulary
    • ReactJS Vocabulary
    • Web Vocabulary

    • Learning
    • Useful Questions
    • Learning: tips
    • Writing
    • Taking Notes
    • Comments
    • Document
    • Auto-Evaluate

    • Books & Articles
    • Books
    • Articles

    • What is SCRUM
    • SCRUM Agile Methodology
    • Agile Manifesto & Values
    • SCRUM Guide

    • Scrum Steps
    • Meetings, Impediments and Iterations
    • User stories, Tasks and Habits
    • Delivering Value & Communication
    • ScrumMaster, how it works
    • Mindset, the key to everything
    • Product Owner, how it works
    • Managing Time & Mind
    • Team & the Specialist
    • Albertus’ Dilemma
    • Before SCRUM
    • Team Dynamics
    • Emotions and Thoughts
    • Decision Making and Intuition
    • Beyond SCRUM
    • Balances, atmosphere and tools

    • Resources
    • SCRUM Resources
  • QGIS
    • QGIS basics
    • QGIS: basic concepts

    • QGIS laboratories
    • QGIS Laboratory 1: Introduction to Open Source GIS
  • ArcGIS Pro
    • ArcGIS Pro basics
    • ArcGIS Pro: basic concepts

    • ArcGIS Pro laboratories
    • ArcGIS Pro Laboratory 1: Getting Started
  • Bookmarks
    • Online Resources
    • Online Resources
  • About
    • About me and this site
    • About me
    • About this site
    • About images credit
  • Maps
  • Featured Projects
    • ✈️ Flight tracker
    • 🧑 People tracker
    • 🚗 Renting Car
    • Email
    • GitHub
    • LinkedIn
  1. Python Basics
  2. Python: Numbers
  • Python

  • Python Basics
    • Python: Basic Concepts
    • Python: Numbers

On this page

  • 1 Overview
  • 2 Numbers
  • 3 Numbers
  • 4 Supermarket Scale Cost
  • 5 Tomato Stall
  • 6 Numbers in Python
  • 7 Integers
  • 8 Floats
  • 9 Round
  • 10 Underscore
  • 11 Type conversion
  • 12 Operation precedence
  • 13 Booleans
  • 14 Logical operators
  • 15 and
  • 16 or
  • 17 not
  • 18 Precedence
  • 19 Comparison operators
  • 20 Built-in functions
  • 21 Imports
  • 22 Example
  • 23 Code readability
  • Edit this page
  • View source
  • Report an issue
  1. Python Basics
  2. Python: Numbers

Python: Numbers

Python Numbers

python
numbers
The file system is organized into a hierarchical structure, starting with the root directory
Author

pedromartinezduran

Published

Sunday, June 1, 2025

Modified

Saturday, October 4, 2025

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.
apple_kgs = 1.5
apple_price_kg = 2

cost = apple_kgs * apple_price_kg

print("Cost:", cost, "e")
Cost: 3e

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")
Today’s net (before IRPF): 16.21e

6 Numbers in Python

  • int (integers)
  • float (floating-point)
  • Decimal
  • Complex

7 Integers

Whole numbers without decimal points:

  • positive
  • negative
  • zero
print(type(4))
print(type(0))
print(type(-145))
<class ‘int’>
<class ‘int’>
<class ‘int’>

Basic integer operations: addition, subtraction, multiplication, floor division, modulus, and exponentiation.

x = 10
y = 3

print(x + y)  # addition
13
print(x - y)  # subtraction
7
print(x * y)  # multiplications
30

  • Integer division (or floor division) rounds down the result to the nearest whole number.

    print(x // y) # floor division
    3
print(x % y)  # modulus
1
  • In other languages, a common operator for exponentiation is ^, but this is not the case in Python!

    print(x ** y) # exponentiation
    1000

8 Floats

Numbers with decimal points (floating-point numbers).

x = 4.5
y = 2.61

print(x + y)  # addition
7.109999999999999
print(x - y)  # subtraction
1.8900000000000001
print(x * y)  # multiplications
11.745

print(x / y)  # division
1.7241379310344829
print(x % y)  # modulus
1.8900000000000001
print(x ** y) # exponentiation
50.68560805900789

  • Limitations due to the way computers represent decimal numbers.
  • Ints and floats can be mixed, giving the most restrictive type as a result:
x = 1   # this is an integer
y = 2.8 # this is a float
z = x + y

print(z, type(z))
3.8 <class ‘float’>

  • There can be rounding errors:
# This result should be 0.3
print(0.1 + 0.2)
0.30000000000000004

9 Round

  • round() function rounds to the nearest whole number.
print(round(2.4))
2
print(round(2.6))
3

  • If specified, rounds to a specific number of decimal places.
# Round to 2 decimal places
print(round(0.123456, 2))
0.12
# Round to 5 decimal places
print(round(0.123456, 5))
0.12346

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.
# Using underscores in integers for better readability
one_million = 1_000_000
one_billion = 1_000_000_000

# Compare it without underscores, it is much more difficult to read
one_million = 1000000
one_billion = 1000000000

11 Type conversion

  • int to float:
x = 10
print(type(x))
<class ‘int’>
# Convert to float
x = float(x)
print(x, type(x))
10.0 <class ‘float’>

  • float to int:
x = 4.6
print(type(x))
<class ‘float’>
# Converting to int truncates the decimals
x = int(x)
print(x, type(x))
4 <class ‘int’>

12 Operation precedence

  • ()

    <!– * **

  • -x –>

  • *, /, //, % (same precedence, evaluated from left to right)

  • +, - (same precedence, evaluated from left to right)

result = 2 + 3 * 4 - 1
print(result)
  • Result?


result = (2 + 3) * (4 - 1)
print(result)
  • Result?


13 Booleans

  • Data type for representing true and false statements.
is_true = True
is_false = False

Conversion to bool:

  • bool(0) → False (True for any number different than 0).

  • bool('') → False (True for any other string).


  • Internally, True is the integer 1 and False the integer 0.
print(True + 3)  # True is treated as 1, so the result is 1 + 3 = 4
4
print(False * 5)  # False is treated as 0, so the result is 0 * 5 = 0
0

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.
True and True   # True
True and False  # False
False and True  # False
False and False # False

16 or

  • Is false when all the elements are false, and true otherwise.
True or True   # True
True or False  # True
False or True  # True
False or False # False

17 not

  • Negates the element.
not True  # False
not False # True

  • We can chain as many as we want:
True and True and True and True # True
True and True and True and False # False

True or True or True or False # True
False or False or False or False # False

(True and True) or (True and False) # True

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


print(False or True and not True)
False
  • 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 (>=)


# Equal to

5 == 5  # True
5 == 6  # False
# Not equal to

5 != 5  # False
5 != 6  # True

# Less than

5 < 6  # True
6 < 5  # False
# Greater than

5 > 6  # False
6 > 5  # True

# Less than or equal to

5 <= 5  # True
5 <= 6  # True
6 <= 5  # False
# Greater than or equal to

5 >= 5  # True
6 >= 5  # True
5 >= 6  # False

  • Comparison operators can be combined with logical operators:
x = 5
y = 7

print(x < y and y < 10)
True
  • They can be chained with no limit:
print(3 < 5 < 7)  # Equivalent to (3 < x) and (x < 7)
True
## Comparison operators precedence
# - Higher precedence than logical operators (`not`, `and`, `or`).
# - Lower precedence than arithmetic operators.
# 
# 3 < 5 * 2  # equivalent to 3 < (5 * 2)
# 3 < 5 and 5 < 7  # equivalent to (3 < 5) and (5 < 7)

20 Built-in functions

# absolute value
print(abs(-2))
2
# two to the power of 3
print(pow(2, 3))
8.0

# returns the minimum number
print(min(2, 1, 4, 9, 5))
1
# returns the maximum number
print(max(2, 1, 4, 9, 5))
9

21 Imports

  • Used to bring external code into your current program.
import math
# Calculate square root
print(math.sqrt(9))
3.0
# Show pi
print(math.pi)
3.141592653589793
# 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:

import random

# Generates a random integer between 1 and 10
random_number = random.randint(1, 10)
print(random_number)
7

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.

# Calculate BMI

weight = float(input("Enter your weight in kilograms:"))
height = float(input("Enter your height in meters:"))

bmi = weight / height ** 2
print("Your BMI is", bmi)

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 of x=x+1).
Back to top
Python: Basic Concepts

License: CC0 1.0 Universal

Per aspera, ad astra

  • Edit this page
  • View source
  • Report an issue