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