Python for .NET Developers Transcripts
Chapter: The Python Language
Lecture: Concept: Code structure in Python
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Now lets quickly review the shape of a Python program. Or the shape of code structure within the Python language. I want to make a quick comment though
0:09
about these little review segments these metacomment if you will. Some folks have said, hey we've just talked about this
0:16
why are you showing me the same thing? I'm going to show you the structure and so on and highlight a few important things.
0:21
One, well, I think review is important and we talked about it for twelve minutes let's summarize it in thirty seconds.
0:26
I think that's helpful for remembering. The other thing, though, this is meant to be reference material for you.
0:32
These little tiny things are super easy they're going to be separate videos you can just go back and jump into and look at.
0:38
Oh, how do I do iteration with something like IEnumerable collection? I'm going to jump into that concept review really quick
0:45
and just catch that in two minutes, right. When you're coming back a couple months later most video courses have a really bad
0:51
or they're just absent of any reference material so these are your little reference points you can come back really quick.
0:58
Stepping out of that metacomment there let's talk about the shape of a Python program. Python defines code blocks, technically called code suites
1:05
but I like to call them code blocks so I'm just going to keep doing that and it uses colons to initiate them
1:10
and then white space to define what is in it. So, here's a single source file that I've put a bunch
1:14
of colored boxes around to highlight each code block that is happening, code suite if you will. So, everything here, these are all code suites.
1:22
The blue boxes, they're defined by the def some_method and then a colon or def main colon and then anything indented in four spaces
1:31
or more is defined to be within that code block. And then, we have an if statement in there and a colon, and we indented four more.
1:38
That's a print hello old friend. Then else, that's a not indented, different code block colon, indent, that's a bunch more.
1:46
We have two print statements in there, and so on. So, this is Python's alternative view of the world on structuring code.
1:53
Instead of using curly braces and semi-colons use colons and white space. A couple things to note:
1:58
Obviously no semi-colons, code blocks start with a colon but white space really, really matters. There are no braces, there's no parentheses
2:09
tab as an item inside the file is not your friend. These have to be spaces, but all the important editors
2:15
know that, and when you press tab, it puts four spaces when you hit delete, it removes four spaces things like that. There's actually an exception
2:22
like, hey I found a tab in your code exception type in Python. Alright, so this is how Python defines the structure of code.
2:30
It might seem unusual at first but trust me, you'll get used to it and it actually works out really nicely.