Conditions and Branching in Python

From Sustainability Methods

THIS ARTICLE IS STILL IN EDITING MODE

Conditions

Conditions are comparison operations that will produce the answer Yes/ No or Boolean (True/ False) in the end.

There are many conditions that you can use to compare 2 values:

  • equal: ==
  • not equal: !=
  • greater than: >
  • less than: <
  • greater than or equal: >=
  • less than or equal: <=

To try your first conditions, we can start easy:

a = 1

#The result is False because 1 is not equal to 2. 
a == 2

#If you try again, the result will be True. 
a == 1

a > 0 # Result: True

a < 0 #Result: False

We can also use it to check strings and other types of data:

“Kora” != “Aang”

Branching

In programming, branching is more known as if-statement. Why? Because the logic is similar to a tree. You check if something is True or not.

For example, if Aang can learn how to bend all four elements, he does not have to learn anymore, otherwise, he has to learn them to save the world.

If statement

age = 4

#expression that can be true or false
if age >= 4:

		#within an indent, we have the expression that is run if the condition is true
		print("you started to speak" )

#The statements after the if statement will run regardless if the condition is true or false
print("print this anyway")


The ":" in "if age >=4:" is necessary to let Python know that another line of command telling python what to do if the condition is fulfilled will come. In a normal sentence, this would be a comma: "If you are already four or older, then you have already started to speak." The ":" in the if-statement demands an indentation (as you can see the "print("you started to speak")" by one tab. This means that this print command is nested in the if-statement. You can therefore imagine the indentation to create a logical dependency between commands. The commands that are on the same level do not logically depend on each other. The commands with an indentation are logically dependent on the commands with one indentation less. For our case, this means: "You started to speak" will only be printed if the age is 4 or older, whereas "print this anyway" will be printed independently from the age. You can try it out by changing the age:

age = 3

#expression that can be true or false
if age >= 4:

		#within an indent, we have the expression that is run if the condition is true
		print("you started to speak" )

#The statements after the if statement will run regardless if the condition is true or false
print("print this anyway")

You can see here that only "print this anyway" has been printed in this case.

Else Statement

After “if” statement, we have “else” statement. Else statement will be triggered if the “if” statement does not get activated.

age = 4
#age = 6
if age > 5 :
	print(”older than 5 y.o”)

else: 
	print(”younger than 5 y.o”)

You can try changing the age between 4 and 6 to see the difference reaction that the code shows with “if” and “else” statement.

Elif Statement

Between “if” and “else” statement, we have “elif” (else if). The logic follows:

  • if A:
  • elif B1:
  • elif B2:
  • else C:

Meaning that if A is wrong, the code will check the condition of “elif”. If it is still False, then go one row further.

name = “toph”

if name == “toph”:

	print(”earth bending”)

elif name == “katara”:

	print(”water bending”)

elif name == “aang”:

	print (”air bending”)

else 

	print(”fire bending”)

The code above will check one by one whether the name is “toph”, “katara”, “aang”, or others to check what bending skills the person has.

Logical Operators

Sometimes you want to combine the conditions together to have more complex conditions. For example: only males that are older than 18 years old must go into the military. In Python, there are 3 logical operators that can join two conditions or more:

  • and
  • or
  • not

Example:

military={"gender":("male", "female", "male"), "age":[17,18,20]}
gender=military["gender"]
    age=military["age"]
if gender == "male" and age >=18:
    print("military")
else:
    print("no military")

Quiz

1. Write an if statement to check if “Saka” have any bending skills.

2. Write a logic that checks 2 variables: color and fruit. colors can be : red, green, yellow fruits: apple, banana, pinaple And print : “yellow banana” if it the color is yellow, and the fruit is banana.

The author of this entry is XX. Edited by Milan Maushart