Manual 6 - Operators & Conditionals in Python
Manual 6 - Operators & Conditionals in Python
Lab Manual 6
Important Instructions
1. Every student should have lab manual in the lab; otherwise, there will be no evaluation and
attendance.
2. The lab manual should be printed on both sides of the paper. Color printing is not required.
3. Those students who have Laptop must bring it in the lab.
4. Students should read the manual before coming to the lab.
5. Every student will have to submit assignments individually. Assignments after due date
will not be accepted.
Experiment No. 6
Operators & Conditionals in Python
Learning objectives
By the end of this module, you'll be able to:
• Use if, else, and elif statements to execute code under various conditions.
• Use ‘and’ and ‘or’ operators to combine conditional logic and create more complex conditions.
Introduction
Booleans are a common type in Python. Their value can only ever be one of two things: true or false.
Understanding how to use Boolean values is critical, because you need them to write conditional logic.
In this lab, you'll use Boolean keywords and operators to write several types of conditional expressions and
determine the spacecraft's reaction.
Test expressions
You need to use an if statement to run code only if a certain condition is satisfied. The first thing you do when
you write an if statement is to check the condition by using a test expression. You then determine whether
the statement evaluates to True or False. If it's True, the next indented code block is run:
a = 97
b = 55
# test expression
if a < b:
# statement to be run
print(b)
In this example, a < b is the test expression. The program evaluates the test expression and then runs the code
within the if statement only if the test expression is True. If you evaluate the expression, you know that
it's False, so any code you write in the if statement won't be run.
Write if statements
You use an if statement if you want to run code only if a certain condition is satisfied. The syntax of
an if statement is always:
if test_expression:
# statement(s) to be run
For example:
a = 93
b = 27
if a >= b:
print(a)
Output: _______________
In Python, the body of an if statement must be indented. Any code following a test expression that isn't
indented will always be run:
a = 24
b = 44
if a <= 0:
print(a)
print(b)
Output: _______________
What if you also want your program to run a piece of code when your test expression is False? Or what if you
want to include another test expression? Python has other keywords you can use to make more
complex if statements, else and elif. When you use if, else, and elif in combination, you can write complex
programs with multiple test expressions and statements to run.
In this example, if a isn't greater than or equal to b, nothing happens. Let's say you want to instead print b if
the test expression is False:
a = 27
b = 93
if a >= b:
print(a)
else:
print(b)
Output: ____________________
If the test expression is False, the code in the body of the if statement is skipped, and the program continues
running from the else statement. The syntax of an if/else statement is always:
if test_expression:
# statement(s) to be run
else:
# statement(s) to be run
Output: _____________________________________________________________
In this variation, the elif statement in the block of code won't run, because the if statement is True.
if test_expression:
# statement(s) to be run
elif test_expression:
# statement(s) to be run
a = 27
b = 93
if a < b:
print("a is less than b")
elif a > b:
print("a is greater than b")
else:
print ("a is equal to b")
Output: _________________________________________
A code block that uses all three types of statements has the following syntax:
if test_expression:
# statement(s) to be run
elif test_expression:
# statement(s) to be run
elif test_expression:
# statement(s) to be run
else:
# statement(s) to be run
Output: ___________________________________________________________________
In above piece of code, change the values of variables a, b, c as shown below and observe the output:
a, b, c = 25, 26, 27
Output: ___________________________________________________________________
Again, change the values of variables a, b, c as shown below and observe the output:
a, b, c = 27, 26, 25
Output: ___________________________________________________________________
Nested conditional logic follows the same rules as regular conditional logic within each code block. Here's
one example of the syntax:
if test_expression:
# statement(s) to be run
if test_expression:
# statement(s) to be run
else:
# statement(s) to be run
elif test_expression:
# statement(s) to be run
if test_expression:
# statement(s) to be run
else:
# statement(s) to be run
else:
# statement(s) to be run
You will start your project by creating the code to determine if a piece of space debris is of a dangerous size.
For this exercise we will use an arbitrary size of 5 meters cubed (5m3); anything larger is a potentially
dangerous object.
In the cell below write a code to add a variable named object_size and set it to 10 to represent 10m3. Then
add an if statement to test if object_size is greater than 5. If it is, display a message saying We need to keep
an eye on this object. Otherwise, display a message saying Object poses no threat.
Output: ________________________________________________________
You might occasionally want to combine test expressions to evaluate multiple conditions in one if, elif,
or else statement. In this case, you'd use the Boolean operators and and or.
The or operator
You can connect two Boolean, or test, expressions by using the Boolean or operator. For the entire expression
to evaluate to True, at least one of the subexpressions must be true. If none of the subexpressions is true, the
whole expression evaluates to False. For example, in the following expression, the entire test expression
evaluates to True, because one of the conditions in the subexpressions has been met:
a = 23
b = 34
if a == 34 or b == 34:
print(a + b)
If both subexpressions are true, the entire test expression also evaluates to True. A Boolean expression that
uses or has the following syntax: sub-expression1 or sub-expression2
A Boolean expression that uses and has the following syntax: sub-expression1 and sub-expression2
In the previous exercise you created code to test the size of the object. Now you will test both the object's size
and proximity. You will use the same threshold for size of 5m3. If the object is both larger than the threshold
and within 1000km of the ship, evasive manoeuvres will be required.
Add the code to the cell below to create two variables: object_size and proximity. Set object_size to 10 to
represent 10m3. Set proximity to 9000. Then add the code to display a message saying “Evasive manoeuvres
required” if both object_size is greater than 5 and proximity is less than 1000. Otherwise display a message
saying, “Object poses no threat”.
Output: ___________________________________________
a) A test expression that uses the and operator evaluates to True when both subexpressions are true.
b) A test expression that uses the and operator evaluates to True when one subexpression is true and
the other is false.
c) A test expression that uses the and operator evaluates to True when both subexpressions are false.
Summary
By using test expressions, otherwise known as Boolean expressions, you can conditionally run Python code.
They're commonly used in Python to "make decisions" about what should happen next while a program is
running. In the earlier sections, you wrote test expressions as part of if, else, and elif statements, and you
further fine-tuned your conditions by using the “and” and ‘or’ operators.
Boolean expressions and statements are a key part of creating complex Python programs, where you want
your code to run only under certain conditions.
Here are some of the essentials you now know about conditional logic:
• Booleans can have one of two values, True or False.
• if, else, and elif statements can be used to run code under specific conditions.
• “and” and “or” operators can be used to combine test expressions and create more complex
conditions.
With these tools, you should feel ready to write more complex code blocks in Python.
Task # 1: Write a program that accepts two values from the user and compares both values. The program
displays “Both are equal” if the values are qual, or “The first value is greater than second” or “The second
value is greater than the first” if user has entered so.
Task # 2: Write a Python code to accept marks of a student from 1-100 and display the grade according to the
following formula.
Grade F if marks are less than 50, Grade E if marks are between 50 to 60, Grade D if marks are between 61
to 70, Grade C if marks are between 71 to 80, Grade B if marks are between 81 to 90,
and Grade A if marks are between 91 to 100.
Task # 3: Write a Python code to accept temperature value from user (in centigrade) and display an appropriate
message as below.
FREEZING if temperature is less than 0, COLD if temperature is between 0 to 15, WARM if temperature is
between 16 to 30, HOT if temperature is between 31 to 40, VERY HOT if temperature is greater than 40.