Logo

dev-resources.site

for different kinds of informations.

Division, Floor Division and Modulus - Python Arithmetic Operators every beginner should know.

Published at
8/2/2022
Categories
python
operators
beginners
modulus
Author
lilianmwaura
Author
12 person written this
lilianmwaura
open
Division, Floor Division and Modulus - Python Arithmetic Operators every beginner should know.

What is an Operator?

Operators are used to perform operations on variables and values.
In python, Operators are classified into different categories based on the kind of operations they perform:
They include:

  • Arithmetic Operators
  • Assignment Operators
  • Comparison Operators
  • Logical Operators
  • Identity operators
  • Membership operators
  • Bitwise operators

Arithmetic Operators
Arithmetic operators are used with numeric values to perform common mathematical operations:

Operator Name Example

  • + Addition x + y
  • - Subtraction x - y
  • * Multiplication x * y
  • / Division x / y
  • % Modulus x % y
  • ** Exponentiation x ** y
  • // Floor division x // y

Difference between Division, Modulus and Floor Division
All these operators are quite interesting because they all perform division, but print different results.

#Division (/) - basically gives out the result of a division.
a = 5
b = 2 
print(5/2)
# result will be 2.5

#Floor Division (//) - Provides the lower-bound of an integral division
a = 5
b = 2
print(5//2)
# result will be 2, the decimal is cut off returning only the whole number

#Modulus(%) - Computes the reminder of a division,which is the 'leftover' of an integral division.
a= 5
b=2
print(5%2)
#result will be 1, which is the 'leftover' integer after the division

Enter fullscreen mode Exit fullscreen mode

First time I learnt all these operations and the different results they print out, I did not quite understand the essence of knowing all these.

But understanding operators comes in handy when solving problems in Python.

For example, modulus is important in finding out even and odd numbers in python.
See the example below :

How to determine even and odd numbers using modulus(%)

n % 2 == 1 
# when n is divided by 2 and the output has a reminder of 1 which means that the number is odd.

n % 2 == 0
# when n is divided by 2 and the is 0, meaning the number is divisible by 2 then the result is an even number.



Enter fullscreen mode Exit fullscreen mode

Hope you learnt something.

Happy Coding!

operators Article's
30 articles in total
Favicon
Essential MySQL Operators and Their Applications
Favicon
Exposing replica nodes in Percona Operator for PostgreSQL
Favicon
It’s just β€˜,’ – The Comma Operator
Favicon
Operators, Conditionals and Inputs
Favicon
Practical Guide to Python Conditional Statements
Favicon
Python Operators Demystified
Favicon
SQL Operators Made Easy for Beginners
Favicon
First Steps in SQL Operators: A Beginner's Guide
Favicon
AND / OR operators, Short-Circuiting and Nullish Coalescing in Javascript
Favicon
From Zero to Hero: Disaster Recovery for PostgreSQL with Streaming Replication in Kubernetes
Favicon
Google Search Operators & Usage Tips
Favicon
Operators in C programming
Favicon
MySQL Operators – A Guide
Favicon
Annotations in Kubernetes Operator Design
Favicon
Exploring the unusual: JavaScript arrays and the 'in' operator
Favicon
Install Kubernetes Controllers via Operators - ARGO CD
Favicon
Mastering Advanced JavaScript Operators: The Ultimate Guide
Favicon
Operators in JavaScript: The Fundamentals
Favicon
Dart as, is, is! operatΓΆrleri
Favicon
Nullish Coalescing Operator
Favicon
Difference between ? and ?? in JavaScript/Typescript
Favicon
Ordering Event Bus Events with RxJS and concatMap
Favicon
Division, Floor Division and Modulus - Python Arithmetic Operators every beginner should know.
Favicon
Operators in Python
Favicon
Angular - Rxjs - Operator mergeAll
Favicon
Angular - Rxjs - Operator map
Favicon
Swift β€” 11 Useful Combine Operators You Need to Know
Favicon
Cloud Native CICD Pipelines in OpenShift
Favicon
Kubernetes Operators to realize the dream of Zero-Touch Ops
Favicon
JavaScript Basic - Variable, Data Types, Operators, Comparisons

Featured ones: