Programming often involves examining a set of conditions and deciding which action to take based on
these conditions.
Python's If statement allows you to examine the current state of a program and respond appropriately
to that state.
In this section, you will learn to write conditional tests which allow you to check any condition of
interest, and you will learn to write simple if statements and you will learn how to create a more
complex series of if statements to identify when the exact conditions you want are present, and you
will then apply this concept to lists and so you will be able to write a for loop that handles most
items in a list one way, but handles certain items in a with specific values in different way.
So this example here we will show the if tests that let you respond to special situations correctly.
Imagine you have a list of cars and you want to print out the name of each car.
So car names are proper names.
So the names of most cars will be printed in title names title case.
However, the value BMW here should be printed in all uppercase and here we will.
Which we will do that right now.
So we will car makers, car makers here.
And after that we will equal to.
So first we will enter the Volkswagen, the BMW, the Toyota, Toyota.
Port.
And.
And here we will use the four car in car makers and here.
So if car equals to BMW, then we will print the car dot upper and else here we will.
Else we will print the car dot title.
That's it.
So now let's run it.
That's it.
Here.
Oops.
If BMW oops if BMW, that's it.
Now this will work here.
So now the loop in this example.
First checks.
If the current value of a car, if the current value of a car is BMW.
If it is, the value is printed in uppercase here with this function.
Right.
If the value of a car is anything other than a BMW, it is printed in a title case.
And here this is our output here.
All of these names are written in title case, but here BMW is written in uppercase.
So this example combines the number of concepts you will learn about in this section.
Let's begin by looking at the kinds of tests you can use to examine the conditions in your program,
so we can also use conditional tests in Python.
So at the heart of every if statement is an expression that can be evaluated as true.
As true.
Or false, Right?
So Python uses the values true and false to decide to decide whether the code is in a if statement should
be executed or not.
So if a conditional test evaluates true.
Then Python executes the code following the if statement.
So if the test evaluates false.
If python ignores the code following the if statement.
We can also check for equality.
So the most conditional test compare the current value of a variable to a specific value of interest.
So the simplest conditional test check test checks whether the value of a variable is equal to the value
of interest.
In order to do that, we will just delete for now our for loop and we will print here.
Now car equals BMW here.
Or let's actually, instead of writing that, let's use car makers.
BMW index is one BMW and BMW here.
So now as you can see we got an error cannot assign to a function call.
So here that is we're going to get an error if we try to do that.
However, if you try this in Python console, which we will now.
Here.
We opened our python.
Now you can reach this by using just python.
And here you're going to see this python 64 bit or 32 bit.
You just enter that and here you can see the new tab opened.
Here, let's actually make it closer.
So most conditional.
Let's ah, compare the current value of a variable to a specific value of interest.
Let's create a new value named car here and make it BMW.
And after that we will check the is car equal to BMW.
And here you can you can see we got true.
So the first line here actually, let me get this.
So this first line, let me fix this.
Now here, this first line here.
Sets the value of a car.
Car sets the value of a car to be actual b m.
W.
Using a single equal sign.
So here we use the single equal sign here.
Right?
As you've seen many times already, this sign.
So the next line checks the whether the value of a cars is BMW.
Here you see here.
Of sexual issues.
Different color here.
So now we are checking this by using a double equal sign.
Double equal sign.
Here.
Car.
Is.
We?
And W.
So this is the equality operator.
So and so this equality operator here, this is this is named equality Operator.
Let me actually write that here.
This is equality.
Operator.
So here this equality operator returns true, like in this case.
If the values on the left and the right side of the operator match in this case, these are match.
So car is actually equals to BMW.
Actually, let me use the different color.
So here our car is equals to BMW here.
Because we already assign that in first line.
And our BMW here equals to BMW, Right.
So we are checking this BMW W and here they match.
In this case, they will return.
True.
That's it.
But here the values in this example.
Match.
So Python returns true.
So when the value of a car is anything other than a BMW, for example, let's try car equals equal the
jeep here we will get false if car equal equal uppercase B M-W We will get false again because these
equations equalization equation equality operator are case sensitive in strings.
So here if we try something ice cream, ice cream, we will get false again.
So.
That's it here with operator.
So a single equal sign is really a statement.
And you might read the first line of a code here that we are setting a set the value of a car equal
to.
It.
For example, Jeep, on the other hand, a double equal sign ask asks a question.
Let me actually write it down.
Some new programmers might confuse this with equate initialization.
Here.
So this this double equal.
Double equal signs asks the Or.
Here asks this.
I get this text here, this ask this.
So is the value of car.
Equal to BMW.
So here our.
W equate equality.
W equal sign asks whether value of this value of a car or a car in this case in is variable, right?
Because we use this car as a variable name and here is the value of car equal to BMW.
In this case it's equal.
So it will return.
True.