0% found this document useful (0 votes)
20 views19 pages

Python_4_023216

Uploaded by

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

Python_4_023216

Uploaded by

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

Python

Selection Statements

Wadhah S. Sebti
Selection
Selection: if and if-else Statements
In this section, we explore several types of selection statements, or
control statements, that allow a computer to make choices.

Before you can test conditions in a Python program, you need to


understand the Boolean data type. The Boolean data type consists of
only two data values—true and false. In Python, Boolean literals can be
written in several ways, but most programmers prefer to use the
standard values True and False.
Selection
Selection: if and if-else Statements
Simple Boolean expressions consist of the Boolean values True or
False, variables bound to those values, function calls that return
Boolean values, or comparisons. The condition in a selection statement
often takes the form of a comparison.
Selection
Selection: if and if-else Statements
Selection
The following session shows some example comparisons and their values:
>>> 4 == 4
True
>>> 4 != 4
False Note that == means equals,
>>> 4 < 5 whereas = means assignment.
True
>>> 4 >= 3
True
>>> "A" < "B"
True
Selection
One-Way Selection Statements
The simplest form of selection is the if statement. This type of control
statement is also called a one-way selection statement, because it
consists of a condition and just a single sequence of statements.
if Statement
Here is the syntax for the if statement:
if <condition>:
<sequence of statements>
>>> if x < 0:
x = –x
Selection
if-else Statements

The if-else statement is the most common type of selection statement. It


is also called a two-way selection statement, because it directs the
computer to make a choice between two alternative courses of action.
Selection
if-else Statements
Our next example prints the maximum and minimum of two input numbers.
first = 10
second = 20
if first > second:
maximum = first
minimum = second
else:
maximum = second
minimum = first
Selection
if-else Statements
print("Maximum:", maximum)
print("Minimum:", minimum)
Selection
Multi-Way if Statements
a program is faced with testing several conditions that entail more than two
alternative courses of action.
The syntax of the multi-way if statement is the following:
if <condition-1>:
<sequence of statements-1>
elif <condition-n>:
<sequence of statements-n>
else:
<default sequence of statements>
Selection
Multi-Way if Statements

The process of testing several conditions and responding accordingly can be


described in code by a multi-way selection statement. Here is a short
Python script that uses such a statement to determine and print the letter
grade corresponding to an input numeric grade:
Selection
Multi-Way if Statements
number = int(input("Enter the numeric grade: "))
if number > 89:
letter = 'A'
elif number > 79:
letter = 'B'
elif number > 69:
letter = 'C'
else:
letter = 'F'
print("The letter grade is", letter)
Selection
Logical Operators and Compound Boolean Expressions
The two conditions can be combined in a Boolean expression that uses the
logical operator.
Selection
Logical Operators and Compound Boolean Expressions
Selection
Logical Operators and Compound Boolean Expressions
The next example verifies some of the claims made in the truth tables in Figure 3-5:
>>> A = True
>>> B = False
>>> A and B
False
>>> A or B
True
>>> not A
False
Selection
Logical Operators and Compound Boolean Expressions
The next code segment accepts only valid inputs for our grade conversion
script and displays an error message otherwise:
number = int(input("Enter the numeric grade: "))
if number > 100:
print("Error: grade must be between 100 and 0")
elif number < 0:
print("Error: grade must be between 100 and 0")
else:
# The code to compute and print the result goes here
Selection
Logical Operators and Compound Boolean Expressions
The two conditions can be combined in a Boolean expression that uses
the logical operator or. The resulting compound Boolean expression
simplifies the code somewhat, as follows:
number = int(input("Enter the numeric grade: "))
if number > 100 or number < 0:
print("Error: grade must be between 100 and 0")
else:
print(“Your grade is: “,number)
Selection
Logical Operators and Compound Boolean Expressions

Yet another way to describe this situation is to say that if the number is
greater than or equal to 0 and less than or equal to 100, then we want the
program to perform the computations and output the result; otherwise, it
should output an error message. The logical operator and can be used to
construct a different compound Boolean expression to express this logic:
Selection
Logical Operators and Compound Boolean Expressions

number = int(input("Enter the numeric grade: "))

if number >= 0 and number <= 100:

print(“Yes: the grade is OK")

else:

print("Error: grade must be between 100 and 0")

You might also like