Selection

Computer Science: KS3, topic 2.3.6

If-statements

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

If statements

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.
Else statements

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.
Elif statements

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.
Example

Example

Test yourself on Selection

  • List the three keywords that may be used in a Python if statement.

Practise this topic for free