Python for .NET Developers Transcripts
Chapter: The Python Language
Lecture: Code structure demo

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


Talk Python's Mastodon Michael Kennedy's Mastodon