daniel_garcia
4d ago โข 0 views
Hey everyone! ๐ Struggling with 'if' statements in Python? No worries, we've all been there! These are super fundamental for making your programs smart and reactive. I've put together a quick study guide and some practice questions to help you nail them down. Let's get coding! ๐
๐ป Computer Science & Technology
1 Answers
โ
Best Answer
thomas_richardson
3d ago
๐ Quick Study Guide: Python 'If' Statements
- ๐ What are 'If' Statements? They allow your program to make decisions and execute different blocks of code based on whether a condition is true or false. Think of them as the brain of your code!
- โ
Basic Syntax: An
ifstatement starts with theifkeyword, followed by a condition, and ends with a colon (:). The code to be executed if the condition is true is indented below it.if condition: # code to execute if condition is True - โ ๏ธ Indentation is Crucial: Python uses indentation (whitespace) to define code blocks. Incorrect indentation will lead to an
IndentationError. Typically, 4 spaces are used. - ๐ก
elifandelse:- โก๏ธ
elif(else if): Used to check multiple conditions sequentially. If the precedingiforelifcondition is false, Python checks the nextelif. - ๐
else: Catches all cases where none of the precedingiforelifconditions were true. It's optional and always comes last.
if condition1: # code for condition1 elif condition2: # code for condition2 else: # code if neither condition1 nor condition2 is true - โก๏ธ
- ๐ง Comparison Operators: Used within conditions to compare values:
==(equal to)!=(not equal to)<(less than)>(greater than)<=(less than or equal to)>=(greater than or equal to)
- ๐ Logical Operators: Combine multiple conditions:
- โ
and: Both conditions must be true. - โ
or: At least one condition must be true. - ๐ซ
not: Reverses the boolean result of the condition.
- โ
๐ง Practice Quiz: Python 'If' Statements
1. What will be the output of the following Python code?
x = 10
if x > 5:
print("Hello")
if x < 12:
print("World")
- Hello
- World
- Hello
World - Error
2. Which keyword is used to handle multiple conditions sequentially in an 'if' statement chain?
- else if
- elif
- then
- otherwise
3. What is the correct way to check if a variable age is between 18 and 65 (inclusive)?
- if age >= 18 and age <= 65:
- if 18 <= age <= 65:
- if age >= 18 or age <= 65:
- Both A and B
4. What will happen if the indentation is incorrect in an 'if' statement block?
x = 5
if x > 3:
print("Greater") # Incorrect indentation
- The code will run, but with unexpected output.
- It will print "Greater" if x is greater than 3.
- It will raise an
IndentationError. - It will be ignored by the interpreter.
5. Consider the following code:
num = 7
if num % 2 == 0:
print("Even")
else:
print("Odd")
What will be the output?
- Even
- Odd
- Error
- Nothing (no output)
6. What is the output of this code?
a = 10
b = 20
if a > 15 or b > 15:
print("Condition Met")
else:
print("Condition Not Met")
- Condition Met
- Condition Not Met
- Error
- True
7. Which of the following conditions evaluates to True?
not (5 > 3 and 10 < 5)"Python" == "python"(10 != 10) or (False)3 + 2 == 6
Click to see Answers
๐ Answer Key:
- C. Both
x > 5(10 > 5 is True) andx < 12(10 < 12 is True) are true, so both print statements execute. - B.
elifis the correct keyword for "else if" in Python. - D. Both
if age >= 18 and age <= 65:andif 18 <= age <= 65:are valid and correct ways to check ifageis within the range. Python supports chained comparisons. - C. Python uses indentation to define code blocks. Incorrect indentation will result in an
IndentationError. - B.
num % 2 == 0(7 % 2 == 0) is False, so theelseblock executes, printing "Odd". - A. The condition
a > 15 or b > 15evaluates to(10 > 15) or (20 > 15)which isFalse or True, resulting inTrue. So, "Condition Met" is printed. - A. Let's break it down:
(5 > 3 and 10 < 5)is(True and False)which isFalse. Thennot (False)isTrue.- B:
"Python" == "python"isFalsedue to case sensitivity. - C:
(10 != 10) or (False)is(False) or (False)which isFalse. - D:
3 + 2 == 6is5 == 6which isFalse.
- B:
Join the discussion
Please log in to post your answer.
Log InEarn 2 Points for answering. If your answer is selected as the best, you'll get +20 Points! ๐