Oh yes.
And welcome back to another class of our course about Python.
So until now we talked about a lot of very interesting things.
And until now you were supposed to have a pretty much good knowledge of python and how it works.
So we talked in our best class about the if statement and how exactly it works.
We talked about the if or statement we talked about the if and statement and basically everything what's
around the if statement.
And we also saw that the if statement works with the boolean.
So usually you need a true or a false to be able to run an if statement which is true in the majority
of cases but you don't really need the statement in itself to be able to run an IF function.
Let me explain myself so you can compare it let's say for example when you compare a number to another
number.
Normally you will have a true or false value.
So for example if you compare one with one well one equals to one.
And in this case you have your if statement.
So this statement means it's true because 1 equals 2 1 So this is the same thing for for our well for
our what we will do today.
So my goal today is really to present you guys how exactly we can work with the if statement without
necessarily having a true or false values or boolean values.
So let's try this out.
So the first thing.
So we'll we'll do some of the also two basic exercises with variables and then after that we'll go and
work with some more advanced data.
So let's start the first thing that I will do.
We can just watch for now.
I'll have two variables so I'll have verbal number one which will be equal to one and viable number
two which will be equal to five.
I'll write down my if statement.
So in this case if variable number one is equal to variable number five a variable number two.
Sorry I want to print.
Yes
otherwise
I want to print.
No.
So in this case print.
No.
If I run the app so just over two points.
If I run my app as you can see the answer will be no.
Why.
Because variable number one is not equal to variable number two.
And in this case if for example I write down five.
So if I won my app it's gonna be yes.
So as you can see I don't necessarily have the true or false statement and nowhere no statements are
the true or false data nor where there is stored in any variable but instead I have let's say numbers.
So I compare one number to another number and because they are you one not the first number is equal
to the same number.
This means that it's true.
So this value is true.
So this theoretically equals 2 true.
And because of that python will print.
Yes I can do it more eventually for example I can even have let's say three variables or in this case
three variables and let's say all right.
Equal to to I can even print something like this equal to variable number three and I can run my app
and as you can see the value will be no.
Why.
Because Bible number one is equal to Bible number two but is not equal to viable number three.
So what I can use is another statement which is not equal to verbal.
Number three.
So if I run my app it's gonna be yes.
Why.
Because Bible number one is equal to Bible number two but not equal to verbal number three so if you
want to write equal it's gonna be two equal signs if you're on the right don't not equal it's gonna
be an exclamation mark and an equal sign.
All right.
So right now you get the point of all this.
Let's try something else.
Let's say for example here I'll have five and six instead of equal what I can do is I can write greater.
So let's say for example Bible number one is greater than Bible number two.
And two points.
Once a year.
If I run my app as you can see Bible number one is not greater than variable number two.
So in this case the answer will be no.
But what I can do also I can say for example equal or greater or equal.
So in this case if I run it it's going to be no but if it's five it's gonna be yes because it's equal.
So in this case I would ask if it's greater or equal.
Well it's equal.
So devout the answer will be yes you can do this for greater for equal for greater or equal for smaller
equal can do this for many many.
Well there are many comparisons that exist.
But the most popular ones are equal not equal greater smaller greater or equal smaller or equal.
All right.
So right now that you understand what exactly are is the comparison part in the if statement I propose
we guys to make two basic apps so you'll be able to practice and understand it a little bit more.
So let's jump in.
So the first thing I'll ask you guys to do is open up your wallet.
You already on so on Europe your text editor and what you will do is create four variables.
So we'll have one and we'll use colors so we'll use string.
You can use colors or any other type of string.
Doesn't matter.
So we have 4 1 4 2
right.
So we have everything what we need that we need
after that you simply add the colors that you want to add.
So in my case it's gonna be blue red black and yellow.
Great
I have what I need.
So I will do right now.
Also my goal right now is simply to show you that you can compare strings together.
So five seconds ago we compared numbers together but right now we'll compare strings together.
So for example if we write down our if available once again a function sorry.
We have our variable number one which is for example equal to level number two in the case that it's
true.
What we'll do we want Python to return.
Print.
Yes.
It's the same color.
Great
so we run our app and we have nothing because it's not the same.
So what will be our same condition.
Else if it's not true.
Once Python to print.
No no it's not true enough or yet very important.
You're two points when it's all done simply run your app.
So as you can see the answer is no it's not true.
Because Bible number one is not equal to Bible number two if for example.
Here we have red.
So red is equal to red.
We run our app.
Yes it's the same color.
So we can if it is the same thing it's going to tell you this but let's say for example we have red
with a capital letter and red with the small letter.
It's not going to work.
Why.
Because Python respects kept the letters.
So if you have two words but the letters one it's in the capital the other is not it's not gonna work.
So very important you need the exact same thing if you want it to work.
All right.
So right now you get the point that you can compare strings together you can compare numbers together
you can compare boolean together you can compare absolutely everything together but you can't compare
string with numbers or numbers with that for example Boolean if for example you want to compare numbers
with strings and you need to transform your string into a number before being able to do it and you
have some functions do it.
All right we will.
So let's create another small app.
This time it's going to give him the app will be a bit more complex since I want you guys to practice
a bit more but that in this app what we'll do principally is very simple.
Our goal is to find out who is the richest person between the three different persons.
So what exactly we'll have to do right now the first thing we'll need to find out the name of each person.
So we'll ask users to enter their names and enter how much money they have.
So let's write it down.
So we have our first variable which will be a person's name.
So we have divided it looks like this equal input.
So we have our input and we'll ask our user to do is what is your name.
So right here the user will enter his or her name from now the next thing that we want to know from
this user is how much money do you have.
So we'll treat another variable that will name person's wallet the state's purse wallet and we'll write
down input and ask the person how much money do you have.
Right.
So the better the person will answer us with his or her name and the money that that this person as
you can.
Right now what we need to do is the exact same thing for the two next persons because we'll have three
different persons so you can simply copy everything and past it and instead of having person number
one we'll have person number two.
Great.
So we do this for person number two and person number three as well.
All right.
So until now you have everything you need to be able to continue.
So if person number one number two and number two write down that we have our basic database.
It's time for us to create our app.
So once again we'll use the if function in this app to be able to run it.
So what we will ask well will ask Python to do right now.
Very simple.
We want to know what the name of the person end if the person number one has more money than the person
number two.
If yes we want to verify if Person number one has more money than person number three.
If those two statements are respected we'll simply return person number one is the richest.
So very very simple as you can see the app is simple.
Right now it's time to create it.
So we'll have our if statement right here and the first thing that will need to enter right now is we
need to convert our strings into numbers so as I explained before you can't work with strings and numbers
at the same time because Python can compare strings with numbers.
So very important to convert to convert it first.
So how do we convert strings to numbers with the float functions that don't float open up your parentheses
and you copy this variable right here.
So if person number one while it equal in this case to Person number two wallet now for you have to
write down your float function because without it nothing will work.
You'll have errors.
So.
All right.
We have float person number one wallet greater than Person number two.
Well so this is the first part of the function we verify if it's true then we want to verify if the
person number one has more money than the person number tweet as well.
So we'll use the end function here.
We'll use if and and we'll write down our float to be able to work with the numbers.
So person number one wallet is greater than
Person number two well all right so we have everything we need.
We write down our two points and we jump.
So next question.
Next thing that we need to do right now that we have our first function ready is our output right.
Our output.
So very simple what one python to print right now.
So very simple.
We print person number one name.
So we need the name of the person number one plus is the richest.
So is the richest.
So we have our inputs right here.
We have our database right here so everyone enters his name.
And how much money he has.
The calculation right here.
So person number one wallet greater than Person number two same thing for a person number three
and then we print the name of person number one is the richest if it's true if it's false if it doesn't
work what we need to do right now is very simple.
So we verify if it's number two the person number two is the richest.
So in this case we'll use the Elif function once again it's an if function that is integrated in another
function.
So we do the exact same thing instead of free writing.
Everything can simply copy it and post it here then the next thing that you guys need to do is changing
the name of people.
So here we'll have Person number two is richer than person number one.
Same thing here.
Person number two in number three and in the name not forget the right person number two as well
and finally you copy and paste everything to be able to do the exact same thing for person number two
when we have all this what we can do now we can run up.
So we have absolutely everything we have our condition so our logical test for Person 1 2 and 3 and
we can run our up to see what it looks like.
So what is your name.
In this case name will say Jack how much money Jack has one hundred dollars.
What is your name.
I don't know.
Meaning how much money Mina has.
Fifty dollars.
And finally what is your name.
So let's say John.
How much money John has.
Twenty five dollars.
We run our app.
Jack is the richest.
Which makes sense.
Let's change our inputs.
In this case what's earning.
Once again we'll say Jack
how much money do we have Jack.
Ten dollars.
Mina how much money do you have.
Fifty dollars.
And finally what's your name.
Let's say John.
How much money do you have John.
Fifteen let's say fifteen dollars.
We run the app.
Mina is the richest as you can see Isaac is very very simple.
The app works perfectly we can find who is the richest person.
So until now you know how you can use the if statement the if statement is really for decision making.
And right now you can see that you don't necessarily need the Boolean value the true and the false rate
in somewhere or stored somewhere in a variable.
You can just compare a you can compare a string with a string and expect to have a true or false value
so let's say for example if two things are not the same or it doesn't respect the comparison.
Well this means it's not true.
And if it respects it well this means it's true.
So let's say a number is greater than another number.
Well this means it respects the comparison of this number is greater than this number.
So in this case it's true.
So I hope you guys understand right now that you can work with the if statement this week and this will
be very very useful for you guys in the future when you will become more advanced in this programming
language.
So that's it for this class.
Guys see you all in our next class.