Python Crash Course 2nd Revision: Stage-4

python Rev2 Math Operators

Math Operators and Operations

In the fourth part of our Python Crash Course, we’ll dive into the world of math operators. Understanding how to perform mathematical operations is essential for programming, and Python provides a powerful set of tools for this purpose. We’ll explore arithmetic operators, operator precedence, and how to use math in your Python programs. Let’s get started with the magic of math in Python!

Arithmetic Operators:

Python supports a variety of arithmetic operators that allow you to perform basic math operations. These operators include:

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

Basic Math Operations:

You can use these operators to perform basic math operations in Python. For example:

result = 5 + 3

In this code, result stores the value 8, which is the result of adding 5 and 3.

Operator Precedence:

In Python, operators have precedence rules that determine the order in which operations are performed. For example, multiplication and division take precedence over addition and subtraction. You can use parentheses to specify the order of operations explicitly. If any of these symbols are used together in a single command, you will need to remember the order in which each operator will execute.

Order of Precedence:

  • () Parenthesis
  • ** Exponent
  • / Multiplication and Division
  • +- Addition and Subtraction
Example with Precedence:
result = 5 + 3 * 2

In this example, the multiplication (*) takes precedence, so result will be 11, not 16.

Additional Math Functions


Modulo is often mistaken by beginners as percent, and why wouldn’t it, but it’s actual use is to find the remainder of a division. What do you think will be printed when you run the code below?


Hopefully you knew the result from the code above would print 0 this is because there is no remainder left. Now try it with some different numbers, modulo is typically used with whole numbers, but go ahead and experiment by yourself.

We can also use Modulo to find if an entered number is odd or even:

num=(input("Please enter a number: "))
if float(rem==0):
print(num," is even")
elif float(rem==1):
print(num," is odd")
print("Please enter a valid number")
print("have a nice day!")

Exponent is used to multiply a number by itself, otherwise known as "to the power of".


The code above results in 9 being printed to the console. To break this down if you don’t understand it, a is the base number and b is the exponent or number of times to multiply by itself. 3² = 3x3 = 9.

Math Examples

Take a look at the next math example. Run them through your Python interpreter.  Experiment with the width and height variable values.

Multiplication Bot

I have written this basic Python script to show you how you can use Python math operators along with other coding concepts. You will notice some new concepts we haven’t covered yet. Don’t worry, by the end of this crash course you will understand them all, but for now, run the code below in your Python interpreter:

from time import sleep #Imported time for delays

dt=.001 #Adjust the timing here
b=1 #Starting Point for our times tables

def timesTables(): #The function
global b #Call a variable outside of the function
a = 1 #Starting point for our times tables
for i in range (1,15): #To loop code 14x
c = a * b #Mathematical operation
print(str(b) + " x " + str(a) + " = " + str(c))
a+=1 #Add +1 with loop
sleep(dt) #Delay time
b+=1 #Add +1 to starting point with loop

while True:
timesTables() #call the function

The script will continue to escalate through the times tables for as long as its running with no end. The magic here, there is only one calculation c = a * b from that one calculation it will continue multiplying until the script stopped.

Math Library

Python provides a math module that includes functions for more advanced mathematical operations. To use this module, you need to import it.

import math

result = math.sqrt(25)

In this code, we import the math module and use the sqrt() function to calculate the square root of 25.


In this fourth part of the Python Crash Course, you’ve delved into the realm of math operators, which are fundamental to performing mathematical calculations in Python. As you continue to learn and practice with these operators, you’ll gain the ability to create more complex and functional Python programs. In the upcoming posts, we’ll explore data types, control structures, and more. In the next part of our Python crash course, we will be learning about Looping Code. Stay tuned for more Python insights!

 That’s All Folks!

You can explore more of our Python guides here: Python Guides

Luke Barber

Hello, fellow tech enthusiasts! I'm Luke, a passionate learner and explorer in the vast realms of technology. Welcome to my digital space where I share the insights and adventures gained from my journey into the fascinating worlds of Arduino, Python, Linux, Ethical Hacking, and beyond. Armed with qualifications including CompTIA A+, Sec+, Cisco CCNA, Unix/Linux and Bash Shell Scripting, JavaScript Application Programming, Python Programming and Ethical Hacking, I thrive in the ever-evolving landscape of coding, computers, and networks. As a tech enthusiast, I'm on a mission to simplify the complexities of technology through my blogs, offering a glimpse into the marvels of Arduino, Python, Linux, and Ethical Hacking techniques. Whether you're a fellow coder or a curious mind, I invite you to join me on this journey of continuous learning and discovery.

One thought on “Python Crash Course 2nd Revision: Stage-4

  1. I like what you guys are up too. Such smart work and reporting! Carry on the superb works guys I’ve incorporated you guys to my blogroll. I think it will improve the value of my website 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

Verified by MonsterInsights