Imagine you're a security guard at a museum, and your job is to check the visitors' bags before they enter the exhibition. You have a set of rules that you must follow to determine whether a bag is allowed in or not:
If the bag is empty, it's allowed in.
If the bag contains only food and drinks, it's allowed in.
If the bag contains a camera or a phone, it's not allowed in.
If the bag contains any other items, it's up to your discretion.
To implement these rules in Python, you could use nested if statements like this:
bag = ['phone', 'wallet', 'water bottle']
if len(bag) == 0:
print("This bag is allowed in.")
else:
if 'camera' in bag or 'phone' in bag:
print("Sorry, no cameras or phones allowed.")
elif all(item in ['food', 'drink'] for item in bag):
print("This bag is allowed in for consumption.")
else:
print("This bag may not be allowed in.")
In this example, the outer if statement checks if the bag is empty. If it's not empty, the inner if statement checks if it contains a camera or a phone. If it does, the bag is not allowed in. If it doesn't, the elif statement checks if the bag contains only food and drinks. If it does, the bag is allowed in for consumption. If it contains any other items, the final else statement is triggered and the decision is left up to the security guard's discretion.
Tips and tricks:
Use boolean operators in conditions: You can use and, or, and not operators to combine multiple conditions in an if or elif statement. For example:
if age >= 18 and citizenship == 'USA':
print("You can vote in the US!")
Use nested if statements: You can nest if statements inside other if statements to create more complex conditions. For example:
if age >= 18:
if citizenship == 'USA':
print("You can vote in the US!")
else:
print("You can't vote in the US.")
else:
print("You're not old enough to vote.")
Use the ternary operator: You can use the ternary operator x if condition else y to write concise if/else statements. For example:
x = 5
y = 10
z = x if x > y else y
print(z) # Output: 10
Use in and not in operators: You can use the in and not in operators to check if an element is present in a list, tuple, or string. For example:
fruits = ['apple', 'banana', 'orange']
if 'banana' in fruits:
print("I love bananas!")
Use pass statement: You can use the pass statement as a placeholder for code that you haven't written yet. For example:
age = int(input("Enter your age: "))
if age < 18:
print("You're too young.")
else:
pass # Code to handle older users goes here
Use assert statement: You can use the assert statement to check if a condition is true, and raise an error if it's not. For example:
age = 15
assert age >= 18, "You must be at least 18 to vote."
print("You can vote!")
Use elif instead of multiple if statements: Instead of writing multiple if statements with similar conditions, you can use elif to make your code more concise. For example:
grade = 85
if grade >= 90:
print("A")
elif grade >= 80:
print("B")
elif grade >= 70:
print("C")
elif grade >= 60:
print("D")
else:
print("F")
YOU ARE READING
Python Programming for Lazy Beginners: A Simple and Easy Tutorial
Non-FictionPython Programming for Lazy Beginners: A Simple and Easy Tutorial is a comprehensive guidebook for anyone who wants to learn the basics of programming using the popular Python language. This book is specifically designed for beginners who have no pr...
CHAPTER 5
Start from the beginning
