Meraj
PF 3rd Chapter 50 Important Short Questions
What is a decision structure in Python?
A decision structure is a programming construct that allows a program to choose between
different paths based on conditions. The most common decision structure in Python is the if
statement, which executes a block of code if a condition is true. You can also use elif (else if)
for multiple conditions and else for a default action when no conditions are true.
Example:
python
Copy code
x = 5
if x > 0:
print("Positive")
else:
print("Non-positive")
2. What is the syntax of an if statement in Python?
The syntax of an if statement in Python starts with the if keyword, followed by a condition.
The block of code to execute if the condition is true is indented. Optionally, you can include
elif (else if) and else blocks.
Example:
python
Copy code
if condition:
# code to execute if condition is true
elif another_condition:
# code to execute if another_condition is true
else:
# code to execute if neither condition is true
3. What is a Boolean value in Python?
A Boolean value in Python is a data type that can have one of two values: True or False.
Boolean values are used to evaluate conditions in decision structures. These values are typically
the result of comparisons or logical operations.
Example:
python
Copy code
x = 5
is_greater = x > 3 # is_greater is True
Meraj
4. What is the difference between and, or, and not operators in Python?
• and: Returns True if both conditions are true.
• or: Returns True if at least one condition is true.
• not: Reverses the Boolean value of a condition (i.e., it turns True into False and vice
versa).
Example:
python
Copy code
x = 5
y = 10
print(x > 0 and y > 5) # Output: True
print(x > 10 or y > 5) # Output: True
print(not(x > 10)) # Output: True
5. What is an if-else statement?
An if-else statement is a decision structure that allows a program to choose between two paths:
one if a condition is true, and another if the condition is false. It ensures that one of the two
blocks of code will always execute.
Example:
python
Copy code
age = 18
if age >= 18:
print("Adult")
else:
print("Minor")
6. What is an elif statement?
The elif (short for "else if") statement is used to check multiple conditions in a decision
structure. It allows you to specify additional conditions to check if the initial if condition is
false. You can have multiple elif statements.
Example:
python
Copy code
age = 20
if age < 18:
print("Underage")
elif age == 18:
print("Just an adult")
else:
print("Adult")
Meraj
7. How do you use comparison operators in Python?
Comparison operators are used to compare two values. These operators return Boolean values
(True or False). Common comparison operators include:
• ==: Equal to
• !=: Not equal to
• >: Greater than
• <: Less than
• >=: Greater than or equal to
• <=: Less than or equal to
Example:
python
Copy code
x = 5
y = 10
print(x == y) # Output: False
print(x < y) # Output: True
8. What is the purpose of the if statement's condition?
The condition in an if statement is an expression that evaluates to a Boolean value (True or
False). If the condition is true, the code inside the if block is executed; if the condition is false,
the code inside the else block (if present) is executed.
Example:
python
Copy code
x = 10
if x > 5:
print("Greater than 5") # This will be executed because the condition is
True
9. What is a nested if statement?
A nested if statement is an if statement inside another if statement. It allows for more complex
decision structures, where multiple conditions must be checked in sequence.
Example:
python
Copy code
x = 10
if x > 5:
if x < 15:
print("Between 5 and 15") # This will be executed
Meraj
10. How can you check if two values are equal in Python?
You can use the equality operator == to check if two values are equal. This operator returns True
if the values are equal and False if they are not.
Example:
python
Copy code
x = 5
y = 5
print(x == y) # Output: True
11. How do you negate a condition in Python?
You can negate a condition using the not operator. This operator inverts the Boolean value of
the condition it is applied to, turning True into False and False into True.
Example:
python
Copy code
x = 5
print(not(x > 10)) # Output: True, because x > 10 is False, and not False is
True
12. What is the order of operations in Boolean expressions?
In Python, the order of operations in Boolean expressions is as follows:
1. not is evaluated first.
2. and is evaluated second.
3. or is evaluated last.
This means that not has the highest precedence, followed by and, and or has the lowest
precedence.
Example:
python
Copy code
x = 5
y = 10
z = 15
print(not(x > 10) and y < z or x == 5) # Output: True
Meraj
13. What is the else statement used for?
The else statement is used to specify a block of code that will execute if the condition in the if
statement is false. It acts as a default action when none of the conditions in if or elif are true.
Example:
python
Copy code
x = 10
if x > 15:
print("Greater than 15")
else:
print("Not greater than 15") # Output: Not greater than 15
14. What is the if statement used for in Python?
The if statement is used to check if a condition is true. If the condition evaluates to True, the
code inside the if block is executed. If the condition is False, the code inside the if block is
skipped. It is the fundamental decision structure in Python for making choices based on
conditions.
Example:
python
Copy code
x = 10
if x > 5:
print("x is greater than 5") # This will be printed
15. What happens if the condition in an if statement is false?
If the condition in an if statement evaluates to False, the code inside the if block is skipped,
and the program moves on to the next part of the code. If there is an else statement following
the if, the code inside the else block will be executed instead.
Example:
python
Copy code
x = 3
if x > 5:
print("x is greater than 5")
else:
print("x is not greater than 5") # This will be printed
Meraj
16. What is a chained if statement?
A chained if statement is when multiple conditions are checked in sequence using if, elif, and
else. If the first condition is false, the program checks the next condition in the elif block. If
all conditions fail, the else block is executed.
Example:
python
Copy code
x = 10
if x < 5:
print("Less than 5")
elif x == 10:
print("Equal to 10") # This will be printed
else:
print("Greater than 5")
17. How does Python evaluate conditions?
Python evaluates conditions based on the Boolean values of the expressions involved. If an
expression evaluates to True, the code in the if block is executed. If it evaluates to False,
Python moves to the next part of the code (e.g., elif or else). Comparison and logical operators
like ==, >, <, and, and or are used to form these conditions.
Example:
python
Copy code
x = 7
y = 5
if x > y:
print("x is greater than y") # This will be printed
18. What is the == operator used for?
The == operator is used to check if two values are equal. It returns True if the values on both
sides of the operator are equal, and False if they are not.
Example:
python
Copy code
x = 5
y = 5
print(x == y) # Output: True
Meraj
19. What is the difference between = and == in Python?
The = is the assignment operator, which is used to assign a value to a variable. The == is the
equality operator, which is used to compare two values to check if they are equal.
Example:
python
Copy code
x = 5 # Assignment
if x == 5: # Comparison
print("x is 5")
20. What is the not operator used for?
The not operator is used to reverse the Boolean value of a condition. If the condition is True,
applying not makes it False, and if the condition is False, applying not makes it True.
Example:
python
Copy code
x = 10
print(not(x > 5)) # Output: False, because x > 5 is True, and not True is
False
21. How do you check multiple conditions in Python?
You can check multiple conditions using logical operators like and, or, and not. These operators
allow you to combine multiple conditions into one compound condition.
• and returns True if both conditions are true.
• or returns True if at least one condition is true.
• not reverses the Boolean value of a condition.
Example:
python
Copy code
x = 5
y = 10
if x < 10 and y > 5:
print("Both conditions are true")
Meraj
22. What is the elif statement used for in Python?
The elif (else if) statement is used to check multiple conditions after an initial if condition. It
allows you to test additional conditions if the previous if condition is false. If none of the if or
elif conditions are true, the else block is executed (if present).
Example:
python
Copy code
x = 10
if x > 20:
print("Greater than 20")
elif x == 10:
print("Equal to 10") # This will be printed
else:
print("Less than 10")
23. What is a nested if statement?
A nested if statement is when one if statement is placed inside another if statement. This
allows you to check more specific conditions after an initial condition is met.
Example:
python
Copy code
x = 10
if x > 5:
if x < 15:
print("Between 5 and 15") # This will be printed
24. How do you handle invalid input in Python?
You can handle invalid input in Python using try and except blocks. This allows you to catch
errors and provide an alternative response or handle the error gracefully.
Example:
python
Copy code
try:
age = int(input("Enter your age: "))
except ValueError:
print("Invalid input, please enter a number")
Meraj
25. What is the importance of else in decision-making?
The else statement is used to provide a default action when none of the preceding if or elif
conditions are true. It ensures that something is executed when all conditions fail, providing an
alternative path.
Example:
python
Copy code
x = 2
if x == 1:
print("One")
else:
print("Not one") # This will be printed
26. What are logical operators in Python?
Logical operators are used to combine multiple conditions or Boolean expressions. The three
primary logical operators in Python are:
• and: Returns True if both conditions are true.
• or: Returns True if at least one of the conditions is true.
• not: Reverses the Boolean value of a condition.
Example:
python
Copy code
x = 5
y = 10
if x > 3 and y < 15:
print("Both conditions are true")
Meraj
27. What is the truth table for the and operator in Python?
The and operator returns True only when both operands are true. Here's the truth table for and:
• True and True → True
• True and False → False
• False and True → False
• False and False → False
Example:
python
Copy code
x = True
y = False
print(x and y) # Output: False
28. What is the truth table for the or operator in Python?
The or operator returns True if at least one of the operands is true. Here's the truth table for or:
• True or True → True
• True or False → True
• False or True → True
• False or False → False
Example:
python
Copy code
x = True
y = False
print(x or y) # Output: True
29. What is the truth table for the not operator in Python?
The not operator inverts the Boolean value of its operand:
• not True → False
• not False → True
Example:
python
Copy code
x = True
print(not x) # Output: False
Meraj
30. What is the purpose of an if statement in Python?
An if statement is used to execute a block of code only if a specific condition is true. It allows
the program to make decisions and execute different actions based on logical conditions. The
condition in an if statement is evaluated as a Boolean expression.
Example:
python
Copy code
x = 10
if x > 5:
print("x is greater than 5") # Output: x is greater than 5
31. What is a Boolean expression in Python?
A Boolean expression is an expression that evaluates to either True or False. It is commonly
used in decision structures like if statements. These expressions often involve comparison
operators (==, !=, >, <) or logical operators (and, or, not).
Example:
python
Copy code
x = 10
y = 5
print(x > y) # Output: True
32. What is the difference between == and = in Python?
The == operator is used to compare two values for equality. It checks whether the values on both
sides are the same. On the other hand, the = operator is used for assignment, which assigns a
value to a variable.
Example:
python
Copy code
x = 5 # Assignment
if x == 5: # Comparison
print("x is 5") # Output: x is 5
Meraj
33. How do you evaluate multiple conditions in Python?
You can evaluate multiple conditions in Python using logical operators like and, or, and not.
These operators allow you to combine conditions and make more complex decisions.
Example:
python
Copy code
x = 10
y = 5
if x > 5 and y < 10:
print("x is greater than 5 and y is less than 10") # Output: x is
greater than 5 and y is less than 10
34. How can you use else in an if statement?
The else statement follows an if block and provides an alternative set of instructions to execute
if the condition in the if statement is false. It ensures that one of the conditions will be executed,
either the if or else.
Example:
python
Copy code
x = 4
if x > 5:
print("Greater than 5")
else:
print("Not greater than 5") # Output: Not greater than 5
35. What does the if-elif-else structure allow you to do?
The if-elif-else structure allows you to check multiple conditions in sequence. If the first if
condition is False, the program checks the elif conditions (else if) one by one. If none of the
if or elif conditions are true, the code inside the else block is executed.
Example:
python
Copy code
x = 15
if x > 20:
print("Greater than 20")
elif x == 15:
print("Equal to 15") # Output: Equal to 15
else:
print("Less than 20")
Meraj
36. What is the syntax for an if statement in Python?
The syntax for an if statement in Python is:
python
Copy code
if condition:
# Code block to execute if condition is True
You can also use elif and else to handle multiple conditions:
python
Copy code
if condition1:
# Code block if condition1 is True
elif condition2:
# Code block if condition2 is True
else:
# Code block if no condition is True
37. What is the if-else chain used for?
The if-else chain is used to make decisions based on multiple conditions. It allows you to
choose one of several paths in your program. It checks each condition in sequence, and once a
condition is true, the corresponding block of code is executed, and the remaining conditions are
skipped.
Example:
python
Copy code
x = 10
if x > 15:
print("Greater than 15")
elif x == 10:
print("Equal to 10") # Output: Equal to 10
else:
print("Less than 10")
Meraj
38. How do you handle a situation where multiple conditions need to be checked
in sequence?
You can use if, elif, and else statements to check multiple conditions in sequence. This
decision structure allows you to test different conditions and execute different code based on
which condition is true.
Example:
python
Copy code
x = 7
if x < 5:
print("Less than 5")
elif x == 7:
print("Equal to 7") # Output: Equal to 7
else:
print("Greater than 5")
39. What is an example of using if statements with nested conditions?
A nested if statement is when an if statement is placed inside another if statement. It allows
for more complex decision-making where multiple conditions need to be checked one after
another.
Example:
python
Copy code
x = 10
if x > 5:
if x < 15:
print("x is between 5 and 15") # Output: x is between 5 and 15
Meraj
40. What is the difference between if and elif in Python?
In Python, if is used to check the first condition, and if it's true, the associated block of code is
executed. On the other hand, elif (short for "else if") is used to check additional conditions only
if the previous if (or elif) conditions were false. You can have multiple elif blocks but only
one if and one else.
Example:
python
Copy code
x = 10
if x > 20:
print("Greater than 20")
elif x > 5:
print("Greater than 5") # Output: Greater than 5
else:
print("Less than or equal to 5")
41. What are comparison operators in Python?
Comparison operators are used to compare two values and return a Boolean result (True or
False). The main comparison operators in Python include:
• == (equal to)
• != (not equal to)
• > (greater than)
• < (less than)
• >= (greater than or equal to)
• <= (less than or equal to)
Example:
python
Copy code
x = 5
y = 10
print(x < y) # Output: True
Meraj
42. What is the role of the else clause in an if statement?
The else clause is used to specify a block of code that will execute if the condition in the if
statement is false. It acts as a default when none of the if or elif conditions are true. The else
block does not need a condition.
Example:
python
Copy code
x = 3
if x > 5:
print("Greater than 5")
else:
print("Not greater than 5") # Output: Not greater than 5
43. What is a Boolean variable?
A Boolean variable is a variable that can only have one of two values: True or False. These
values are commonly used for control flow in decision-making structures like if statements. You
can create a Boolean variable using expressions that evaluate to True or False.
Example:
python
Copy code
is_valid = True
if is_valid:
print("Valid") # Output: Valid
44. What is the purpose of using not in Python?
The not operator in Python is used to reverse the Boolean value of an expression. If the
expression is True, not makes it False, and if the expression is False, not makes it True.
Example:
python
Copy code
x = False
print(not x) # Output: True
Meraj
45. How would you test if a number is between two other numbers in Python?
You can test if a number is between two values using a combination of comparison operators.
Typically, the number should be greater than or equal to the lower bound and less than or equal
to the upper bound.
Example:
python
Copy code
x = 7
if 5 <= x <= 10:
print("x is between 5 and 10") # Output: x is between 5 and 10
46. What does the in operator do in Python?
The in operator is used to check if a value exists in a sequence (like a list, string, or tuple). It
returns True if the value is present in the sequence and False otherwise.
Example:
python
Copy code
fruits = ['apple', 'banana', 'cherry']
print('apple' in fruits) # Output: True
47. What is the role of parentheses in an if condition?
Parentheses in an if condition are used to group conditions and ensure that logical operators are
applied in the correct order. They are important when combining multiple conditions using and,
or, or not.
Example:
python
Copy code
x = 7
y = 5
if (x > 5) and (y < 10):
print("Both conditions are true") # Output: Both conditions are true
Meraj
48. How do you check if a number is divisible by another number in Python?
You can check if a number is divisible by another using the modulo operator (%). If the result of
x % y == 0, it means x is divisible by y.
Example:
python
Copy code
x = 10
if x % 2 == 0:
print("x is divisible by 2") # Output: x is divisible by 2
49. How does the if statement differ from the while loop?
An if statement is used for one-time conditional checks, meaning it only runs the associated
block of code once if the condition is true. On the other hand, a while loop repeatedly checks a
condition and executes the block of code as long as the condition remains true.
Example:
python
Copy code
# If statement:
x = 10
if x > 5:
print("x is greater than 5") # Runs once
# While loop:
while x > 5:
print(x)
x -= 1 # Continues until x is no longer greater than 5
Meraj
50. How would you combine multiple conditions with the and operator?
To combine multiple conditions, you use the and operator to ensure that all conditions must be
true for the entire expression to evaluate as True. If any of the conditions is false, the entire
expression evaluates to False.
Example:
python
Copy code
x = 10
y = 20
if x > 5 and y < 30:
print("Both conditions are true") # Output: Both conditions are true