Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 2: Guess that number game
Lecture: Concept: Shape of Python code (blocks and suites)
0:00 A core concept of this application is to understand the shape of a Python program. So far, until very recently,
0:08 we've been just writing straight top to bottom code, with no blocks, no functions, no conditionals or loops
0:16 or anything that has to be disambiguated in that sense. And now, what I want to focus on is this concept of a shape of the Python program.
0:25 Everything you see on the screen that is in the square, the gray square, the blue squares, the purple squares, those are all separate code blocks,
0:33 sometimes called code suites in Python that are executed in some particular manner. The blue blocks are functions
0:40 that are executed when the functions are called, the purple blocks are if or else blocks
0:45 that are executed depending on whether the argument is batch or something else. The key thing that defines code blocks in Python
1:02 C#, Swift, those types of languages, they all use curly braces and when you open the curly brace that begins a block of code,
1:10 and when you close curly brace that block code is now done. It doesn't work this way in Python, it's all about the indentation,
1:16 so you can see the gray block which is just the overall program and we have two functions we are defining, we have def main and we say colon,
1:26 and then we indent all the body of this function. And that body is made up of those two blocks in there,
1:32 there is two purple blocks sort of the if else statement. Then we unindent and we define another run function
1:37 the details don't matter, we say colon and indent again and that's the body of the function for a run.
1:43 So you've already seen previously that editors like PyCharm and other Python intelligent editors know about this shape,
1:51 they know about this code blocks and if you type if something colon enter, it will automatically indent and stay intended
1:58 until you unintend it manually. While it might seem a little challenging to use spaces to define a structure,
2:05 it turns out the editors know all about it and make it super easy. One more comment I should add while we are talking about indentation or white space,
2:13 tabs? tabs are not a good idea, in Python, Python does not like tabs. This white space should be actual the space character, not tabs,
2:24 now that doesn't mean that you don't press tab in your editor like in PyCharm if you hit tab it will indent but it actually just means
2:30 it's going to insert four spaces for you and many of the Python editors are like that. So if you are coming to Python from another language
2:38 that does not use significant white space like C++ or some of the C based languages
2:44 this could feel a little strange to you and it does take a week or so to really become super comfortable with this idea but once you do
2:52 it's very natural and it's a lovely way to program but I know that some of you out there you are thinking WOW!
2:59 this is really different and to you I say just give it a try, use a nice editor and you will start to love it right away.
3:06 if this feels totally natural and good to you, well then you are going to love Python right from the beginning.