Python book of magic/06.If
if statement
editYou 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
if condition:
statement1
statement2
statement3
e.t.c
Statements are those things run when the condition is met. The collon(:) is used for indentation purposes (remember python is indented language)
Conditions
editLike 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
editExpressionns 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.
== - check if Left Hand Side(LHS) is equal to RHS. =! - check if LHS in not equak to RHS < - checks if LHS is less than RHS > - do oposite of (<) <= - check if LHS is less than or equal to RHS >= - opposes ( <= )
Example 01
a = 3
b = 3
if a == b:
Print('correct')
a == b
Output
correct
True
let chage the value of a
a = 10
b = 3
if a == b:
Print('correct')
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
a = 10
b = 3
if a == b:
Print('correct')
else:
print('wrong')
Output
wrong
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
a = 5
b = 12
if a == b:
Print('Equal')
elif a < b:
print('a less than b')
elif a > b:
print('a greater than b')
else:
print('none correct')
Output
a less than b
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
editLogics are ways of including or excluding events that are occuring or will occur. Event can be anything that occurs e.g death, winning match or adding numbers.
Some logic works are
A and B - include both event A or B - include either A or B not A - A not included not B - B not include
I know you wont understand what thise things above mean. Let use True and False instead of A and B.
Example 04(and)
A = True
B= False
A and B
B and B
A and A
Output
False
False
True
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)
A = True
B= False
A or B
B or B
A or A
Output
True
False
True
In or, if one of the ouput(A or B) is True then True is returned. Study the example below to get more information.
Example 06(not)
A = True
B= False
not B
not (A and B)
not A
Output
True
True
False
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
Num= [23, 'kevin, 45]
a='john'
if kevin in Num and a == 'marry':
print('logic')
else:
print('stop guessing')
Output
stop guessing
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).