Numbers in Python

Upendra Pratap Kushwaha
Upendra Pratap Kushwha
2 min readJun 3, 2020

This blog post is part of our course Python Tutorial for Ultimate Beginners.

In this blog, 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

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.

Basic Arithmetic

# Addition 
>>> 2+1
3
# Subtraction
>>> 2-1
1
# Multiplication
>>> 2*2
4
# Division
>>> 3/2
1.5
# Floor Division
>>> 7//4
1

What just happened? Last time I checked, 7 divided by 4 equals 1.75 not 1!

The reason we get this result is that we are using “floor” division. The // operator (two forward slashes) truncates the decimal without rounding and returns an integer result.

So what if we just want the remainder after division?

4 goes into 7 once, with a remainder of 3. The % operator returns the remainder after division.

Arithmetic continued…

# Powers
>>> 2**3
8
# Can also do roots this way
>>> 4**0.5
2.0
# Order of Operations followed in Python
>>> 2 + 10 * 10 + 3
105
# Can use parentheses to specify orders
>>> (2+10) * (10+3)
156

Originally published at https://blog.upendra.tech on June 3, 2020.

--

--

Upendra Pratap Kushwaha
Upendra Pratap Kushwha
0 Followers

Software Engineer — Machine Learning — Python, AWS, SQL