Glad my code could make decisions


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

Today we talk about the real decision-makers in Python: conditionals

Quick request before we begin: If the newsletter helps you, I would love your feedback through the poll at end of this email. Feel free to reply to this email as well with any suggestions. You can read past issues here.

Conditionals control the flow of your code. They decide what happens based on whether the "condition" is true or false.

⛳ It all starts with a simple IF

In [1]:
age = 20
if age > 18:
    print("You can vote") # only prints if age is greater than 18
Out [1]: You can vote

✅ Double check if you don't agree.

In [2]:
age = 16
if age > 18:
    print("You can vote")
Out [2]:

After IFs there should be BUTs ELSE 😱

In [3]:
age = 16
if age >= 18:
    print("You can vote")
else:
    print("Too young to vote")
Out [3]: Too young to vote

Note: Conditions are executed in a sequence. When "IF" condition is correct then code inside "IF" is executed, otherwise its skipped all together and moves to "ELSE"

🤞 When one if isn’t enough, elif gives your code another chance

In [4]:
score = 75
# conditions checked sequentially, stops when any is satisfied
if score >= 90:
    print("Grade A")
elif score >= 75:
    print("Grade B")
else:
    print("Work harder")
Out [4]: Grade B

🪞 Two variables can also be compared directly (x vs y)

In [5]:
x = 10
y = 20
if x < y:
    print("x is less than y")
Out [5]: x is less than y

🏆 You can compare strings too

In [6]:
fruit = "apple"
# '=' is used twice to compare strings
if fruit == "apple":
    print("Fruit is apple")
Out [6]: Fruit is apple

🌪️ IF inside an IF (IFception 😁) - Nested IFs

In [7]:
x = 10
y = 20
if x < y:
    if y < 30:
        print("x is less than y and y is less than 30")
Out [7]: x is less than y and y is less than 30

💪 The bouncer outside the pub uses "AND" as both conditions need to hold

In [8]:
age = 22
has_id = True
if age >= 18 and has_id:
    print("Allowed to enter")
else:
    print("Not allowed to enter")
Out [8]: Allowed to enter

↔️ I am available for dates on any day ("OR" condition) of the weekend

In [9]:
day = "Saturday"
if day == "Saturday" or day == "Sunday":
    print("It's a weekend")
else:
    print("It's a weekday")
Out [9]: It's a weekend

⚔️ Two negatives make a positive (NOT is a negative condition)

In [10]:
is_raining = False
if not is_raining:
    print("You can go outside")
else:
    print("Take an umbrella")
Out [10]: You can go outside

In simple terms - NOT flips the answer — true becomes false, false becomes true.

🪣 Save condition output in a variable ("result")

In [11]:
score = 85
result = "Pass" if score >= 50 else "Fail"
print(result)
Out [11]: Pass

🥪 Check if something (x) is stuck between something (1 and 9)

In [12]:
x = 5
if x > 0 and x < 10:
    print("x is between 1 and 9")
Out [12]: x is between 1 and 9

Another way..

In [13]:
x = 15
if 10 < x <= 20:
    print("x is between 11 and 20")
Out [13]: x is between 11 and 20

🌧️ "IN" to check if an item exists in the list (can be used for dictionaries, tuples as well)

In [14]:
fruit = "apple"
if fruit in ["apple", "banana", "cherry"]:
    print("Fruit is in the list")
Out [14]: Fruit is in the list

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

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