Short-hand If Statements0%

Short-hand If Statements

Beginner7 min readUpdated: 2026-09-03
Study Materials

Short-hand If Statements

When you have only one statement to execute, you can place it on the same line as the if keyword.


Key Concepts & Detailed Explanation

undefined


Code Examples & Output

Python
score = 95
 
# One-line if
if score >= 90: print("Outstanding performance!")
 
# Multiple statements separated by semicolons (use sparingly)
if score == 95: print("Top scorer!"); bonus = 5; print("Bonus added:", bonus)

Expected Output:

Output
Outstanding performance!
Top scorer!
Bonus added: 5

Best Practices & Common Pitfalls

Use short-hand if only for very simple one-liners; keep multiline indented blocks for readability.


Practice Quiz

1. Is 'if x > 10: print(x)' valid Python syntax?

  • A) Yes
  • B) No, indentation on new line is required
  • C) Only in Python 2
  • D) SyntaxError
Answer: A Explanation: Single-line short-hand if statements are completely valid in Python.

2. What symbol allows separating multiple statements on a single line?

  • A) :
  • B) ;
  • C) ,
  • D) |
Answer: B Explanation: The semicolon (;) separates multiple statements on a single line.

Practice Challenge

Write a short-hand if statement that prints 'Discount unlocked' if bill_amount > 1000.

Next Lesson

Ternary Operators

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Nested ConditionsTernary Operators

Practice Quiz

Test your understanding of this lesson with 1 questions. Each question has one correct answer.

PrevNext