click below
click below
Normal Size Small Size show me how
chapter 5 computer s
| Question | Answer |
|---|---|
| Purpose of if statements: | when you need to make decisions/choices in your program, allows you to execute the code only if the statement is true. |
| What is car=='bmw' | A conditional statement |
| Always need | An else statement |
| For Boolean you get | True or false |
| == is what? | Equality operator |
| != is what? | Inequality statment |
| For and they have to be? | Both have to be TRUE |
| for or they have to be? | Only ONE Of them has to be true. |
| What is the purpose of an elif statement? | It allows you to check multiple conditions after an initial if. Only the first true elif block will execute. |
| What happens if none of the if or elif conditions are true and there is no else? | The program simply skips the conditional block and continues with the rest of the code. |
| What is the difference between = and ==? | = assigns a value to a variable, while == checks if two values are equal. |
| How can you combine multiple conditions in one if statement? | Using logical operators like and, or, and not. |
| What does the not operator do in a conditional? | It reverses the Boolean value (True becomes False, False becomes True). |
| True or False: The code inside an if block will always run. | False. It only runs if the condition is True. |
| Can you write an if statement without elif or else? | Yes. elif and else are optional. |
| What is short-circuit evaluation in Python? | In and or or statements, Python stops evaluating as soon as the outcome is determined. |
| Which operator has higher precedence: and or or? | and has higher precedence than or. |
| How do you check if a variable x is NOT equal to 10? | Using x != 10. |
| What is a nested if statement? | An if statement placed inside another if statement; allows checking multiple layers of conditions. |
| What is a nested if statement? | An if inside another if |
| What will this code print? x = 8 if x > 5: if x < 10: print("x is between 6 and 9") | x is between 6 and 9 |
| Which of the following is falsy in Python? | 0 |
| if "": print("Hello") else: print("World") | World |
| Which is a correct comparison chain for x > 5 and x < 15 | 5 < x < 15 |
| in and not inWhat does the in operator do? | Checks if a value exists in a sequence |
| How do you check if "banana" is NOT in a list ["apple", "orange", "grape"]? | "banana" not in ["apple", "orange", "grape"] |
| How do you write this if-else as one line? if x > 0: message = "Positive" else: message = "Non-positive" | message = "Positive" if x > 0 else "Non-positive" |
| What is wrong with this code? if x = 5 print("x is 5") | = should be == |
| Why will this code cause an error? | print must be indented |