Types of Numbers in Python

Python has various "types" of numbers (numeric literals). We'll mainly focus on integers and floating point numbers.
Integers are just whole numbers, positive or negative. For example: 2 and -2 are examples of integers.
In this lecture, we will learn about numbers in Python and how to use them.
We'll learn about the following topics:
- Types of Numbers in Python
- Basic Arithmetic
- Differences between classic division and floor division
- Object Assignment in Python
Types of Numbers
Python has various "types" of numbers (numeric literals). We'll mainly focus on integers and floating point numbers.
Integers are just whole numbers, positive or negative. For example: 2 and -2 are examples of integers.
Floating point numbers in Python are notable because they have a decimal point in them, or use an exponential (e) to define the number. For example 2.0 and -2.1 are examples of floating point numbers. 4E2 (4 times 10 to the power of 2) is also an example of a floating point number in Python.
Throughout this course we will be mainly working with integers or simple float number types.
Here is a table of the two main types we will spend most of our time working with some examples:
Examples | Number "Type" |
---|---|
1,2,-5,1000 | Integers |
1.2,-0.5,2e2,3E2 | Floating-point numbers |
Now let's start with some basic arithmetic.
Basic Arithmetic
# Addition
2+1
# Subtraction
2-1
# Multiplication
2*2
# Division
3/2
# Floor Division
7//4
# Modulo
7%4
Arithmetic continued
# Powers
2**3
# Can also do roots this way
4**0.5
# Order of Operations followed in Python
2 + 10 * 10 + 3
# Can use parentheses to specify orders
(2+10) * (10+3)
Variable Assignments
Now that we've seen how to use numbers in Python as a calculator let's see how we can assign names and create variables. We use a single equals sign to assign labels to variables. Let's see a few examples of how we can do this.
# Let's create an object called "a" and assign it the number 5
a = 5
#Now if I call a in my Python script, Python will treat it as the number 5.
# Adding the objects
a+a
What happens on reassignment? Will Python let us write it over?
# Reassignment
a = 10
# Check
print(a)
Yes! Python allows you to write over assigned variable names. We can also use the variables themselves when doing the reassignment. Here is an example of what I mean:
# Use A to redefine A
a = a + a
# Check
print(a)
The names you use when creating these labels need to follow a few rules:
- Names can not start with a number.
- There can be no spaces in the name, use _ instead.
- Can't use any of these symbols :'",<>/?|\()!@#$%^&*~-+
- It's considered best practice (PEP8) that names are lowercase.
- Avoid using the characters 'l' (lowercase letter el), 'O' (uppercase letter oh), or 'I' (uppercase letter eye) as single character variable names.
- Avoid using words that have special meaning in Python like "list" and "str" Using variable names can be a very useful way to keep track of different variables in Python. For example:
# Use object names to keep better track of what's going on in your code!
my_income = 100
tax_rate = 0.1
my_taxes = my_income*tax_rate
# Show my taxes!
print(my_taxes)
So what have we learned? We learned some of the basics of numbers in Python. We also learned how to do arithmetic and use Python as a basic calculator. We then wrapped it up with learning about Variable Assignment in Python.
Random Blogs
- Ideas for Content of Every niche on Reader’s Demand during COVID-19
- Important Mistakes to Avoid While Advertising on Facebook
- AI in Cybersecurity: The Future of Digital Protection
- Role of Digital Marketing Services to Uplift Online business of Company and Beat Its Competitors
- Data Analytics: The Power of Data-Driven Decision Making
- The Ultimate Guide to Data Science: Everything You Need to Know
- Transforming Logistics: The Power of AI in Supply Chain Management
- Python Challenging Programming Exercises Part 3
- Government Datasets from 50 Countries for Machine Learning Training
- Understanding HTAP Databases: Bridging Transactions and Analytics
Prepare for Interview
- SQL Interview Questions for 2–5 Years Experience
- SQL Interview Questions for 1–2 Years Experience
- SQL Interview Questions for 0–1 Year Experience
- SQL Interview Questions for Freshers
- Design Patterns in Python
- Dynamic Programming and Recursion in Python
- Trees and Graphs in Python
- Linked Lists, Stacks, and Queues in Python
- Sorting and Searching in Python
- Debugging in Python
- Unit Testing in Python
Datasets for Machine Learning
- Amazon Product Reviews Dataset
- Ozone Level Detection Dataset
- Bank Transaction Fraud Detection
- YouTube Trending Video Dataset (updated daily)
- Covid-19 Case Surveillance Public Use Dataset
- US Election 2020
- Forest Fires Dataset
- Mobile Robots Dataset
- Safety Helmet Detection
- All Space Missions from 1957
- OSIC Pulmonary Fibrosis Progression Dataset
- Wine Quality Dataset
- Google Audio Dataset
- Iris flower dataset
- Artificial Characters Dataset
- Bitcoin Heist Ransomware Address Dataset