Quiz 2 Flashcards
What is a Boolean Expression?
A bool is a data type. An expression that is tested to see whether it is true or false by an if statement in Python.
What is a control structure?
A control structure allows us to control the order in which a set of statements executes
What is a sequence structure?
a set of statements that execute in the order that they appear.
What is a dual alternative decision structure? How does it work? What statment do you use in Python to write a dual alternative decision structure?
if else clause. One taken if the condition is true and the other if the condition is false.
Write a snippet of code that will compare two strings. The program will print “They are the same” if the strings are identical. The program will print “They are not the same.” if the strings are different.
P = "something" Q = "something" if (P == Q): print ("They are the same.") else: print("They are the same.")
What is a compound Boolean expression?
( P==Q and R==S)
What will the program display? s1 = "Dillon" s2= "Butte" if s1 > s2: print (s2) print (s1) else: print (s1) print (s2)
Butte
Dillon
Simplify:
Not (num >0 and temp <=0)
num <= 0 or temp >0
Simplify:
not (sum<5 or not (num>0)
sum >=5 and num>0
simplify:
not(num>0 and sum >0)
num<= 0 and sum <=0
simplify:
not( X and not Y) or (X and Y)
(not X or Y) and (X or Y)
What is a decision structure:
specific action(s) performed only if a condition exists
write an if statement that assigns 20 to the variable y and assigns 40 to the variable z if the variable x is greater than 100
if x > 100:
y = 20
Z = 40
What is a single alternative decision structure:
provides only one alternative path of execution
What is a relational operator?
determines whether a specific relationship exists between two values Example: greater than (>)
What are logical operators?
operators that can be used to create complex Boolean expressions
Write an if-else statement that determines whether the points variable is outside the range 9 to 51. if the variable’s value is outside this range it should display “invalid points.” Otherwise, it should display “Valid points.”
points = 100 if points < 9 or points > 51: print("Invalid points:) else: print("Valid points")
points = 100 if 9 <= points <= 51: print("valid points") else: print("invalid points")
Convert the nested if-else statements into an if-elif-else statement: if num == 1: print("one") else: if num == 2: print("two") else: if num == 3: print("three") else: print("unknown")
if num == 1: print ("one") elif num == 2: print ("two") elif num == 3: print ("three") else: print("unknown")
The area of a rectangle is the rectangle’s length times its width. Write a program that asks for the length and width of two rectangles. The Program should tell the user which rectangle has the greater are, or the areas are the same.
length1 = float(input("Enter the length of the first rectangle: ")) width1 = float(input("Enter the width of the first rectangle: "))
length2 = float(input("Enter the length of the second rectangle: ")) width2 = float(input("Enter the width of the second rectangle: "))
if ((length1width1) == (length2width2)):
print(“They have the same area”)
elif ((length1width1) > (length2width2)):
print(“The area of rectangle 1 is greater than rectangle 2”)
else:
print(“The area of rectangle2 is greater than rectangle 1.”)