0% found this document useful (0 votes)
16 views34 pages

Week 8 - Conditions

The document covers the fundamentals of logic statements and conditions in Python programming, explaining how to validate user input and execute code based on conditions using if, if-else, and if-elif statements. It also introduces logical operators, the importance of indentation, and provides examples of using the exit() function to handle invalid data. Additionally, it includes review questions and DIY activities to reinforce the concepts learned.

Uploaded by

fajer.abdulla2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views34 pages

Week 8 - Conditions

The document covers the fundamentals of logic statements and conditions in Python programming, explaining how to validate user input and execute code based on conditions using if, if-else, and if-elif statements. It also introduces logical operators, the importance of indentation, and provides examples of using the exit() function to handle invalid data. Additionally, it includes review questions and DIY activities to reinforce the concepts learned.

Uploaded by

fajer.abdulla2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Python and Cloud Fundamentals

Week 8: Logic statements (conditions)


Conditions
• Sometimes you need to specify different conditions in
your code where different outcomes will happen
depending on the true condition.
• For example, to check whether the user’s input is
proper or not.
• For example, if you ask the user to enter his/her
username and password. In this case, the program
should check if the username and password are
available or not.
• This process is called validating user entry by using a
condition to check the user input.
Conditions
• A condition in programming is comparing two
expressions to check whether they equal, not equal
or one expression is greater than or less than the
other expression.
• A condition is either True or False.
Logical Operators and Conditions
• In programming, Operator Description
we use logical == Equal
operators to create != Not equal
conditions. > Greater than
• The main operators >= Greater than or equal
we will use in < Less than
Python are == , > , <= Less than or equal
< , >= , <= and !=
Logical Operators and Conditions
• The logical operator is always places between two
expressions:
• Examples:
• 9 == 10  False condition
• 5 + 7 != 12  False condition
• 6>3  True condition
• “Ahmed” == “ahmed”  False condition
• 10 >= 9  True condition
• 8 <= 8  True condition
Conditions

Expression 1 Operator Expression 2

Example 5+7 == 12

variable > value

Example age >= 18

variable1 <= variable2

Example Total_sales <= profit


Logical Statement and Condition
• A statement includes condition is called logical
statement, and the output of any logical statement is
either True or False.
• Usually, logical statement starts with if, followed by a
condition, and ends with actions when the condition is
true.
• Example of logical statement:
“If temperature is 50, then do not go work”
• The condition is “temperature is 50”.
• The action is “do not go work” which will be applied
if statement:
if Statement
• In programming, logical statement is commonly called
"if statement".
• The if statement is used when you want to execute a
certain code in case something is true only.
• "if statement" is written by using the if keyword.
if Statement
• Syntax of if statement in Python:

if condition :

Code to run if the


condition is True

Code out of if
statement range
if Statement
• Example.

• The two print() lines below if statement will be


executed only if the age is grater than or equal 18.
• The last line will be executed all the time because it is
outside if statement range.
• Change the value of age to 17 and check the output.
if Statement … Cont’d
• Python relies on indentation (whitespace at the
beginning of a line) to define the scope of the code
under condition control. If you leave out this
indentation, then you will get an error.

indentation
Example: Python if Statement
# If the number is positive, we print a message

Output
Try to enter -1
Example: Python if statement
• In the previous example, num > 0 is the condition.
• The body of the “if statement” will be executed only if this
condition is True.
• When the variable num is equal to 3, the condition is True,
and the statement inside the body of “if statement” is
executed.
• When the variable num is equal to -1, the condition is False,
and the statement inside the body of is skipped.
• The print() statement outside of the “if statement” block is
executed regardless of the condition.
if-else statement:
if-else Statement
• What if you want to execute code if the condition is True and
execute another code if the condition is False?
• You can use ‘else’ with if statement.
• The else keyword will execute another code when the
condition is False.
• Example:
if-else Statement Example
If-else Statement … Cont’d
• Note that you can use one else only in each if
statement block. The code below is wrong
if...else Exercise
• Without writing the code,
• what is the output when num is 4?
• what is the output when num is 5?
The if-elif statement:
if-elif statement
• Sometimes, you want to test more than one condition.
In this case, you can use elif (short for else if) in if
statement block.
• The elif keyword means "if the previous condition is not
true, then try this condition".
Example: if-elif

Check the mark:


95
79
60
83
55
The if-elif-else Statement
if-elif-else Example 1
• We can use one else only in any if statement block.
• The else part must be written at end of the block.
• The else part does not include condition.
• When all conditions of if and elif are False, the else part
will be executed.
• We can do the previous example of marks using else. If
the mark is not A, B, C, or D, then it will be F.
if-elif-else Example 1

If the mark is less than


60, then else part will
work
Using exit() Function
• The exit() command is used to close the program at any
point.
• We can use exit() command to prevent program to
continue with wrong data entry.
• For example, in our marks code, if the user enters the
mark as 5000, it will print “Grade: A” because 5000 is
more than 90.
• One of the solutions is to use exit() command if the
mark is greater than 100.
• The exit() command could be written just exit() or with a
message such as exit(“invalid mark”)
exit() Example

Output
Using if for selection
Create a program that display the following list:
1- Area of Rectangle
2- Area of Triangle
3- Area of Circle
After that, ask the user to select a number by
writing the number 1, 2 or 3. If the user enters 1,
then ask to enter the length and width. If 2 is
selected, then ask to enter the base and height. If 3
is selected, then ask to enter the radius. For each
choice, calculate and display the area.
Review Questions
• What is the purpose of using if ?
• When should we use if-else?
• When should we use if-elif?
• How many statements will be as a result of if-
elif block?
• Can we use “==“ operator with strings?
• Can we use “>” and “<“ operators with strings?
Review Questions
• What is the output of this code if the user
enters 10?

num = input(“Guess the number: “)


if num == 10:
print(“You get the number”)
else: print(“Wrong number”)
DIY-Activity 1
• Ask the user to enter an amount of money.
Write code that will display “You have money”
if the number is positive, “You’re out”, if it’s
zero, and “You seem to be in debt”, if it’s less
than zero. Your code should have an if
statement, elif statement and an else.
DIY-Activity 2
• A copy center offer discount according to the number
of copies shown in the table below:
Copies < 50 < 100 <150 >= 150
Discount 0% 10% 15% 20%

• Ask the user to enter the number of copies. Write


code that will display “You will get …% discount”
according to the discount given in the table. For
example, if the user enters 120, then the program
should display “You will get 15% discount”.
DIY-Activity 3
Create a Python program that asks the user to enter the
temperature of the weather and display a proper message
according to the status shown in Table 1 below:
Temperature Weather Status

More than or equal 40 Very hot


More than or equal 30 Hot
More than or equal 15 Mild
Less than 15 Cold

For example, if the user enters 35 for the temperature,


then the program should display “Weather is hot”.

You might also like