Tiny symbols with huge power in Python


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

​

In past issues, you might have already met some operators β€” those tiny symbols (+, -, >) that feel intuitive. Today’s issue is your cheat sheet of the most important ones. Reply to this email if you need a handy pdf of all operators.

​

"Operators" are special symbols or keywords that tell Python to perform an action on values or variables.

​

πŸ”’ Arithmetic Operators

Some more math apart from basic addition, subtraction like modulus (%) and floor division (//)

​

In [1]:
a = 10
b = 3

print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Exponent:", a ** b)
print("Division:", a / b)
print("\n") # to add a new line
print("Floor division:", a // b) # Round to lower integer
print("Modulus:", a % b) # Remainder
Out [1]:
Addition: 13
Subtraction: 7
Multiplication: 30
Exponent: 1000
Division: 3.3333333333333335

Floor division: 3
Modulus: 1

Note: See how we used comma (,) to print multiple things

​

πŸͺž We all compare sadly - Comparison Operators

​

In [2]:
x = 5
y = 8

print(x == y) # Is x same as y
print(x != y) # Is x not equal to y
print(x < y) # Is x less than y
print(x >= y) # Is x greater than equal to y
Out [2]:
False
True
True
False

​

πŸ‘‰ Assignment operators - No they won't do your college assignments

Assign values and update variables efficiently.

Instead of writing count = count + 2, we can use (+=) to shorten it

​

In [3]:
count = 5
print(count)
count += 2 # count = count + 2 -> 7
print(count)
Out [3]:
5
7

​

And much more stuff efficiently..

In [4]:
count -= 1 # count = count - 1 -> 6
print(count)
count *= 3 # count = count * 3 -> 18
print(count)
count //= 4 # count = count // 4 -> 4
print(count)
Out [4]:
6
18
4

​

πŸ” Membership (doesn't get you into Costco) Operators - In, Not In

Check if something is present (in) or not present (not in) in a list, dictionary or any other collection

​

In [5]:
fruits = ["apple", "banana", "cherry"]

print("apple" in fruits)
print("orange" not in fruits)
Out [5]:
True
True

​

🧠 Logical Operators - And, Or, Not

To combine conditional statements that we covered yesterday​

​

In [6]:
age = 20
has_id = True

print(age > 18 and has_id) # True (both conditions True)
print(age < 18 or has_id) # True (one condition True)
print(not has_id) # False (negation)
Out [6]:
True
True
False

​

πŸͺͺ Identity Operators (Is, Is Not)

Two variables can have same values ( x = 2, y =2) but might point to different object in memory *think backend), Example - There can be many by name Peter Parker, but the spider man one would have a specific ID (lol)

  • is returns True if the variables refer to the exact same object.
  • is not returns True if they refer to different objects.

​

Run the below snippet yourself and see

In [7]:
a = [1, 2, 3]
b = a # b points to the same object as a
c = [1, 2, 3] # c is a different object with the same value

print(a is b) # True, because a and b refer to the same list object
print(a is c) # False, because a and c are different objects (even though values are equal)
print(a == c) # True, because the values inside the lists are equal

print(a is not c) # True, since they are not the same object
Out [7]:
True
False
True
True

​

β­πŸ“£ 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...