-
Not Synced
1
00:00:04,960 --> 00:00:10,270
Well, in this video, I'm going to talk with you about some of the basic control structures that we have
-
Not Synced
2
00:00:10,270 --> 00:00:21,050
in Python and the syntax that Python uses for indicating different pieces of code.
-
Not Synced
3
00:00:21,050 --> 00:00:25,490
The learning outcomes for this video are for you to be able to write basic python control structures.
-
Not Synced
4
00:00:25,490 --> 00:00:32,180
Understand the python block syntax. No, the standard practice for using whitespace in Python.
-
Not Synced
5
00:00:32,180 --> 00:00:38,990
One of the key concepts here is that Python uses whitespace to detect blocks such as the bodies of loops or conditionals.
-
Not Synced
6
00:00:38,990 --> 00:00:45,860
Unlike other languages such as Java, JavaScript, HPC, etc., all use squiggly braces.
-
Not Synced
7
00:00:45,860 --> 00:00:56,090
Python uses indentation as syntactically significant indicator of what a block is, such as the body of a loop.
-
Not Synced
8
00:00:56,090 --> 00:01:03,830
So in the first Python intro video, we saw a for loop in the for loop iterates over it iterable.
-
Not Synced
9
00:01:03,830 --> 00:01:11,060
So you the, the, the syntax is that we have for ie the variable name in.
-
Not Synced
10
00:01:11,060 --> 00:01:17,540
Range of music range here, but this is the iterable expression.
-
Not Synced
11
00:01:17,540 --> 00:01:27,770
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.
-
Not Synced
12
00:01:27,770 --> 00:01:32,630
Remember the F string that says use variables in the string.
-
Not Synced
13
00:01:32,630 --> 00:01:41,480
And so we are we're printing out the iteration number through each iteration of this loop.
-
Not Synced
14
00:01:41,480 --> 00:01:46,360
And then at the end of the loop, we're gonna print done, and that's gonna happen once.
-
Not Synced
15
00:01:46,360 --> 00:01:52,440
Because whitespace still limits blocks.
-
Not Synced
16
00:01:52,440 --> 00:02:00,400
The colon and the and the indentation, they indicate that we're in a new block.
-
Not Synced
17
00:02:00,400 --> 00:02:06,200
And then when the indentation stops and it goes back out to the same level.
-
Not Synced
18
00:02:06,200 --> 00:02:12,220
As the for loop started, that indicates the end of the block.
-
Not Synced
19
00:02:12,220 --> 00:02:17,710
So, as I said, the python block begins with a colon at the end of the line.
-
Not Synced
20
00:02:17,710 --> 00:02:26,020
And there's several different python keywords that. Are something that can start a block the if family f.
-
Not Synced
21
00:02:26,020 --> 00:02:36,610
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.
-
Not Synced
22
00:02:36,610 --> 00:02:40,390
The standard practice here is to use force bases.
-
Not Synced
23
00:02:40,390 --> 00:02:47,830
Jupiter, along with most modern python editing environments, default their configuration to force spaces for you automatically.
-
Not Synced
24
00:02:47,830 --> 00:02:51,970
But this convention, while Python does not strictly mandate this.
-
Not Synced
25
00:02:51,970 --> 00:02:58,700
All it mandates is that you are consistent. You can't say mixed tabs and spaces in the same file.
-
Not Synced
26
00:02:58,700 --> 00:03:03,670
This almost universally standard python practice is to indent with four spaces
-
Not Synced
27
00:03:03,670 --> 00:03:12,040
and then the block ends when the indentation returns to the previous level.
-
Not Synced
28
00:03:12,040 --> 00:03:17,410
You can also have comments in Python, a comment begins with a hash and continues until the end of the line.
-
Not Synced
29
00:03:17,410 --> 00:03:24,700
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.
-
Not Synced
30
00:03:24,700 --> 00:03:25,990
When we're writing Jupiter notebooks,
-
Not Synced
31
00:03:25,990 --> 00:03:34,360
we're going to put a lot of the discussion and the explanation in markdown cells in the Jupiter notebook rather than in comments.
-
Not Synced
32
00:03:34,360 --> 00:03:39,040
But comments are very useful when you're when you start writing Python scripts.
-
Not Synced
33
00:03:39,040 --> 00:03:42,970
They're also useful when you want when you want to write.
-
Not Synced
34
00:03:42,970 --> 00:03:50,860
Just really briefly, why a particular line in one of your code cells is working the way that it's working?
-
Not Synced
35
00:03:50,860 --> 00:03:54,370
The if statement is structure.
-
Not Synced
36
00:03:54,370 --> 00:03:58,180
It works like the fourth statement in terms of how the blocks work.
-
Not Synced
37
00:03:58,180 --> 00:04:03,040
And we open with if we don't need any parentheses, we have F and we have an expression in this case,
-
Not Synced
38
00:04:03,040 --> 00:04:13,150
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.
-
Not Synced
39
00:04:13,150 --> 00:04:20,200
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.
-
Not Synced
40
00:04:20,200 --> 00:04:28,090
But it's not else F or else, if it's just L.F. e-Life is the python syntax for else F.
-
Not Synced
41
00:04:28,090 --> 00:04:33,460
So if it's not greater than 10 but it is greater than or equal to five, it's going to print medium.
-
Not Synced
42
00:04:33,460 --> 00:04:37,780
And that's the one we're actually going to run in this case because X is equal to five.
-
Not Synced
43
00:04:37,780 --> 00:04:45,940
And then finally. Else we're going to print small. So in Python there are several things that are considered false.
-
Not Synced
44
00:04:45,940 --> 00:04:54,290
For the purpose. Most for the purposes of. Of an if statement.
-
Not Synced
45
00:04:54,290 --> 00:05:02,900
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.
-
Not Synced
46
00:05:02,900 --> 00:05:07,520
That is the python's version of a no means no data here.
-
Not Synced
47
00:05:07,520 --> 00:05:15,650
Zero is false. Empty containers, empty lists, sets, tuples and empty strings are all also false.
-
Not Synced
48
00:05:15,650 --> 00:05:21,980
Most other things are true. But those are the.
-
Not Synced
49
00:05:21,980 --> 00:05:26,600
Those are the. That's how ifs work in Python. They aren't strict like in Java.
-
Not Synced
50
00:05:26,600 --> 00:05:30,220
It has to be a boolean python does not require it to be a billion.
-
Not Synced
51
00:05:30,220 --> 00:05:38,990
It requires to be something that can be converted to a boolean. And then these are the things that.
-
Not Synced
52
00:05:38,990 --> 00:05:50,000
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.
-
Not Synced
53
00:05:50,000 --> 00:05:56,240
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.
-
Not Synced
54
00:05:56,240 --> 00:06:01,070
Because while loops can't be empty in general, Bloks cannot be empty.
-
Not Synced
55
00:06:01,070 --> 00:06:04,790
So passe is a python statement that does nothing.
-
Not Synced
56
00:06:04,790 --> 00:06:10,280
It's just needed when you need to make something syntactically valid. Maybe you're in the middle of testing some code.
-
Not Synced
57
00:06:10,280 --> 00:06:16,250
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.
-
Not Synced
58
00:06:16,250 --> 00:06:26,060
Do nothing. For now, you can just say pass. So to wrap up, Python provides the usual control control structures for a programing language.
-
Not Synced
59
00:06:26,060 --> 00:06:33,620
If for while blocks crucially are based on indentation and the standard there is to use for spaces.
-
Not Synced
60
00:06:33,620 --> 00:06:40,910
The hash sign starts a comment. There is some limitations to working with for loops.
-
Not Synced
61
00:06:40,910 --> 00:06:56,433
We're not going to use for work to working with, well, any loops in Python.
-
Not Synced