WEBVTT

00:01.060 --> 00:07.420
Programming often involves examining a set of conditions and deciding which action to take based on

00:07.420 --> 00:08.440
these conditions.

00:08.470 --> 00:15.580
Python's If statement allows you to examine the current state of a program and respond appropriately

00:15.580 --> 00:16.510
to that state.

00:16.540 --> 00:22.660
In this section, you will learn to write conditional tests which allow you to check any condition of

00:22.660 --> 00:27.970
interest, and you will learn to write simple if statements and you will learn how to create a more

00:27.970 --> 00:35.320
complex series of if statements to identify when the exact conditions you want are present, and you

00:35.320 --> 00:41.410
will then apply this concept to lists and so you will be able to write a for loop that handles most

00:41.410 --> 00:47.070
items in a list one way, but handles certain items in a with specific values in different way.

00:47.080 --> 00:57.440
So this example here we will show the if tests that let you respond to special situations correctly.

00:57.460 --> 01:02.810
Imagine you have a list of cars and you want to print out the name of each car.

01:02.810 --> 01:04.910
So car names are proper names.

01:04.910 --> 01:09.440
So the names of most cars will be printed in title names title case.

01:09.470 --> 01:16.160
However, the value BMW here should be printed in all uppercase and here we will.

01:16.160 --> 01:17.900
Which we will do that right now.

01:17.900 --> 01:23.120
So we will car makers, car makers here.

01:23.120 --> 01:25.130
And after that we will equal to.

01:25.160 --> 01:33.680
So first we will enter the Volkswagen, the BMW, the Toyota, Toyota.

01:37.020 --> 01:37.710
Port.

01:38.650 --> 01:39.430
And.

01:41.760 --> 01:49.740
And here we will use the four car in car makers and here.

01:49.740 --> 02:02.430
So if car equals to BMW, then we will print the car dot upper and else here we will.

02:02.460 --> 02:07.650
Else we will print the car dot title.

02:07.650 --> 02:08.550
That's it.

02:08.550 --> 02:10.500
So now let's run it.

02:10.890 --> 02:11.460
That's it.

02:11.460 --> 02:12.090
Here.

02:12.360 --> 02:12.680
Oops.

02:13.050 --> 02:17.820
If BMW oops if BMW, that's it.

02:18.180 --> 02:20.130
Now this will work here.

02:21.450 --> 02:24.240
So now the loop in this example.

02:24.240 --> 02:25.260
First checks.

02:25.260 --> 02:32.460
If the current value of a car, if the current value of a car is BMW.

02:33.760 --> 02:39.440
If it is, the value is printed in uppercase here with this function.

02:39.460 --> 02:40.030
Right.

02:40.300 --> 02:47.050
If the value of a car is anything other than a BMW, it is printed in a title case.

02:47.170 --> 02:49.840
And here this is our output here.

02:50.020 --> 02:58.000
All of these names are written in title case, but here BMW is written in uppercase.

02:58.000 --> 03:02.730
So this example combines the number of concepts you will learn about in this section.

03:02.740 --> 03:09.220
Let's begin by looking at the kinds of tests you can use to examine the conditions in your program,

03:09.220 --> 03:12.850
so we can also use conditional tests in Python.

03:12.850 --> 03:18.820
So at the heart of every if statement is an expression that can be evaluated as true.

03:19.060 --> 03:20.800
As true.

03:22.630 --> 03:24.460
Or false, Right?

03:29.990 --> 03:41.630
So Python uses the values true and false to decide to decide whether the code is in a if statement should

03:41.630 --> 03:42.950
be executed or not.

03:42.980 --> 03:46.550
So if a conditional test evaluates true.

03:47.470 --> 03:52.240
Then Python executes the code following the if statement.

03:52.240 --> 03:54.550
So if the test evaluates false.

03:54.910 --> 03:58.540
If python ignores the code following the if statement.

03:58.930 --> 04:00.490
We can also check for equality.

04:00.490 --> 04:06.130
So the most conditional test compare the current value of a variable to a specific value of interest.

04:06.130 --> 04:13.990
So the simplest conditional test check test checks whether the value of a variable is equal to the value

04:13.990 --> 04:14.860
of interest.

04:14.890 --> 04:20.350
In order to do that, we will just delete for now our for loop and we will print here.

04:20.740 --> 04:25.870
Now car equals BMW here.

04:26.050 --> 04:31.120
Or let's actually, instead of writing that, let's use car makers.

04:31.120 --> 04:36.370
BMW index is one BMW and BMW here.

04:36.610 --> 04:41.740
So now as you can see we got an error cannot assign to a function call.

04:41.740 --> 04:46.480
So here that is we're going to get an error if we try to do that.

04:46.480 --> 04:50.960
However, if you try this in Python console, which we will now.

04:52.850 --> 04:53.270
Here.

04:53.270 --> 04:54.650
We opened our python.

04:54.650 --> 04:59.090
Now you can reach this by using just python.

05:00.900 --> 05:05.070
And here you're going to see this python 64 bit or 32 bit.

