Python for the .NET developer Transcripts
Chapter: The Python Language
Lecture: Code structure demo
0:00 Our Python project is ready to create in PyCharm.
0:03 We've created our virtual environment here
0:07 and on macOS, you can load files and directories
0:09 by just dragging and dropping them on here.
0:13 On Windows, you would just say File, Open Directory
0:15 but we get this little shortcut here.
0:18 Now, we don't have any source files at all.
0:20 You do want to check really quick in the terminal
0:22 that your venv got activated.
0:25 Turns out, in this case, annoyingly, it did not
0:28 so we can go really quickly to the settings
0:31 to the project, Project Interpreter
0:34 and sometimes PyCharm detects it, sometimes it doesn't.
0:37 I don't know why.
0:38 You see it's detected it here and suggesting it
0:40 but these are our projects we got in there
0:42 or our libraries that just manage installs.
0:45 This is kind of like NuGet for you all.
0:48 Okay, so now, check one more time, notice this is here.
0:51 We can ask which python.
0:52 On Windows, that's where python, but it's the right one.
0:55 So, what we can do is we can come over here
0:57 and create a project just like we had in C#.
1:00 We called it ch03_lang
1:02 I'm going to make it lowercase.
1:03 It's more of a convention in Python.
1:05 There's more lowercasing of things
1:06 so chapter, let's go with ch03_lang
1:09 'cause we're going to have more than 10.
1:12 And in here, we can add a new Python file
1:17 and let's call it program.
1:20 So, let's remind ourselves
1:22 it's been a whole couple minutes
1:24 about what we're going to try to put here.
1:26 This is what we want to simulate
1:28 or to recreate, rather, in Python.
1:31 We want to have a method that runs
1:34 and is going to ask, what is your name?
1:38 Get that, pass it to another method
1:40 check that for lowercase and trim it
1:41 and I see one of these two things
1:43 based on the outcome there.
1:45 Alright, so how do we do that over here?
1:47 Well, it turns out to be pretty easy.
1:50 We're going to come and we're going to define a function
1:52 let's call it run at first.
1:55 Let's call it main; that's a little more common in Python.
1:58 Like so. Now, the way we define a function is
2:02 we use the keyword def.
2:04 Either it's a member function of a class
2:07 or it's just a standalone function
2:08 we define it with the keyword def.
2:10 We do not specify a return value.
2:12 More on that later.
2:14 This one doesn't take any arguments
2:16 and instead of having curly braces
2:18 this is where it gets a little funky
2:19 instead of having curly braces like this
2:21 you can see PyCharm does not like the curly braces.
2:24 It's like, no, no, no, something is really wrong here.
2:26 We do use them but for other things in the language.
2:29 It's as you expected, a colon.
2:30 So, what we do is we'd write code and we define blocks.
2:33 What would be the curly braces in C#
2:37 in Python we'd say colon.
2:39 Then what we do is a little bit funky
2:41 the very first time you see it
2:42 but it turns out to be really nice over time
2:44 and actually, I love it a lot now these days
2:47 is we indent four spaces.
2:49 So, see when I hit Enter, PyCharm knows I indent four spaces
2:52 and automatically did that for me
2:54 like those are four spaces right there.
2:57 Now, you might think, oh, my gosh
2:58 spaces, not tabs, that's crazy.
3:01 Well, it doesn't really matter so much
3:03 because you'll see that editors make this
3:04 more or less, transparent.
3:05 If I hit Back, notice those four spaces got deleted.
3:08 If I hit Tab, it goes forward four spaces like so.
3:14 So, basically, and also, when I hit colon Enter
3:16 it added those four spaces.
3:18 All the editors, VS Code, PyCharm
3:20 they know all about this structure
3:22 and they're very good at helping you write it correctly
3:25 or lint it back into the right shape if it's not there.
3:28 So, what did we do?
3:29 In C#, we said, next thing was get this
3:34 what is your name bit here, we're going to ask that
3:37 and then we want to get the name as a string.
3:39 So, we're going to define a variable called name.
3:41 We don't say string name, we just say name
3:44 leveraging the dynamic types of Python
3:48 and then we want to print on a message
3:50 and get that value back from the user.
3:52 That's super simple, we just say input
3:55 and you put the string. That's it.
3:58 So, we can go and run this now
3:59 and just for starters, see what we're getting already.
4:02 I encourage you to run, make little changes
4:04 run, make little changes, and so on.
4:06 So, what we can do, this is not really an important warning
4:10 what we can do is we can just right-click over here
4:12 and say run.
4:13 It's not going to give you the outcome that you're hoping for
4:16 but let's just run it.
4:17 So, it ran it with Python from the virtual environment
4:20 and you can see the argument way at the end is that file
4:24 and nothing happened. That's weird.
4:26 Well, it turns out that Python doesn't have this concept
4:29 of a static main void that is the one
4:31 and only static main void.
4:34 You have to call the function at the very end of the file
4:38 like it is at startup of the application.
4:41 So, if we run that, does what is your name?
4:43 Cool, I'll say Michael.
4:45 It's cool with that, and we could print it out
4:47 and see what we got there, but notice
4:49 it did that little bit we were hoping for.
4:52 However, because sometimes we want to run this only
4:56 if we're targeting the application as the program
4:59 but we might want to use this as a library
5:01 and pull in other functions, there's this convention
5:04 which is, albeit weird, but you get used to it
5:07 is we put a little if down here
5:08 and there's these implicit values
5:10 that are defined for all the Python files, like __name__.
5:14 We ask if that is __main__.
5:17 Alright, so this is weird, but you get used to it
5:20 and if that's the case, then we'd run main.
5:22 And it's just not liking the spacing.
5:24 Let's run it again. Operate the same.
5:27 There we go. This is just the convention.
5:29 This is the static main void of Python
5:31 and because this is so common, I've created
5:35 within PyCharm, a little thing called if main.
5:39 If I hit Tab.
5:40 Oh, I didn't create over here, let's go create together.
5:41 It was in my other profile.
5:43 Let's go over to Live Templates
5:46 and let's just go to Python and let's add one.
5:51 So, it's very common to have a main
5:54 and we have this test down here. If...
6:05 Alright, it says we also have to define a context.
6:07 This is valid within Python.
6:09 Alright, so now we can just say if main like this.
6:13 So, we're going to use that throughout the rest of class
6:15 which is why I took the time to show you that
6:17 because we're going to write 20 or so programs
6:19 I don't want to type that in, I just want to say
6:21 we're just going to do this main trick.
6:22 So, remember, what we had was name equals input
6:26 what is your name? Like that.
6:29 Okay, so that was step one.
6:30 Now we want to define a second function up here
6:33 and it was called some_method, so we say def some_method.
6:37 In C#, we have camel case, like so, it would be like this.
6:42 In Python, we have, well, snake case, which looks like this.
6:45 Okay. This is the convention in the language
6:48 so that's how we're going to do it.
6:49 And then it takes an argument name
6:51 but we don't say string name, just like we don't up here.
6:53 We just say it takes a name. Excellent.
6:57 Now, what do we have to do after this?
6:59 We had an if statement, so we're seeing a little bit
7:02 about if.
7:03 We'll come here and say if, and then a boolean condition
7:05 and we don't have the parentheses, we omit them.
7:09 I'll say some more about that in a second
7:11 but let's do this test here. We have name dot.
7:15 Now, it doesn't give us a lot of help here.
7:19 We can trick it, and I'll show you how
7:21 to do this more in a second, but if we go down here
7:24 and type this, notice we actually get autocomplete
7:26 for all the string information.
7:28 We can actually specify some type information here
7:31 and PyCharm would totally pick it up.
7:33 For some reason, it's not.
7:34 Oh, hold on, I think if we were to call this
7:37 it might actually automatically do it.
7:41 Will it? There you go.
7:43 So, you can see, PyCharm's actually understanding the types
7:46 the real runtime types, that are being passed around
7:49 as we're using them here.
7:50 So, now, it's like, oh, you're calling this function
7:52 with a string, that must have string operations on it.
7:55 But there's a way to actually specify the types if you want
7:58 as we'll get to.
7:59 So, what have we did? We did a trim, which, in Python, is a strip
8:03 and we did a ToLower, which, in Python, is just a lower.
8:07 We said if that's equal to Michael, like that
8:10 define a code block here, like so.
8:13 I'm going to say print, that's like Console.WriteLine
8:17 hello, old friend.
8:20 We're going to do it just the same. Colon and print.
8:24 Nice to meet you, and we had the name here like this, right?
8:28 Well, it turns out, the string formatting in Python
8:30 and C# is almost identical.
8:32 We could do a dot format and pass a name value over
8:35 just like you can in C#, but we can also
8:38 remember, in C# we had a dollar, in Python we have an f
8:41 for formatted strings, and now notice
8:43 name is lit up like a variable.
8:46 And I'll also have the other one print my name is C#.
8:50 Exclamation, you're not Python. Here we go.
8:54 Alright, well, there's a little warning
8:56 that we need two lines by convention between those
8:58 so we can tell it to autoformat.
9:00 See down here, you can see the code
9:02 the key that I'm running, the hot key there.
9:05 Okay, I think we're ready to run it.
9:07 Have we created our C# equivalent in Python?
9:09 I think we have, what is your name?
9:11 Michael. Hello, old friend.
9:13 Let's try again. Sarah.
9:15 Nice to meet you Sarah, my name is Python.
9:17 Super cool.
9:19 This probably looks a little bit weird
9:21 to you if you've never seen Python and you're like
9:23 what is this space stuff? This space is crazy.
9:25 But like I said, it actually, you don't really deal
9:28 with spaces even though they're technically present
9:31 in the language.
9:32 These colon blocks are interesting.
9:34 The if statements here, notice, I could actually do this
9:38 if I was missing my parentheses
9:39 I could put them in there and it would still run
9:44 still runs okay, but the warning is
9:46 these parentheses are not needed.
9:48 You can get rid of them.
9:49 Actually, a lot of things like that are optional
9:52 or just missing in Python, and it's really, really nice
9:55 that you don't have to put them.
9:56 First it feels weird, later it's weird to require them
9:58 because if they are not really required
10:00 why do I have to type them all the time?
10:02 Okay, so this is that program.
10:05 Let's go and look at it again in C#.
10:07 I'm going to try to put these things side by side for you.
10:09 So, let's see if I can side-by-side this here
10:12 side-by-side that. Alright.
10:15 Well, you got to forget this part for a second
10:18 'cause there's a separate file in the C# version
10:20 that represents that.
10:21 This is the program.cs that has a static void main.
10:26 Look at this.
10:27 The Python one probably felts a little weird
10:29 to you if you haven't seen it before
10:31 and if you have, maybe it's fine, maybe it's no big deal
10:34 but if you haven't and you're like
10:36 wait a minute, there's no parentheses here
10:37 and this colon thing is kind of funky and so on
10:40 but there is something nice and beautiful about
10:43 that code on the left compared
10:44 to how many symbols there are on the right.
10:48 There's so much syntactical stuff in C# and whatnot
10:51 that I feel like it almost makes it hard to read
10:54 because it's just covered in a lot of curly braces
10:57 and brackets and static void this, static void that
11:01 and so on. But let's just look at them side by side.
11:04 There's a lot more stuff on the right-hand side
11:06 that just doesn't need to be there.
11:08 These are the ways that we define code structure in C#
11:13 curly braces and semicolons
11:15 and in Python, colon and whitespace.
11:18 If I come over here and I unindent, print other
11:23 this is not in the main method.
11:25 This is a separate line of code
11:27 that is kind of after main is defined
11:29 but then just executed when you import the file.
11:32 If I run this, you'll see, it first prints out other.
11:37 Oh, I took away that thing at the bottom, didn't I?
11:42 Because I wanted to compare them.
11:43 But now if I run it, you'll see other
11:45 and then what is your name, right?
11:47 These are not in each other.
11:48 It's the indentation that defines the structure.
11:51 It takes some getting used to, trust me
11:53 but once you get used to it, it's actually delightful.