Well, in this video, I'm going to talk with you about some of the basic control structures that we have
in Python and the syntax that Python uses for indicating different pieces of code.
The learning outcomes for this video are for you to be able to write basic python control structures.
Understand the python block syntax. No, the standard practice for using whitespace in Python.
One of the key concepts here is that Python uses whitespace to detect blocks such as the bodies of loops or conditionals.
Unlike other languages such as Java, JavaScript, HPC, etc., all use squiggly braces.
Python uses indentation as syntactically significant indicator of what a block is, such as the body of a loop.
So in the first Python intro video, we saw a for loop in the for loop iterates over it iterable.
So you the, the, the syntax is that we have for ie the variable name in.
Range of music range here, but this is the iterable expression.
That's something that we can we can loop over and then within within that loop, we are going to we're gonna print and we're using an F string.
Remember the F string that says use variables in the string.
And so we are we're printing out the iteration number through each iteration of this loop.
And then at the end of the loop, we're gonna print done, and that's gonna happen once.
Because whitespace still limits blocks.
The colon and the and the indentation, they indicate that we're in a new block.
And then when the indentation stops and it goes back out to the same level.
As the for loop started, that indicates the end of the block.
So, as I said, the python block begins with a colon at the end of the line.
And there's several different python keywords that. Are something that can start a block the if family f.
Else L.F., the loop's foreign while and then the keywords for defining functions and classes, def and class block contents indented one level.
The standard practice here is to use force bases.
Jupiter, along with most modern python editing environments, default their configuration to force spaces for you automatically.
But this convention, while Python does not strictly mandate this.
All it mandates is that you are consistent. You can't say mixed tabs and spaces in the same file.
This almost universally standard python practice is to indent with four spaces
and then the block ends when the indentation returns to the previous level.
You can also have comments in Python, a comment begins with a hash and continues until the end of the line.
You can have a car line where all it is is a comment. You can also put a comment at the end of a line that contains some code.
When we're writing Jupiter notebooks,
we're going to put a lot of the discussion and the explanation in markdown cells in the Jupiter notebook rather than in comments.
But comments are very useful when you're when you start writing Python scripts.
They're also useful when you want when you want to write.
Just really briefly, why a particular line in one of your code cells is working the way that it's working?
The if statement is structure.
It works like the fourth statement in terms of how the blocks work.
And we open with if we don't need any parentheses, we have F and we have an expression in this case,
we're gonna say if X is greater than or equal to 10, then we're one to print big if Elif is the python else.
If you can have as many of these as you want and if you don't have to have one, you don't have to have an else either.
But it's not else F or else, if it's just L.F. e-Life is the python syntax for else F.
So if it's not greater than 10 but it is greater than or equal to five, it's going to print medium.
And that's the one we're actually going to run in this case because X is equal to five.
And then finally. Else we're going to print small. So in Python there are several things that are considered false.
For the purpose. Most for the purposes of. Of an if statement.
The bout valued the boolean value false, which you write with an uppercase F, is considered false as is none, which is a special python value.
That is the python's version of a no means no data here.
Zero is false. Empty containers, empty lists, sets, tuples and empty strings are all also false.
Most other things are true. But those are the.
Those are the. That's how ifs work in Python. They aren't strict like in Java.
It has to be a boolean python does not require it to be a billion.
It requires to be something that can be converted to a boolean. And then these are the things that.
These are the things that Python converts to false when it's doing that boolean conversion a while loop iterates until a condition becomes false.
I'd put a past statement here. The past is not part of the while loop. The past is just here to make the syntactically valid python.
Because while loops can't be empty in general, Bloks cannot be empty.
So passe is a python statement that does nothing.
It's just needed when you need to make something syntactically valid. Maybe you're in the middle of testing some code.
Maybe you're working on an F and you don't have all that figured out yet. So you just want to make one of the branches of the F.
Do nothing. For now, you can just say pass. So to wrap up, Python provides the usual control control structures for a programing language.
If for while blocks crucially are based on indentation and the standard there is to use for spaces.
The hash sign starts a comment. There is some limitations to working with for loops.
We're not going to use for work to working with, well, any loops in Python.