Python: If Statements

This is a worksheet to teach you if statements in Python/Sage. Please make sure you know “For Loops” first.

Here is the basic “if statement”. This is a way to do something only when a certain check passes. In this example, we wish to print a number only when it is prime.

 

There’s also an else clause that can be useful. Try to guess what this will do and then try it out:

 
Here’s a box for you to experiment with: