0% found this document useful (0 votes)
6 views

Manual 6 - Operators & Conditionals in Python

Here is the code to check if a piece of space debris is larger than 5m3: size = int(input("Enter the size of debris in meters cubed: ")) if size > 5: print("Debris is larger than 5m3 and may be dangerous") else: print("Debris is smaller than 5m3 and is not dangerous") Exercise: Add proximity check Now add an additional check to see if the debris will come within 1 km of the spacecraft. If both conditions are true (size > 5m3 and proximity < 1km), print a warning message.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Manual 6 - Operators & Conditionals in Python

Here is the code to check if a piece of space debris is larger than 5m3: size = int(input("Enter the size of debris in meters cubed: ")) if size > 5: print("Debris is larger than 5m3 and may be dangerous") else: print("Debris is smaller than 5m3 and is not dangerous") Exercise: Add proximity check Now add an additional check to see if the debris will come within 1 km of the spacecraft. If both conditions are true (size > 5m3 and proximity < 1km), print a warning message.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Department of Avionics Engineering

Introduction to Information Technology

Lab Manual 6

Engr. Hafiz Ali Ahmad


Lecturer

Student Name: ___________________________


Registration Number: ___________________________
Section: ___________________________
Deadline: ___________________________
Date of Submission: ___________________________

Institute of Space Technology, Islamabad


Introduction to Information Technology Session: 2023 (AVE-9)

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.

Department of Avionics Engineering,


Institute of Space Technology, Islamabad 2|Page
Introduction to Information Technology Session: 2023 (AVE-9)

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.

Scenario: Print warning messages


Suppose you're creating a program that identifies if a piece of space debris is a danger to a spacecraft. If the
object is larger than a specified size and will come within range of the spacecraft, a manoeuvre needs to be
performed to avoid a collision. Otherwise, the spacecraft can stay on course.

In this lab, you'll use Boolean keywords and operators to write several types of conditional expressions and
determine the spacecraft's reaction.

Write 'if' statements


To express conditional logic in Python, you use if statements. When you're writing an if statement, you're
relying on another concept we cover in this module, mathematical operators. Python supports the common
logic operators from math: equals, not equals, less than, less than or equal to, greater than, and greater
than or equal to. You're probably used to seeing these operators displayed using symbols, which is how
they’re represented in Python, too.
• Equals: a == b
• Not Equals: a != b
• Less than: a < b
• Less than or equal to: a <= b
• Greater than: a > b
• Greater than or equal to: a >= b

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)

Department of Avionics Engineering,


Institute of Space Technology, Islamabad 3|Page
Introduction to Information Technology Session: 2023 (AVE-9)

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.

Note: In Python, None and 0 are also interpreted as False.

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 are 'else' and 'elif' statements?

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.

Work with else


You know that when you use an if statement, the body of the program will run only if the test expression
is True. To add more code that will run when your test expression is False, you need to add an else statement.
Let's adapt an example from the previous section:
a = 27
b = 93
if a >= b:
print(a)

Department of Avionics Engineering,


Institute of Space Technology, Islamabad 4|Page
Introduction to Information Technology Session: 2023 (AVE-9)

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

Work with elif


In Python, the keyword elif is short for else if. Using elif statements enables you to add multiple test
expressions to your program. These statements run in the order in which they're written, so your program will
enter an elif statement only if the first if statement is False. For example:
a = 27
b = 93
if a <= b:
print("a is less than or equal to b")
elif a == b:
print("a is equal to b")

Output: _____________________________________________________________

In this variation, the elif statement in the block of code won't run, because the if statement is True.

The syntax of an if/elif statement is:

if test_expression:
# statement(s) to be run
elif test_expression:
# statement(s) to be run

Combine if, elif, and else statements


You can combine if, elif, and else statements to create programs with complex conditional logic. Remember
that an elif statement is run only when the if condition is false. Also note that an if block can have only
one else block, but it can have multiple elif blocks.

Let's look at an example with an if, an elif and an else statement:

Department of Avionics Engineering,


Institute of Space Technology, Islamabad 5|Page
Introduction to Information Technology Session: 2023 (AVE-9)

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

Work with nested conditional logic


Python also supports nested conditional logic, meaning that you can nest if, elif, and else statements to create
even more complex programs. To nest conditions, indent the inner conditions, and everything at the same
level of indentation will be run in the same code block:
a, b, c = 26, 25, 27
if a > b:
if b > c:
print ("a is greater than b and b is greater than c")
else:
print ("a is greater than b and less than c")
elif a == b:
print ("a is equal to b")
else:
print ("a is less than b")

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: ___________________________________________________________________

Department of Avionics Engineering,


Institute of Space Technology, Islamabad 6|Page
Introduction to Information Technology Session: 2023 (AVE-9)

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

Exercise: Use logic to examine an object's size

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: ________________________________________________________

Department of Avionics Engineering,


Institute of Space Technology, Islamabad 7|Page
Introduction to Information Technology Session: 2023 (AVE-9)

What are 'and' and 'or' operators?

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

The and operator


You can also connect two test expressions by using the Boolean and operator. Both conditions in the test
expression must be true for the entire test expression to evaluate to True. In any other case, the test expression
is False. In the following example, the entire test expression evaluates to False, because only one of the
conditions in the subexpressions is true:
a = 23
b = 34
if a == 34 and b == 34:
print (a + b)

A Boolean expression that uses and has the following syntax: sub-expression1 and sub-expression2

The difference between and and or


To highlight the difference between the two Boolean operators, you can use a truth table. A truth table shows
you what the entire test expression evaluates to based on the two subexpressions.

Here's the truth table for and:

subexpression1 Operator subexpression2 Result


True and True True

True and False False

False and True False

False and False False

Department of Avionics Engineering,


Institute of Space Technology, Islamabad 8|Page
Introduction to Information Technology Session: 2023 (AVE-9)

Here's the truth table for or:

subexpression1 Operator subexpression2 Result


True or True True

True or False True

False or True True

False or False False

Exercise: Use complex logic to determine possible evasive maneuvers

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: ___________________________________________

Department of Avionics Engineering,


Institute of Space Technology, Islamabad 9|Page
Introduction to Information Technology Session: 2023 (AVE-9)

Check your knowledge


1. Under what conditions does a test expression that uses the ‘and’ operator evaluate to True?

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.

2. What is the keyword elif short for in Python?


a) else if
b) only if
c) else

3. What values in a test expression are always interpreted as false?


a) Any non-zero values
b) Empty
c) None and 0

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.

Department of Avionics Engineering,


Institute of Space Technology, Islamabad 10 | P a g e
Introduction to Information Technology Session: 2023 (AVE-9)

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.

Department of Avionics Engineering,


Institute of Space Technology, Islamabad 11 | P a g e
Introduction to Information Technology Session: 2023 (AVE-9)

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.

Department of Avionics Engineering,


Institute of Space Technology, Islamabad 12 | P a g e

You might also like