python ch 3

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

What is a single alternative decision structure?

A decision structure that provides a single alternative path of execution. If the condition that is being tested is true, the program takes the alternative path.

How does a dual alternative decision structure work?

A dual alternative decision structure has two possible paths of execution; one path is taken if a condition is true, and the other path is taken if the condition is false.

What is a control structure?

A logical design that controls the order in which a set of statements execute

What is a flag variable?

A variable that signals when some condition exists in the program

What is a Boolean expression

An expression that can be evaluated as either true or false

What would the following code display? s1 = 'New York' s2 = 'Boston' if s1 > s2: print(s2) print(s1) else: print(s1) print(s2)

Boston New York

The following truth table shows various combinations of the values true and false connected by a logical operator. Complete the table by circling T or F to indicate whether the result of such a combination is true or false. Logical Expression Result (circle T or F) True and False T F True and True T F False and True T F False and False T F True or False T F True or True T F False or True T F False or False T F not True T F not False T F

F T F F T T T F F T

What is a decision structure?

It is a program structure that can execute a set of statements only under certain circumstances.

Assume the variables a = 2, b = 4, and c = 6. Circle the T or F for each of the following conditions to indicate whether its value is true or false. a == 4 or b > 2 T F 6 <= c and a > 3 T F 1 != b and c != 3 T F a >= −1 or a <= b T F not (a > 2) T F

T F T T T

Explain how short-circuit evaluation works with the and and or operators.

The and operator: If the expression on the left side of the and operator is false, the expression on the right side will not be checked. The or operator: If the expression on the left side of the or operator is true, the expression on the right side will not be checked.

What values can you assign to a bool variable?

True or False

When you write an if-else statement, under what circumstances do the statements that appear after the else clause execute?

When the condition is false

What types of relationships between values can you test with relational operators?

You can determine whether one value is greater than, less than, greater than or equal to, less than or equal to, equal to, or not equal to another value.

Convert the following code to an if-elif-else statement: if number == 1: print('One') else: if number == 2: print('Two') else: if number == 3: print('Three') else: print('Unknown'

if number == 1: print('One') elif number == 2: print('Two') elif number == 3: print('Three') else: print('Unknown')

Write an if statement that assigns 0.2 to commissionRate if sales is greater than or equal to 10000.

if sales >= 10000: commissionRate = 0.2

Write an if statement that displays the message "The number is not valid" if the value referenced by speed is outside the range 0 through 200.

if speed < 0 or speed > 200: print('The number is not valid')

Write an if statement that displays the message "The number is valid" if the value referenced by speed is within the range 0 through 200.

if speed >= 0 and speed <= 200: print('The number is valid')

Write an if statement that assigns 0 to x if y is equal to 20

if y == 20: x = 0

What statement do you use in Python to write a dual alternative decision structure?

if-else

What is a compound Boolean expression?

it is an expression that is created by using a logical operator to combine two Boolean subexpressions.

What would the following code display? if 'z' < 'a': print('z is less than a.') else: print('z is not less than a.')

z is not less than a.


Ensembles d'études connexes

FINC 3310 Leavell - All Possible Exams 1, 2, 3, & 4 - Final Exam Preparation

View Set

Accumulated Practice! Arithmetic/Elem. Alg./CL Math

View Set

2016 AP Lang Practice Exam: Multiple Choice

View Set

Psychology 150;Chapter 5, Sensation and Perception

View Set

10 killer job interview questions and answers

View Set

BYU Fitness for Living Well (Online)

View Set

AP Human Geography Review - Unit Six

View Set