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 OperatorsSome 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 assignmentsAssign 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 InCheck 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, NotTo 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)
β 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 πΌ
|
Beginner to Expert in Python in just 5 minutes
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...