Docsity

Decision Making in Python Lecture 4, Lecture notes of Object Oriented Programming

Toggle sidebar
bg1
Decision Making in Python Lecture_4
Page 1 of 5
Prepared by Engr. Bilal Arif [Registered Electrical Engineer]
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:
pf3
pf4
pf5

Partial preview of the text

Download Decision Making in Python Lecture 4 and more Lecture notes Object Oriented Programming in PDF only on Docsity!

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:

code to be executed if condition1 is True

elif condition2:

code to be executed if condition1 is False and condition2 is True

else:

code to be executed if condition1 and condition2 are False

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:

Example program to demonstrate decision making in Python

Prompt the user for their age

age = int(input("Enter your age: "))

Check the age and print a message based on the 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.