



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This study guide covers fundamental concepts of control flow in python, focusing on conditional statements (if, elif, else) and looping structures (while, for). It includes code examples and pseudocode to illustrate how to implement these structures effectively. The guide also addresses common pitfalls and best practices for writing clean and efficient code. Key topics include boolean expressions, nested structures, and the use of range() function. Designed to help students understand and apply control flow concepts in python programming. (410 characters)
Typology: Exams
1 / 5
This page cannot be seen from the preview
Don't miss anything!




What will be displayed after the following code is executed? counter = 1 while counter <= 20: print(counter, end=" ") counter *= 3 print("\nThe loop has ended.") 1 3 9 The loop has ended. Which of the following in this expression is evaluated first? age >= 65 and status == "retired" or age < 18 age >= 65 Which type of expression has a value of either true or false? Boolean How many times will "Hi there!" be displayed after the following code executes? num = 2 while num < 12: print("Hi, there!") num += 2 5 How many times will "Hi again!" be displayed after the following code executes? for i in range(0, 12, 2): print("Hi, again!") 6 What will this loop display? sum = 0 for i in range(0, 25, 5): sum += i print(sum) 50 Pseudocode can be used to plan
the coding of control structures Code Example 3- 1 num_widgets = 0 while True: choice = input("Would you like to buy a widget? (y/n): ") if choice.lower() == "y": num_widgets += 1 else: break print("You bought", num_widgets , "widget(s).") Refer to Code Example 3-1. Which of the following could be a pseudocode plan for the program? Define widget count variable Begin infinite loop Get user input IF user buys widget add 1 to widget count ELSE end loop Display results To jump to the end of the current loop, you can use the break statement The and operator has higher precedence than the or operator, but lower precedence than the not operator Which of the following in this expression is evaluated first? age >= 65 or age <= 18 or status == "retired" age >= 65 Given the following code, select the compound condition that makes sure that the input is an integer greater than 0 and less than 1,000. my_num = input("Enter a number between 1 and 999:") check_num = int(my_num) while _________________________: my_num = input("Try again. The number must be between 1 and 999") check_num = int(my_num) check_num <= 0 or check_num >= 1000
print("You bought", num_widgets , "widget(s).") Refer to Code Example 3-1. If the user enters "Y" at the prompt, what does the program print to the console? You bought 0 widget(s) today. When two strings are compared, they are evaluated based on the ___________________ of the characters in the string. sort sequence What will be displayed after the following code executes? guess = 19 if guess < 19: print("Too low") elif guess > 19: print("Too high") nothing will dsplay If you want to code an if clause, but you don't want to perform any action, you can code a pass statement What will the output of the following code be if the user enters 3 at the prompt? your_num = int(input("Enter a number:")) while (your_num > 0): product = your_num * 5 print(your_num, " * 5 = ", product) your_num - = 1 3 * 5 = 15 2 * 5 = 10 1 * 5 = 5 Which of the following is true for an if statement that has both elif and else clauses? If the condition in the if clause is true, the statements in that clause are executed. In a while loop, the Boolean expression is tested before the loop is executed Which of the following begins by testing a condition defined by a Boolean expression and then executes a block of statements if the condition is true? a while statement For the following code, what will the result be if the user enters 5 at the prompt? sum = 0 end_value = int(input("Enter a number: "))
for i in range(1, end_value): sum = sum + i print(sum, end=", ") 1, 3, 6, 10, Which of the following can use the range() function to loop through a block of statements? a for statement To compare two strings with mixed uppercase and lowercase letters, a programmer can first use the lower() method to convert all characters in each string to lowercase. Code Example 3- 1 num_widgets = 0 while True: choice = input("Would you like to buy a widget? (y/n): ") if choice.lower() == "y": num_widgets += 1 else: break print("You bought", num_widgets , "widget(s).") Refer to Code Example 3-1. If the user enters "no" at the prompt, what does the program print to the console? You bought 0 widget(s) today.