CodeBlanks Logo Icon CodeBlanks

Summary - Conditionals

Combine if, elif and conditional expressions to write a day-of-week app.

Summary - Conditionals

In this group you learned if, elif/else, nested if, truthiness and conditional expressions.

  • if: runs a block when the condition is true.
  • elif/else: extra conditions and fallback.
  • Prefer and over nested if.
  • Truthy/falsy: check empty with if name.
  • Conditional expression: one-line if/else.

Now two tasks combine these concepts.

Practice if/elif/else chain

Assign score = 85. Write if score >= 90: print("A"), elif score >= 80: print("B"), else: print("C").

Starter code

main.py
# if/elif/else chain

Sample solution

main.py
score = 85
if score >= 90:
    print("A")
elif score >= 80:
    print("B")
else:
    print("C")

Expected output

B

Practice Empty check with truthiness

Assign name = "". Write if not name: print("Empty").

Starter code

main.py
# Empty check

Sample solution

main.py
name = ""
if not name:
    print("Empty")

Expected output

Empty

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which is wrong?

  • if checks a condition
  • elif adds a condition
  • and combines two conditions
  • else takes a condition Correct answer

Explanation: else takes no condition, it is just a fallback block.

2 Complete the conditional expression

Question
result = "Passed" if score >= 50 ____ "Failed"
Answer
result = "Passed" if score >= 50 else "Failed"

Explanation: else gives the value when the condition is false.

All lessons in Beginner