2.3.6

Selection

Test yourself

If-statements

If-statements are the only form of selection in Python.

Illustrative background for If statementsIllustrative background for If statements ?? "content

If statements

  • Conditions can be tested using the if statement. For example:
    • if (x > 50):
  • Any code written in an indented block after this line will be executed if the condition is true.
Illustrative background for Else statementsIllustrative background for Else statements ?? "content

Else statements

  • We can also add an else statement to the end, for example:
    • else:
  • This will only execute the following indented block if the condition was false.
Illustrative background for Elif statementsIllustrative background for Elif statements ?? "content

Elif statements

  • Elif statements can be added between if and else blocks.
  • They will execute if the condition before it was false, and the condition following it is true.

Jump to other topics

1Computational Thinking & Algorithms

2Programming

3Hardware & Software

4Data

5The Internet

6Online Safety & Security

Go student ad image

Unlock your full potential with GoStudent tutoring

  • Affordable 1:1 tutoring from the comfort of your home

  • Tutors are matched to your specific learning needs

  • 30+ school subjects covered

Book a free trial lesson