[MUSIC]
So we talked about variable types, and
we talked about small transformations.
So now, let's go to the next step.
In the next step we're going to do conditional statements.
So Esc+M, I'm gonna define another section here,
Conditional statements in python, okay.
So there are various ways to build a conditional statement
in Python depending on how long and how complicated it is.
So let's do a very simple one.
For example, let's say that I want to ask if
the integer variable i that I created is equal to 1.
That's my test.
And the float variable that we created is > 4.
That's my conditional statement.
It's easy to read, and
if this is the case, colon, we put a colon after the if statement.
We do something.
Now in Python, there's no open brackets and
close brackets like you have in C for example or in Java.
All you have to do is put any amount of spacing or tabulation in the next line.
And that's what defines kind of the pieces that will be inside the brackets,
inside of another programming language.
And you just keep aligning them the same way.
And iPython Notebook is nice because it does all the tabulation
automatically for you.
So let's say if i == 1 and f > 4 something happen.
So I'm gonna print
the value of i is 1 and
f is greater than 4.
Now, I'm gonna just print that if this is the case.
But I also wanna have another condition, I wanna have an else condition.
So in Python, you could just type else something, but
I want to have multiple conditions.
So I wanna do another else if, if some other condition is true.
And Python allows you to concatenate elses and ifs together.
So if you just create an elif, it's kind of a cute little syntactic sugar here.
So if the first condition, i == 1, f > 4 does not hold, ask the following question.
Is i > 4?
Or, so there's ands.
So I'm gonna put a nice little space here.
Ands and ors, you just say or.
You don't have any kind of that fancy vertical brackets or anything.
Let's just say and and or, f > 4.
Do something else.
So our something
else is print i and
f are both greater than 4.
Now if this is not true, we can add another else at the end.
So I'm gonna backspace and align in the beginning.
Just put another else with a colon at
the end and say if this is not the case,
then you say print say both.
So if this condition is not the case then what we know.
We know that i is not greater than 4 and i is not equal to 1.
f is not greater than 4.
So there's some things that have happened.
So we can just say something like both i and
f are less than or equal to 4, and we also have that,
well let's say that this is enough.
Okay, I'm done.
I'm done with my statement and now remember the values of i and f.
I just printed it out here.
i was 4 and f was 4.1.
So if you go through the first line, you notice that i is not equal to 1.
So you go to the next line, you notice that i is not greater than 4.
So you go to the next line, you're just going to print this last statement,
both i and f are less than or equal to 4.
So if I do Shift+Enter, I execute this conditional, and there you go.
i and f are both greater than 4.
So, that was good.
We built our first conditional here.
It's all fine.
Now that we've done some conditionals, let's do some loops.
So, again, I'm gonna go into
the editing mode markdown Esc+M,
and we're gonna do some conditional loops.
So, what kind of conditional loops can we do in Python?
We can do for loops.
So for example, we can do a for, so remember we had a list.
This is kind of fun.
So let me just print it to remind ourselves.
So if I print a list l, that we built in the beginning, it was 3, 1, 2.
So let's do for a loop that iterates over that list.
So for, let's say element in l For
each element in l, I'm going to do something.
I'm going to print the element.
So, this goes element by element of l.
And I just call it element, I could have called it anything.
Let's call it e.
So, for e in l, print it.
So it's going to go 3 print, 1 print, 2 print.
Let's execute that and you see 3 1 2.
And notice in Python when you call print, you always insert a new line at the end,
so you've got 3 1 2 in your lines.
Now that is a simple for loop.
Lastly, let's create a simple while loop.
So you can do fors, you can do whiles.
So for example, while might say, while a counter is,
let's just say < 10, do something.
So I'm going to print the counter.
And, I'm going to add 1 to the counter.
So, I'm going to say counter, += 1.
So, what this is going to do is create a counter and print it, and add 1,
print it, add 1, until we get to 10, when it's going to stop.
So, notice that we never initialize counter, which should give us an error.
And so let's just run it and see what happens.
We got an error, and whenever you get an error in Python,
the error messages sometimes can be a little scary.
So you just go here and look at the name of error.
And it says down here, name counter is not defined.
Remember, we never initialized counter.
And it actually goes and
tells you a little above what line of the code that error occurred in.
So when it first started test while the counter is less than 10,
counter is never initialize, it broke.
So let's go back and edit.
I'm gonna insert a new line here.
Let's initialize a counter first through any number less than 10.
So, how about 6?
So, counter = 6, and I'm just gonna execute again.
And you'll see now it prints 6, 7, 8, 9.
When it gets to 10, stops.
[MUSIC]