# Python book of magic/06.If

Contents | Previous | Next

# if statement

You have already came across the word if heavily used in English. If you read this book from the beginning, you may have came across this short word. if work the same as (if) in English.
For example
If I complete high school, I am going to university.
We have If as used in english, after it there is a condition(underlined). This condition is the one that will determine whether the learner will go to university or not.
Suppose the learner was unable to complete high school. The learner will not go to university. Imagine the sentence without if,the sentence will be meaningless. This is how useful if is in python.
Most time is spent creating conditions which would involve logic expressions and some operators.
if statement definition
Statements are those things run when the condition is met. The collon(:) is used for indentation purposes (remember python is indented language)

## Conditions

Like I said conditions are created using logics( like probality in Mathematics ) and expressions( used in Mathematics to compare LHS with RHS ). Even if you never been in math class, you will still find it easy to learn them.

### Expressions

Expressionns are used for comparing two things. Maybe comparing two variables, numbers or anything in python. Their output will either be True or False. If output is True the condition will be run.
Some of operators used are.
Example 01
Output
let chage the value of a
Output
So why correct not printed on screen. Remeber, condition is not completed. Because the value of a is not the same as value of b. But what if you wan't to do something even when condition is not completed.
This is when we have to use another statement called else.
Example 02
Output
Code under else will ongly be run if condition in if statement is not completed. What if I want to have many if statements to increase chances that my code will be run.
Example 03
Output
You are not limited to doing simple things like printing text on screen. You can define functions, create variables, solve problems involving numbers or make anything that python can do.

### Logic

Logics are ways of including or excluding events that are occurring or will occur. Event can be anything that occurs e.g death, winning match or adding numbers.
Some logic works are
I know you wont understand what thise things above mean. Let use True and False instead of A and B.
Example 04(and)
Output
The first line checks if A and B are true. If both of them are true. It returns true or else False. Our output is false, because A and B are not both of them True.
The second line checks if B and B are True. If they are, it return True or else false. The output is False, as both B and B are false.
The third line checks if A and A are True. True is returned when both A and A are True or False if one or both of them is False. The output is True, as both A and A are true.
Example 05(or)
Output
In or, if one of the output(A or B) is True then True is returned. Study the example below to get more information.
Example 06(not)
Output
The not changes True to False and False to True. Look at line 1 and 2 to observe this. Line 2 is bit harder, due to combination of not and and. First you need to get the output of A and B which is True. Then apply <n>not</n> by changing the output which is True to opposite of it which is False.
For you to create a strong condition you need to combine logic gates and expressions into one working tool. What I think you have realised is that , the outputs of both expressions and logics are both True or False. Instead of passing A and B into logics we can use expressions.
Example 07
Output
Ohh, why did it run else statement while kevin is inside the Num list? be aware that and was used in the condition. It means that if kevin is inside Num list and if a is equal to marry, then condition will be met as the output of the condition will be True. ( Remember conditions should return either True or False). If the output of condition is False then the condition is not met, resulting in statements under if not run.
Get used to logics and expressions, they will be useful when creation Graphical User interface sometimes called gooey (GUI).
Contents | Previous | Next