Valuable Python functions hiding in plain sight


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 etc)

In:
p1, p2 = (0, 0), (3, 4)
dist = math.hypot(p2[0] - p1[0], p2[1] - p1[1])
print(dist)
Out:
5.0

🧊 3D distance (can be used for n dimensions too)

In:
p = [0, 0, 0]
q = [3, 4, 12]
d = math.dist(p, q)
print(d)
Out:
13.0

🔜 For approximate math (a and b are not equal but really close)

In:
a = 0.3 * 3 + 0.1
b = 1.0
is_close = math.isclose(a, b)

print(a)
print(b)
print(is_close)
Out:
0.9999999999999999
1.0
True

❌ Nan (Not a number) - An important check in many codes!!

True if value is not a number, false otherwise

In:
print(math.isnan(5.0))
print(math.isnan(float('nan')))
Out:
False
True

♾️ Ensure your number is not moving to infinity (when a number if divided by 0)

convert numbers to float to use 'isnan' and 'isinf' as they don't work on strings

In:
print(math.isinf(10))
print(math.isinf(float('inf')))
print(math.isinf(float('-inf')))
Out:
False
True
True

🪴 Square root of each list item.

In:
nums = [9, 16, 25]
roots = [math.sqrt(n) for n in nums]
print(roots)
Out:
[3.0, 4.0, 5.0]

Mini Exercise 🏋️

You have daily sales growth factors: [1.01, 0.99, 1.02, 1.00].

  1. Get overall growth after 4 days
  2. Is it close enough to 1.02?

⭐📣 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 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...

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,...