Any kind of "math": Python has you covered


Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python.

Today, we'll introduce Python’s math module—your shortcuts for real calculations.

What is the math Module?

A standard Python library that provides mathematical functions - square root, logarithm, trigonometry, you name it. Tasks from rounding prices for invoices to complex analytics for finance or science can be done in a breeze.

Why it matters?

You’ll use it for rounding numbers, growth projections, probability calculations, and cleaning messy decimals. Think: estimating trucks for shipments, calculating compound interest, or normalizing data before analysis.

Also, we will cover this over multiple days, so feel free to share it with anyone who would like to join. You can brush up previous concepts too.

🌟 Loading math library - This loads all functionalities of the module.

In:
import math
Out:

🌟 Pre-Defined Constants

In:
print(math.pi) # value of π
print(math.e) # Euler's number
Out:
3.141592653589793
2.718281828459045

🌟 Round down numbers using floor(), round up using ceil()

In:
print(math.floor(7.9))
print(math.ceil(7.9))
Out:
7
8

🌟 Take square roots

In:
print(math.sqrt(144))
Out:
12.0

🐶 How to take square then? - Use pow()

In:
print(math.pow(12, 2))
Out:
144.0

📖 pow(a,b) -> a raised to power b

In:
print(math.pow(6, 3))
Out:
216.0

🎡 Sizegn does not matter - fabs for absolute values

In:
print(math.fabs(-15.7))
Out:
15.7

💔 Break-up Integer and Fraction/Decimal

In:
print(math.modf(123.45))
Out:
(0.44999999999998863, 123.0)

Factorials are cakewalk

In:
print(math.factorial(5))
Out:
120

Mini Exercise 🏋️

Your revenue is 1000 and grows 8% annually. Calculate the projected revenue after 5 years.


⭐📣 That's it for today! If you liked it, please share it with anyone who will find it useful and share your feedback below 🐼

Pandas Daily

Beginner to Expert in Python in just 5 minutes

Read more from Pandas Daily

Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. Today, we’ll explore the datetime module — the fastest way to work with dates and timestamps in Python What is datetime module? ⏰ A built-in Python library that supplies classes for dealing with dates & times. 🛞 Create, manipulate, and format date and time values. ❓ Why it matters - Think of last 3 month, 6 month sales tracking; invoice generation dates, or scheduling reminders - datetime is the tool to...

Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. Today: Lets dive right away into some of the applications and advanced functions of "math" module. Do check out last two issues for a refresher. 📝 Sum of all numbers in a list? In: import math amounts = [50.12, 25.35, 14.53] total = math.fsum(amounts) print(total) Out: 90.0 🚀🚀 Multiply all numbers of list in one go! In: print(math.prod([2, 2, 3])) Out: 12 📍 Distance between two points (latitude, longitude...

Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. Today is the 2nd day of Python’s math module where we go one level deep! You might go into memory lane as some concepts we would have studied during high school. Feel free to go through first part real quick. 📐 Remember Pythagoras's Theorem? Calculate hypotenuse from two shorter sides In: import math print(math.hypot(3, 4)) Out: 5.0 🌟 Taking logarithms in variety of ways In: print(math.log(100)) # natural...