Introduction To Computers Laboratory Manual: Experiment #4 Selections
Introduction To Computers Laboratory Manual: Experiment #4 Selections
Introduction To Computers Laboratory Manual: Experiment #4 Selections
Code Once
Anum Hameed
Introduction to Computers
Laboratory Manual
Experiment #4
Selections
Experiment #4: Selections
We can directly declare a variable and give it a Boolean value as the following example shows:
b1 = True
b2 = False
True and False are literals. They are reserved words and cannot be used as identifiers in a
program.
Also, we can declare a variable and give it the value using Boolean expressions. Boolean
expression is an expression that computes to True or false. The following example computes
the value of the Boolean expression and stores the value in the variable b:
radius = 1
b = radius > 0
Python provides six comparison operators, also known as relational operators, shown in the
table below, which can be used to compare two values and return a Boolean value.
2
Experiment #4: Selections
if Statement
A one-way if statement executes the statements if the condition is true. If the condition is false
nothing will be executed. The syntax for a one-way if statement is:
if boolean-expression:
statement(s) # Note that the statement(s) must be indented
The statement(s) must be indented at least one space to the right of the if keyword and each
statement must be indented using the same number of spaces.
The following flowchart illustrates how Python executes the syntax of an if statement.
3
Experiment #4: Selections
The following program computes the area of a circle only if the radius is positive.
radius = 10
if radius >= 0:
area = radius * radius * 3.14159
print("The area for the circle is", area)
if boolean-expression:
statement(s)-for-the-true-case
else:
statement(s)-for-the-false-case
The following program computes the area of a circle if the radius is positive, if the radius is
negative it prints "Negative input".
radius = 10
if radius >= 0:
area = radius * radius * 3.14159
print("The area for the circle is", area)
else:
print("Negative input")
4
Experiment #4: Selections
if boolean-expression1:
statements(1)
elif boolean-expression2:
statements(2)
elif boolean-expression3:
statements(3)
else :
statements(4)
a = 24
if a == 0:
print "a is zero"
elif a < 0:
print "a is negative"
else:
print "a is positive"
Logical Operators
The logical operators not, and, and or can be used to create a composite condition. Sometimes,
a combination of several conditions determines whether a statement is executed. You can use
logical operators to combine these conditions to form a compound expression. The following
table shows the logical operators in Python:
Operator Description
and If both the operands are true then condition becomes true
or If any of the two operands is true then condition becomes true
not Used to reverse the logical state
5
Experiment #4: Selections
e1 e2 e1 and e2 e1 or e2 not e1
True True True True False
True False False True False
False True False True True
False False False False True
Ex: Write a Python program that prompts the user to enter a number and checks whether the
number is in the period [0,10] or not. Then print the result.
Solution:
Conditional Expressions
The following statement assigns 1 to y if x is greater than 0, and -1 to y if x is less than or equal
to 0.
if x > 0:
y = 1
else:
y = -1
y = 1 if x > 0 else -1
Ex: Write a Python program that computes the absolute value of a number entered by the user.
6
Experiment #4: Selections
Lab Work
Ex1: Write a Python Program that checks whether a number (entered by user) is even or odd.
Solution:
Other solution:
Ex2: Write a program that requests from user to enter an integer and checks whether the
number is divisible by both 5 and 6, or neither of them, or just one of them.
7
Experiment #4: Selections
Ex3: Write a Python program that reads the month of birth from the user and prints the name
for that month.
Solution:
8
Experiment #4: Selections
Homework
1. What is the output of the following code in (a) and (b) if number is 30, then if number
is 35.
a)
if number % 2 == 0:
print(number, "is even.")
print(number, "is odd.")
b)
if number % 2 == 0:
print(number, "is even.")
else:
print(number, "is odd.")
2. Rewrite the following if statement using a conditional expression:
if age >= 16:
ticketPrice = 20
else:
ticketPrice = 10
3. Rewrite the following conditional expressions using if-else statements:
1. score = 3 * scale if x > 10 else 4 * scale
2. print(i if number % 3 == 0 else j)
4. Write a Python program that reads username and password from the user and prints
"successful login" if the username and password are correct, otherwise, it prints
"username or password is wrong".
Here is a sample run:
9
Experiment #4: Selections
5. Write a Python program that uses an if statement to find the smallest of three given
integers. (The user should enter the three numbers).
Good Luck
10