Structures Questions and Answers
Already Passed
What will the following code output?
```python
x=5
if x > 3:
print("Greater than 3")
else:
print("Less than or equal to 3")
```
✔✔Greater than 3
How do you check if a number is even in Python?
✔✔You can use the condition `if number % 2 == 0:` to check if the number is divisible by 2 with
no remainder.
What does the `else` clause in an `if-else` statement do in Python?
1
,✔✔The `else` clause runs code when the `if` condition is not met.
What is the purpose of a `while` loop in Python?
✔✔A `while` loop repeatedly executes a block of code as long as the specified condition remains
true.
How can you ensure a loop runs a specific number of times in Python?
✔✔Use a `for` loop with a range, like `for i in range(5):`, to run the loop 5 times.
What is the result of the following code?
```python
x = 10
if x > 5:
print("Big")
elif x > 0:
print("Small")
else:
print("Zero")
2
, ```
✔✔Big
How would you write a loop that prints numbers from 1 to 5 in Python?
✔✔Use `for i in range(1, 6):` to iterate over the numbers and print each one.
What happens if the condition in a `while` loop is always true?
✔✔The loop will run indefinitely, creating an infinite loop.
How can you break out of a loop before it finishes all iterations?
✔✔Use the `break` statement to exit the loop immediately.
How can you skip the current iteration of a loop and move to the next one?
✔✔Use the `continue` statement to skip the rest of the current iteration.
What will the following code output?
```python
x=3
3