Bird
0
0

Which of the following is the correct syntax for an if-else statement in Python?

easy📝 Syntax Q12 of 15
Python - Conditional Statements
Which of the following is the correct syntax for an if-else statement in Python?
Aif x > 0: print('Positive') else: print('Non-positive')
Bif x > 0 then print('Positive') else print('Non-positive')
Cif (x > 0) { print('Positive'); } else { print('Non-positive'); }
Dif x > 0 print('Positive') else print('Non-positive')
Step-by-Step Solution
Solution:
  1. Step 1: Recall Python if-else syntax

    Python uses a colon after the condition and indentation for the code blocks.
  2. Step 2: Check each option

    if x > 0: print('Positive') else: print('Non-positive') uses colons and indentation correctly. if x > 0 then print('Positive') else print('Non-positive') uses 'then' which is not Python syntax. if (x > 0) { print('Positive'); } else { print('Non-positive'); } uses braces and semicolons, which are for other languages. if x > 0 print('Positive') else print('Non-positive') misses colons and indentation.
  3. Final Answer:

    if x > 0: print('Positive') else: print('Non-positive') -> Option A
  4. Quick Check:

    Colon + indent = correct if-else [OK]
Quick Trick: Remember colons and indentation for if-else in Python [OK]
Common Mistakes:
MISTAKES
  • Using 'then' keyword
  • Forgetting colons after if and else
  • Not indenting code blocks

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes