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

Conditional Statements

Uploaded by

Hari Sadugudu
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)
7 views

Conditional Statements

Uploaded by

Hari Sadugudu
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/ 8

289

11.9 Chapter Summary


Python provides the Boolean literals True and True.
False.
The bool() function returns either True or
When used in a conditional statement, all ob- False depending on whether its argument is
jects are equivalent to either True or False. equivalent to True or False.
Numeric values of zero, empty containers (for
example, empty strings and empty lists), The template for an if statement is
None, and False itself are considered to be if <test_expression>:
False. All other objects are considered to be <body>
290 CHAPTER 11. CONDITIONAL STATEMENTS

operator not is a unary operator that negates the


The body is executed only if the object returned value of its operand.
by the test expression is equivalent to True.
All comparison operators have higher prece-
if statements may have elif clauses and an dence than logical operators. not has higher
else clause. The template for a general condi- precedence than and, and and has higher
tional statement is precedence than or. Parentheses can be used to
change the order of precedence of these opera-
if <test_expression1>: tors. All math operators have higher precedence
<body1> than both comparison and logical operators.
elif <test_expression2>:
<body2> and and or use “shortcircuit” behavior. In
... # Arbitrary number expressions involving and or or, Python only
... # of elif clauses. evaluates as much as needed to determine the fi-
else: nal outcome. The return value is the object that
<bodyN> determines the outcome.
The body associated with the first test expres- The template for a while-loop is:
sion to return an object equivalent to True is
executed. No other body is executed. If none of while <test_expression>:
the test expressions returns an object equivalent <body>
to True, the body associated with the else The test expression is checked. If it is equiv-
clause, when present, is executed. The else alent to True, the body is executed. The test
clause is optional. expression is checked again and the process is
repeated.
The comparison, or relational, operators com-
pare the values of two operands and return A break statement terminates the current loop.
True if the implied relationship is true. The
comparison operators are: less than (<), less A continue statement causes the remainder
than or equal to (<=), greater than (>), greater of a loop’s body to be skipped in the current it-
than or equal to (>=), equal to (==), and not eration of the loop.
equal to (!=).
Both break and continue statements can be
The logical operators and and or take two used with either for-loops or while-loops.
operands. and produces a True value only
if both its operands are equivalent to True. The in operator returns True if the left
or produces a True value if either or both its operand is contained in the right operand and re-
operands are equivalent to True. The logical turns False otherwise.

11.10 Review Questions


1. Consider the following code. When prompted for input, the user enters the string SATURDAY.
What is the output?
day = input("What day is it? ")
day = day.lower()
11.10. REVIEW QUESTIONS 291

if day == ’saturday’ or day == ’sunday’:


print("Play!")
else:
print("Work.")

2. Consider the following code. When prompted for input, the user enters the string monday.
What is the output?
day = input("What day is it? ")
day = day.lower()
if day != ’saturday’ and day != ’sunday’:
print("Yep.")
else:
print("Nope.")

3. Consider the following code. What is the output?


values = [-3, 4, 7, 10, 2, 6, 15, -300]
wanted = []
for value in values:
if value > 3 and value < 10:
wanted.append(value)

print(wanted)

4. What is the output generated by the following code?


a = 5
b = 10
if a < b or a < 0 and b < 0:
print("Yes, it’s true.")
else:
print("No, it’s false.")

5. What is the value of x after the following code executes?


x = 2 * 4 - 8 == 0

(a) True
(b) False
(c) None of the above.
(d) This code produces an error.

6. What is the output generated by the following code?


292 CHAPTER 11. CONDITIONAL STATEMENTS

a = 5
b = -10
if a < b or a < 0 and b < 0:
print("Yes, it’s true.")
else:
print("No, it’s false.")

7. What is the output generated by the following code?


a = -5
b = -10
if (a < b or a < 0) and b < 0:
print("Yes, it’s true.")
else:
print("No, it’s false.")

8. What is the output produced by the following code?


a = [1, ’hi’, False, ’’, -1, [], 0]
for element in a:
if element:
print(’T’, end=" ")
else:
print(’F’, end=" ")

9. Consider the following conditional expression:


x > 10 and x < 30

Which of the following is equivalent to this?

(a) x > 10 and < 30


(b) 10 < x and 30 > x
(c) 10 > x and x > 30
(d) x <= 10 or x >= 30

10. To what value is c set by the following code?


a = -3
b = 5
c = a <= (b - 8)

(a) True
(b) False
11.10. REVIEW QUESTIONS 293

(c) This code produces an error.

11. What is the output produced by the following code?


def is_lower(ch):
return ’a’ <= ch and ch <= ’z’

print(is_lower("t"))

(a) True
(b) False
(c) None
(d) This code produces an error

12. What is the output produced by the following code?


def is_there(names, query):
for name in names:
if query == name:
return True

print(is_there([’Jake’, ’Jane’, ’Alice’], ’Tom’))

(a) True
(b) False
(c) None
(d) This code produces an error.

13. What output is produced by the following code?


def monotonic(xlist):
for i in range(len(xlist) - 1):
if xlist[i] < xlist[i + 1]:
return False
return True

data1 = [5, 3, 2, 2, 0]
data2 = [5, 2, 3, 2, 0]
print(monotonic(data1), monotonic(data2))

(a) True True


(b) True False
(c) False True
294 CHAPTER 11. CONDITIONAL STATEMENTS

(d) False False


(e) None of the above.

14. What output is produced by the following code?


def swapper(xlist):
for i in range(len(xlist) - 1):
if xlist[i] > xlist[i + 1]:
# Swap values.
xlist[i], xlist[i + 1] = xlist[i + 1], xlist[i]

data = [5, 3, 2, 2, 0]
swapper(data)
print(data)

15. What is the value of x after the following code executes?


y = 10
x = 2 * 4 - 8 or y

(a) True
(b) False
(c) None of the above.
(d) This code produces an error.

16. What is the value of x after the following code executes?


y = 10
if 2 * 4 - 8:
x = 2 * 4 - 8
else:
x = y

(a) True
(b) False
(c) 0
(d) 10

17. What is the value of x after the following code executes?


x = 4
while x > 0:
print(x)
x = x - 1
11.10. REVIEW QUESTIONS 295

(a) 4
(b) 1
(c) 0
(d) -1
(e) None of the above.

18. What is the value of x after the following code executes?


x = 4
while x == 0:
print(x)
x = x - 1

(a) 4
(b) 1
(c) 0
(d) -1
(e) None of the above.

19. What is the value returned by the function func1() when it is called in the following code?
def func1(xlist):
for x in xlist:
if x < 0:
return False
return True

func1([5, 2, -7, 7])

(a) True
(b) False
(c) None of the above.
(d) This code produces an error.

20. What is the value returned by the function func2() when it is called in the following code?
def func2(xlist):
for i in range(len(xlist) - 1):
if xlist[i] + xlist[i + 1] == 0:
return True
return False

func2([5, 2, -7, 7])


296 CHAPTER 11. CONDITIONAL STATEMENTS

(a) True
(b) False
(c) None of the above.
(d) This code produces an error.

ANSWERS: 1) Play!; 2) Yep.; 3) [4, 7, 6]; 4) Yes, it’s true.; 5) a; 6) No,


it’s false.; 7) Yes, it’s true.; 8) T T F F T F F; 9) b; 10) a; 11) a; 12) c; 13) b;
14) [3, 2, 2, 0, 5]; 15) c (it is 10); 16) d; 17) c; 18) a; 19) b; 20) a.

You might also like