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

Python Chapter4

The document discusses Python programming concepts like conditional statements, logical operators, and conditional expressions. It provides examples of using if, else, elif statements to make decisions in a program based on conditions. Random number generation and comparisons between values are also covered.

Uploaded by

Mohamed Mashaleh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
22 views19 pages

Python Chapter4

The document discusses Python programming concepts like conditional statements, logical operators, and conditional expressions. It provides examples of using if, else, elif statements to make decisions in a program based on conditions. Random number generation and comparisons between values are also covered.

Uploaded by

Mohamed Mashaleh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 19

Chapters 4

Selections
Introduction
 A program can decide which statements to execute based on
a condition.
 If you enter a negative value for radius the program displays
an invalid result. If the radius is negative, the program cannot
compute the area. How can you deal with this situation?

if radius<0:
print("Incorrect Input")
else:
area = radius * radius * math.pi
# Display results
print("The area is ", area)

Enter a value for radius: -5


Incorrect Input
Boolean Types, Values, and
Expressions
 A Boolean expression is an expression that evaluates to a
Boolean value True or False.
 Python provides six comparison operators (also known as
relational operators), which an be used to compare two
values.
Boolean Types, Values, and
Expressions
 The result of the comparison is a Boolean value: True or
False.
>>> radius = 1
>>> print( radius > 0)
True

A variable that holds a Boolean value is known as a Boolean


variable.
The Boolean data type is used to represent Boolean values.
A Boolean variable can hold one of the two values: True or
False.
Internally, Python uses 1 to represent True and 0 for False.
Boolean Types, Values, and
Expressions
 Internally, Python uses 1 to represent True and 0 for False.
 You can use the int function to convert a Boolean value to an
integer.
>>> print(int(True))
1
>>> print(int(False))
0
 You can also use the bool function to convert a numeric value to a
Boolean value. The function returns False if the value is 0;
otherwise, it always returns True.
>>> print(bool(0))
False
>>> print(bool(4))
True
Generating Random Numbers
 The randint(a, b) function can be used to generate a random
integer between a and b, inclusively.
 Ex: develop a program to help a first grader practice addition. The
program randomly generates two single-digit integers, number1
and number2, and displays to the student a question such as
What is 1 + 7. After the student types the answer, the program
displays a message to indicate whether it is true or false.
 The program may be set up to work as follows:
 Step 1: Generate two single-digit integers for number1 (e.g., 4)
and number2 (e.g., 5)
 Step 2: Prompt the student to answer, "What is 4 + 5?"
 Step 3: Check whether the student’s answer is correct.
Generating Random Numbers
The program uses the randint function
defined in the random module

What is 1 + 4? 5
1 + 4 = 5 is True random() function to generate a
>>>
What is 7 + 9? 15
random float r such that 0 <= r < 1.0
7 + 9 = 15 is False
if Statements
 A one-way if statement executes the statements if the condition
is true.
 Python has several types of selection statements:
 one-way if statements,
 two-way if else statements,
 nested if statements,
 multi-way if-elif-else statements and conditional expressions.
 The syntax for a one-way if statement is:

if boolean-expression:
statement(s)

# Note that the statement(s) must be indented


if Statements
 Compute Area example:
if radius >= 0:
area = radius * radius * math.pi
print("The area for the circle of radius", radius,
"is", area)
if Statement
 Write a program that prompts the user to enter an integer. If
the number is a multiple of 5, the program displays the result
HiFive. If the number is divisible by 2, the program displays
HiEven.

Enter an integer: 8
HiEven
>>>
Enter an integer: 10
HiFive
HiEven
Two-Way if-else Statements
 A two-way if-else statement decides which statements to execute
based on whether the condition is true or false.
 Here is the syntax for a two-way if-else statement:

if boolean-expression:
statement(s)-for-the-true-case
else:
statement(s)-for-the-false-case
Two-Way if-else Statements
 develop a program for a first grader to practice subtraction.
Nested if and Multi-Way if-elif-else
statements
 One if statement can be placed inside another if statement
to form a nested if statement.
 There is no limit to the depth of the nesting.
if score >= 90.0: if score >= 90.0:
grade = 'A' grade = 'A'
else: elif score >= 80.0:
if score >= 80.0:
grade = 'B'
grade = 'B'
else:
elif score >= 70.0:
if score >= 70.0: grade = 'C'
grade = 'C' elif score >= 60.0:
else: grade = 'D'
if score >= 60.0: else:
grade = 'D' grade = 'F'
else:
grade = 'F' This is better
Nested if and Multi-Way if-elif-else
statements
 write a program to find out the Chinese zodiac sign for a
given year. The Chinese zodiac sign is based on a 12-year
cycle, and each year in this cycle is represented by an animal
— monkey, rooster, dog, pig, rat, ox, tiger, rabbit, dragon,
snake, horse, and sheep
Logical Operators
 The logical operators not, and, and or can be used to create
a composite condition.
 Ex: checks whether a number is divisible by 2 and 3, by 2 or 3,
and by 2 or 3 but not both.

Enter an integer: 9 Enter an integer: 18


9 is divisible by 2 or 3 18 is divisible by 2 and 3
9 is divisible by 2 or 3, but not both 18 is divisible by 2 or 3
Conditional Expressions
 A conditional expression evaluates an expression based on a
condition.
 Conditional expressions are in a completely different style.
The syntax is:

expression1 if boolean-expression else expression2

 The result of this conditional expression is expression1 if


boolean-expression is true; otherwise, the result is
expression2.
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 y = 1 if x > 0 else -1
else:
y = -1

 The following statement displays the message number is even


if number is even, and otherwise displays number is odd.

print("number is even" if number % 2 == 0 else "number is odd")


Operator Precedence and Associativity

 Operator precedence and associativity determine the order


in which operators are evaluated.
Assignment 4
Q1: Suppose you shop for rice and find it in two different sized
packages. You would like to write a program to compare the
costs of the packages. The program prompts the user to enter
the weight and price of each package and then displays the
one with the better price. Here is a sample run:
Enter weight and price for package 1: 25, 11.99
Enter weight and price for package 2: 50, 24.59
Package 1 has the better price.

Q2: Write a program that displays a random uppercase letter.


Q3: Write a program that prompts the user to enter an integer
between 0 and 15 and displays its corresponding hex number.
Q4: Write a program that prompts the user to enter a hex
character and displays its corresponding decimal integer.

You might also like