05:05.100 --> 05:09.450
You just enter that and here you can see the new tab opened.

05:09.480 --> 05:12.050
Here, let's actually make it closer.

05:12.060 --> 05:13.290
So most conditional.

05:13.290 --> 05:17.940
Let's ah, compare the current value of a variable to a specific value of interest.

05:17.970 --> 05:22.170
Let's create a new value named car here and make it BMW.

05:22.410 --> 05:26.850
And after that we will check the is car equal to BMW.

05:26.880 --> 05:30.730
And here you can you can see we got true.

05:30.750 --> 05:34.530
So the first line here actually, let me get this.

05:34.530 --> 05:38.010
So this first line, let me fix this.

05:38.040 --> 05:41.190
Now here, this first line here.

05:42.330 --> 05:44.250
Sets the value of a car.

05:44.890 --> 05:51.880
Car sets the value of a car to be actual b m.

05:51.880 --> 05:52.840
W.

05:54.320 --> 05:56.720
Using a single equal sign.

05:56.720 --> 06:00.860
So here we use the single equal sign here.

06:00.860 --> 06:01.460
Right?

06:02.860 --> 06:05.770
As you've seen many times already, this sign.

06:05.770 --> 06:12.460
So the next line checks the whether the value of a cars is BMW.

06:12.490 --> 06:14.590
Here you see here.

06:16.000 --> 06:16.950
Of sexual issues.

06:16.960 --> 06:18.310
Different color here.

06:21.540 --> 06:27.630
So now we are checking this by using a double equal sign.

06:28.170 --> 06:30.360
Double equal sign.

06:32.120 --> 06:32.660
Here.

06:33.020 --> 06:34.010
Car.

06:35.450 --> 06:36.280
Is.

06:37.700 --> 06:38.540
We?

06:39.440 --> 06:40.940
And W.

06:42.150 --> 06:46.830
So this is the equality operator.

06:46.950 --> 06:54.090
So and so this equality operator here, this is this is named equality Operator.

06:54.090 --> 06:57.150
Let me actually write that here.

06:57.180 --> 06:59.880
This is equality.

07:06.160 --> 07:07.120
Operator.

07:12.950 --> 07:18.710
So here this equality operator returns true, like in this case.

07:19.780 --> 07:25.030
If the values on the left and the right side of the operator match in this case, these are match.

07:25.030 --> 07:28.420
So car is actually equals to BMW.

07:28.450 --> 07:30.400
Actually, let me use the different color.

07:30.400 --> 07:35.500
So here our car is equals to BMW here.

07:36.480 --> 07:39.240
Because we already assign that in first line.

07:39.240 --> 07:43.410
And our BMW here equals to BMW, Right.

07:43.800 --> 07:52.500
So we are checking this BMW W and here they match.

07:52.530 --> 07:55.710
In this case, they will return.

07:55.740 --> 07:56.490
True.

07:58.310 --> 07:59.150
That's it.

07:59.570 --> 08:02.600
But here the values in this example.

08:02.600 --> 08:03.500
Match.

08:03.500 --> 08:04.160
Match.

08:04.160 --> 08:06.830
So Python returns true.

08:07.130 --> 08:15.380
So when the value of a car is anything other than a BMW, for example, let's try car equals equal the

08:15.620 --> 08:27.590
jeep here we will get false if car equal equal uppercase B M-W We will get false again because these

08:27.590 --> 08:34.580
equations equalization equation equality operator are case sensitive in strings.

08:34.580 --> 08:42.830
So here if we try something ice cream, ice cream, we will get false again.

08:43.450 --> 08:44.290
So.

08:45.450 --> 08:47.910
That's it here with operator.

08:47.910 --> 08:51.150
So a single equal sign is really a statement.

08:51.150 --> 08:58.410
And you might read the first line of a code here that we are setting a set the value of a car equal

08:58.410 --> 08:59.010
to.

09:00.440 --> 09:00.670
It.

09:00.860 --> 09:07.100
For example, Jeep, on the other hand, a double equal sign ask asks a question.

09:07.100 --> 09:08.900
Let me actually write it down.

09:09.200 --> 09:13.760
Some new programmers might confuse this with equate initialization.

09:14.970 --> 09:15.560
Here.

09:15.570 --> 09:19.530
So this this double equal.

09:19.740 --> 09:23.970
Double equal signs asks the Or.

09:27.830 --> 09:30.200
Here asks this.

09:32.250 --> 09:35.130
I get this text here, this ask this.

09:35.130 --> 09:39.960
So is the value of car.

09:40.960 --> 09:44.040
Equal to BMW.

09:45.140 --> 09:47.030
So here our.

09:49.130 --> 09:51.650
W equate equality.

09:51.860 --> 10:00.920
W equal sign asks whether value of this value of a car or a car in this case in is variable, right?

10:01.130 --> 10:10.730
Because we use this car as a variable name and here is the value of car equal to BMW.

10:10.730 --> 10:12.170
In this case it's equal.

10:12.170 --> 10:14.480
So it will return.

10:14.480 --> 10:15.230
True.
