If statements

A basic if statement will compare 2 parts of a statement to see whether a condition is true or false.

if some_condition_thats_True_or_False:
    do_an_action
else:
    do_a_different_action

 

A basic if statement example:

if a == b:
    do something...
    Also do this...
else:
    do this instead

 

Before we look at what an if statement is, we need to understand comparative operators first.

In the example of the basic if statement above, the code checks to see if ‘a’ is equal to (==) ‘b’.

Look at the example below. This shows how an if statement is used to print out a string depending on a given answer.

Use the trinket below to write an if statement that checks to see if 10 * 10 = 100. If it does, the code should print out “10 x 10 is equal to 100”. The if statement should also have an else statement. If 10*10 does not equal 100, it should print out “10 x 10 is not equal to 100”.

 

Posted in Uncategorized.

Leave a Reply

Your email address will not be published. Required fields are marked *


*