Chapter 3 : Selections
Assume age = 16, what is ticketPrice after ticketPrice = 20 if age >= 16 else 10
20
List 6 relational operators:
<, <=, ==, !=, >, >=
Which operators are right-associative?
=
suppose income is 4001. What will be displayed in following code? if income > 3000: print (" Income is greater than 3000") elif income > 4000 print (" Income is greater than 4000")
Income is greater than 3000
What happens if you enter an integer as 05?
It will be the same as entering 5.
Analyze the following code: even = False if even: print ("It is even!")
The code is nothing
Analyze the following code: even = False if even = True: print(" It is even!")
The program has a syntax error in like 2 of (even = True). It is not correct condition. It should be replaced with (if even == True:)
What is True and True?
True
What is True or False?
True
What is not False?
True
List the precedence order of the Boolean operators. Evaluate the following expressions: true or true and false true and true or false
True or true and False is True True and True or False is True
The word True is ________.
a Python keyword a Boolean literal
Boolean Value
a data type that is either true or false.
If age is greater than 13 and less than 18
age > 13 and age < 18
Boolean Expression
evaluates to Boolean value
Evaluate the following expressions: 2 * 2 - 3 > 2 and 4 - 2 > 5 2 * 2 - 3 > 2 or 4 - 2 > 5
false false
Boolean variable
holds Boolean value
Write a code to display "Cannot get a driver's license" if age is less than 16 and "Can get a driver's license" if age is greater than or equal to 16.
if age < 16: print ( "Cannot get a driver's license") else: print ( "Can get a driver's license")
tax = income * 0.2 if income > 10000 else income * 0.17 + 1000
if income > 10000: tax = income * 0.2 else: tax = income * 0.17 + 1000
Suppose isPrime is a boolean variable, which of the following is the correct and best statement for testing if isPrime is true.
if isPrime
Example Code
if number print("HiFive") if number == 7: print("LuckySeven")
print ( i if number % 3 == 0 else j )
if number % 3 == 0: print (i) else: print(j)
Example Code
if number % 5 == 0: print("HiFive") elif number == 7: print("LuckySeven") else: print(number)
Which displays the area of a circle if the radius is positive?
if radius > 0: print(radius * radius * 3.14159)
Write an if statement that increases pay by 3% if score is greater than 90, otherwise it increases pay by 1%
if score > 90: pay *= 1.03 else: pay *= 1.01
Example Code
if score >= 90: print("A") elif score >= 80: print("B") elif score >= 70: print("C") elif score >= 60: print("D") else: print("F")
score = 3 * scale if x > 10 else 4 * scale
if x > 10: score = 3 * scale else: score = 4 * scale
Write an if statement that assigns 1 to x if y is greater than 0:
if y > 0: pay *= 1.03
What will be displayed by the following code? isCorrect = False print( " Correct" if isCorrect else "Incorrect")
incorrect
Which are equivalent to x != y
not ( x == y) x > y or x < y
randint(a,b)
random integer between a and b
Generate a random integer between 0 and 5, use:
random.randint(0, 5) random.randrange(0, 6)
random integer 0 or 1 ?
random.randrange (0,2) or random.randint (0,1)
Random integer i such that 10 < = i < 20 ?
random.randrange (10, 50 + 1) or random.randit (10,50)
Generate a random integer i such that 0 < = i < 20?
random.randrange (10,20) or random.randit (10, 19)
rangeint (a,b)
returns a random integer between a and b - 1
random()
returns random float between 0.0 and 1.0 (excluding 1.0)
The_________ function immediately terminates the program
sys.exit()
True or false? All the binary operators except = are left - associative
true
What is the value of the following expression? True or True and False
true
How do you draw a red dot with diameter 3 ?
turtle.dot (3, "red')
How do you hide the cursor on Turtle?
turtle.hideturtle()
If weight greater than 50 and height is greater than 160
weight > 50 and height > 160
Assume x = 4 and y = 5, Which are True ?
x != 5
Write a simple expression that returns true if x is even or false if x is odd
x % 2 == 0
Assume x = 14 and y = 15, Which is True?
x % 2 == 0 and y % 2 == 1 x % 2 == 0 or y % 2 == 0
If you enter 5, what is the output of the following code? i = int(input("Enter an integer: 5 ") if i < 4 : x = 1 else: x = -1 print(x)
x = -1
Given |x| >= 4, which of the following is true?
x >= 4 and x <= -4
Given |x| <= 4, which of the following is true?
x >= 4 and(||) x <= -4
Can the following conversion be allowed? i = int(True) j = int (False) b1 = bool(4) b2 = bool(0)
yes. i becomes 1, j becomes 0, bl becomes True, and b2 becomes False
Write a Boolean expression that evaluates to true if a number stored in variable num is between 1 and 100 or the number is negative
( num > 1) and (num < 100) or (num < 0)
Write a Boolean expression for |x-5| < 4.5
(-4.5 < x - 5 < 4.5)
Rewrite the following code if statements using a conditional expression: (a) if ages >= 16: ticketPrice = 20 else: ticketPrice = 10 (b) if count % 10 == 0: print ( count, end = " * " ) else: print ( count, end = " # ")
(a) - ticketPrice = 20 if (ages >= 16) else 10 (b) - print (count, end = " * " if count % 10 == 0 else " # "
To check Wheater a char variable ch is an uppercase letter, you write:
(ch >= 'A' and ch <= 'Z') + ('A' <= ch <= 'Z')
Rewrite the following statement using a Boolean expression: if count % 10 == 0: newLine = True else: newLine = False
(newLine = (count % 10 == 0)
Write a Boolean expression for |x-5| > 4.5
(x -5 > 4.5 and x - 5 < -4.5)
Which Boolean expressions are correct?
(x > 0) or (x < 0) (-10 < x < 0)
Write a Boolean expression that evaluates to true if a number stored in variable num is between 1 and 100
(x > 1) and (x < 100)
The order of the precedence of the operators +, *, and, or is:
*, + , and , or
Rewrite a conditional expression that returns - 1 or 1 randomly
- 1 if random.randint (0,1) == 0 else 1
What will be displayed by the code? ch = 'F' if ch >= 'A' and ch <= 'Z': print(ch)
F
What is False or False?
False
What is True and False?
False
What is not True?
False