



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Welcome to this lecture on decision making in Python! Decision making is a crucial part of any program as it allows the program to make choices based on certain conditions. In Python, we have several ways to implement decision making, and in this lecture, we will learn about some of the most commonly used ones. If Statement The most basic form of decision making in Python is the if statement. It allows the program to execute a block of code only if a certain condition is met. Here's the syntax for an if statement: if condition : # code to be executed if condition is True Here's an example of an if statement in action: x = 10 if x > 5 : print ( "x is greater than 5" ) In this example, the condition is x > 5 , which is True , so the code block print("x is greater than 5") is executed. If…else Statement Sometimes, we want to execute a different block of code if the condition is not met. In such cases, we can use the if…else statement. Here's the syntax for an if…else statement: if condition: # code to be executed if condition is True else: # code to be executed if condition is False Here's an example of an if…else statement in action:
x = 10 if x > 5 : print ( "x is greater than 5" ) else : print ( "x is not greater than 5" ) In this example, the condition is x > 5 , which is True , so the code block print("x is greater than 5") is executed. If the value of x was less than or equal to 5, the code block print("x is not greater than 5") would have been executed. The elif Statement Sometimes, we want to check for multiple conditions and execute different code blocks based on which condition is met. In such cases, we can use the elif statement, which stands for "else if." Here's the syntax for using elif : if condition1:
elif condition2:
else:
Here's an example of using elif : x = 10 if x > 5:
Note that we can only use a single statement as the suite in an if , elif , or else clause if the statement is simple enough to fit on a single line. If we have a more complex code block that requires multiple lines, we need to use indentation to specify the code block as we did in the previous examples. Here's a complete example that demonstrates the use of decision making in Python using the if , if…else , and elif statements:
age = int(input("Enter your age: "))
if age < 18: print("You are a minor.") elif age < 65: print("You are an adult.") else: print("You are a senior citizen.") In this example, we ask the user for their age and store it in the age variable. Then, we use an if statement to check if the age is less than 18. If it is, we print a message saying "You are a minor." If the age is not less than 18, we use an elif statement to check if the age is less than 65. If it is, we print a message saying "You are an adult." If the age is not less than 65, we use an else clause to print a message saying "You are a senior citizen." I hope this example helps illustrate the use of decision making in Python. Let me know if you have any questions or need further clarification. Conclusion
In this lecture, we learned about decision making in Python using the if , if…else , and elif statements. We also learned about single statement suites, which allow us to use a single statement as the suite in an if , elif , or else clause. With this knowledge, you should now be able to use decision making in your Python programs to make choices based on certain conditions.