Part 4: Conditional Statements, Truthy and Falsy Values


20. Condtional Statements - Part 1

Lecture Video
Code Snippet
age = 10
country = "India"

if (age >= 18) and (country == "India"):
    print("Person is eligible to vote")
else:
    print("Person is not eligible to vote")

print("Verification is completed")


print("-----if-elif-else : Student grades example - version 1----")
marks = 65
if marks >= 80:
    print("A grade")
elif 60 <= marks < 80:
    print("B grade")
elif 40 <= marks < 60:
    print("C grade")
else:
    print("F grade")

print("-----if-elif-else : Student grades example - version 2----")
marks = 65
if marks >= 80:
    print("A grade")
elif marks >= 60:
    print("B grade")
elif marks >= 40:
    print("C grade")
else:
    print("F grade")

21. Condtional Statements - Part 2

Lecture Video
Code Snippet
print("----Nested if statements example: Super market discount----")
age = 70
has_membership = True
cost_price = 1000.0
if age>=60:
    if has_membership:
        print("30% discount is applied")
        final_price = cost_price - (0.3 * cost_price)
    else:
        print("20% discount is applied")
        final_price = cost_price - (0.2 * cost_price)
else:
    if has_membership:
        print("10% discount is applied")
        final_price = cost_price - (0.1 * cost_price)
    else:
        print("0% discount is applied")
        final_price = cost_price

print("Final price is:", final_price)


print("----Ternary conditional statement example----")
score = 30
if score >= 40:
    result = "Pass"
else:
    result = "Fail"

result = "Pass" if score>=40 else "Fail"
print("Result:", result)
print("Pass" if score>=40 else "Fail")

Practice Problem: Determine if Two Queens attack each other on a Chessboard

Lecture Video
Code Snippet
# Queen 1 position
r1, c1 = 5,4

# Queen 2 position
r2, c2 = 2,6

if r1==r2 or c1==c2 or abs(r2-r1)==abs(c2-c1):
    print("The queens attack each other")
else:
    print("The queens dont attack each other")

22. Truthy and Falsy Values - Part 1

Lecture Video
Code Snippet
#Truthy/Falsy Examples
print("Truthy/Falsy examples")
print(bool(False))
print(bool(25))
print(bool("Sachin"))
print(bool(0))
print(bool([]))
print(bool([1,2,3]))
print(bool(""))
print(bool(" "))

#Example 1
#name can only be None, empty string, or a valid non-empty string
#Invalid -> None, empty string("")
#Valid   -> non-empty string
print("Example 1 - Traditional way")
name = None
if name != None and name != "":
    print("Welcome "+ name)
else:
    print("Welcome Guest")

print("Example 1 - Pythonic way")
name = None
if name:
    print("Welcome "+name)
else:
    print("Welcome Guest")

23. Truthy and Falsy Values - Part 2

Lecture Video
Code Snippet
#Example 2
#quantity_available can be None, 0, >0
print("Example 2 - Traditional way")
quantity_available = 0
if quantity_available != None and quantity_available != 0:
    print("Item available")
else:
    print("Item unavailable")

print("Example 2 - Pythonic way")
quantity_available = 10
if quantity_available:
    print("Item available")
else:
    print("Item unavailable")

#Example 3
#Purchase list could be None or empty list or valid non-empty list
print("Example 3 - Traditional way")
purchase_list = ["Book", "Pen", "Clock"]
if purchase_list != None and len(purchase_list)>0:
    print("Items purchased: ", purchase_list)
else:
    print("No items are purchased")

print("Example 3 - Pythonic way")
purchase_list = ["Book", "Pen", "Clock"]
if purchase_list:
    print("Items purchased: ", purchase_list)
else:
    print("No items are purchased")

if not purchase_list:
    print("No items are purchased")
else:
    print("Items purchased: ", purchase_list)

#Using not
print("Using not")
a = 10
print(not a)
b = []
print(not b)

24. Short-Circuiting with Truthy/Falsy Values

Lecture Video
Code Snippet
#Short-circuiting with or examples
print("Short-circuiting with or examples")
a,b = 0,10
c = False
d = "Hello"
e = []

print(a or b)
print(a or d or e)
print(a or c or e)
print(a or c or d)

print(not(a or c or e))
print(not(a or c or d))

#Short-circuiting with and examples
print("Short-circuiting with and examples")
a,b = 0,10
c = True
d = "Hello"
e = []
print(a and b)
print(b and c and d)
print(b and c and e)
print(b and e and d)

#if statement example - Checking if employee details are valid
print("if statement example")
name = None
salary = 50000 #invalid - none or 0  ::  valid - greater than 0
profession = "Engineer"

if name and salary and profession:
    print("Employee details are valid")
else:
    print("Employee details are invalid")

25. Safe Conditional Checks and Fallback Pattern

Lecture Video
Code Snippet
# Assume a student is applying for a course
# student_details:
#             invalid - None or Empty List []
#             valid - Non-empty list with [name, age, percentage]
#                     e.g ["Sachin", 22, 85]
# Student is eligible if age <= 25 and percentage >= 75

print("Safe conditional checks using short-circuiting")
student_details = ["Sachin", 30, 85]
if student_details and student_details[1] <= 25 and student_details[2] >= 75:
    print("You are eligible to apply for the course")
else:
    print("You are not eligible to apply for the course")

#Fallback pattern example - Pythonic way
print("Fallback pattern example - Pythonic way")
name = "Sachin"
display_name = name or "Guest"
print("Welcome "+display_name)

print("Fallback pattern example - Traditional way")
name = ""
if name:
    display_name = name
else:
    display_name = "Guest"