Python "math": Taking one notch up


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 logarithm (base e)
print(math.log10(100)) # base-10 logarithm
Out:
4.605170185988092
2.0

📊 Exponential Function (eˣ), where e ≈ 2.718 (Euler’s number)

In:
growth = math.exp(5)
print(growth)
Out:
148.4131591025766

🔶 Greatest Common Divisor - biggest number that divides both

In:
print(math.gcd(120, 80))
Out:
40

🔽 Least Common Multiple - smallest number both can divide evenly

In:
print(math.lcm(12, 18))
Out:
36

👨‍👩‍👧‍👦 Permutations - Number of ways to arrange k items out of n (order matters)

k -> 10; n -> 3

In:
print(math.perm(10, 3))
Out:
720

🔄 Combinations - Number of ways to arrange k items out of n (order does not matter)

In:
print(math.comb(10, 3))
Out:
120

🌟 Get 1. remainder and 2. Integer after removing the decimal

In:
print(math.fmod(23, 7))   # Remainder
print(math.trunc(4.9))    # Removes decimal
Out:
2.0
4

Mini Exercise 🏋️

A warehouse is at (0,0) and a truck is at (6,8). Calculate the distance between the two.


⭐📣 Tomorrow we'll see more applications of math on different datatypes. 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, 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,...