Python for the .NET Developer Transcripts
Chapter: The Python Language
Lecture: Python function basics

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Here we are on the Python side over in PyCharm. And we're going to create that high-low game again. We're going to start by defining our main method
0:08 and the __main__ convention. So that's my little live template we created. And the first thing that we did over there is we said
0:17 I'm going to show the header. That's the big welcome to the game. So we'll say show_header, like that.
0:21 And this one calling a function that has no parameters and we don't really care about the return value.
0:27 And PyCharm you see will actually create this for us. I really want to do it just once but I don't really like how it does this.
0:33 So notice it went and created the function just fine. If you want it to be empty, you can use this word pass and that's also fine.
0:39 But what I don't like is that it put it above main. It put it above main because that's the safest option. And it's not convinced that we're doing this
0:47 in the right time and place. But I like the main to be the top and then all the stuff after it so I can see the overall view
0:55 of what's happening in this program and then the details. No, I'm going to put this down here. Alright, that's why I don't use that little
1:02 auto-generate bit thing so much. And here we just have a little print statement. I'm just going to paste it 'cause there's no real value
1:08 to watching me type this. So it just changed the word C# to Python here. And that's what we got. So we'd run this one.
1:16 Notice it's got a little header and that's it so far. Okay, so what's next? We can put that away. The next thing we did is we actually generated
1:25 the guess, the random number. Okay, so what we did in .NET is we said Random rand = new Random() like this. Alright, that's what we had.
1:37 And we had to have at the top using System for that to work, right? Python has something very similar.
1:44 If we want to use the random library from the standard library that comes with Python, we have to add an equivalent
1:51 statement like this to say we'd like to reference or refer to this part of the system. So the way we do that in Python is we say import.
1:59 And there's all sorts of stuff we can put up here. One of them is the random library. So we say import random. And down here we would say the guess.
2:09 Alright, this is sort of two steps here. We also had the guess is rand.Next or something like this. I forgot the function. Alright, so we had that.
2:19 So then here we're going to say random.randint. Now, this takes two values. It's not super-helpful about what it is.
2:29 But it's two numbers, I believe inclusive. We can always say view quick documentation. Yeah, so inclusive A to B.
2:39 So we want to go one to 100, like this. So that's the guess. And just for a moment, let me just print the guess. Now, obviously you don't show it
2:47 or it's a pretty boring game. But just so you can see that we're making progress here. Alright, so we guessed 83. Good, we're on a good path.
2:55 I'm not going to do that again. Then what we did is we went around and around asking What do you want to do?
3:01 So we had a while loop and we said while true. We're going to say the guess is equal to 1 and get the guess from the user.
3:11 Alright, so we have a function that's going to return a value back here. We also had the count. That equals 0.
3:20 And each time through, we would increment this counter, right? So we'll say, we wanted to say if they didn't give us
3:26 a guess, just ask them for a guess again. So if not guess. Before we checked whether it was null and things like that.
3:32 We can just say use the truthiness of this here. It'll return none or an integer. And we'll say continue. Exactly like C#.
3:40 Now, we want to make sure, okay, we're recording this check here so we're going to say count. In C#, we had this.
3:47 Python, for some reason, doesn't have a ++. I really kind of wish it did. So we do +=1. Basically the same thing but, you know, ++. I'm kind of a fan.
3:57 And then we need to evaluate this guess. So we'll say if evaluate_guess. So when I give the guess that the person gave
4:05 and we'll also have the, call it the guess. Let's rename this. Use a little refactor rename. Say we're to current rename all the, I'll put this
4:16 to the number. There we go. That's better, right? We're going to evaluate this with a function that doesn't exist yet.
4:24 And if it's true then we're going to break out. At the very end, we're going to do a print statement that says something like this.
4:33 You got the number in some number of attempts. Thanks for playing. Alright, so the last thing to do is write our get the guess function here.
4:43 So let's go and say def get_guess. And remember what we do in get_guess. Well, a couple of things here. Sure, it's easy to just ask for the number
4:53 from the user and then just convert it to an integer. But we want to do some validation here, right? So we'll text. We'll put text as input.
5:02 What number am I thinking of? And then we'll put, let's put the value is going to be convert this to an integer from text, right?
5:09 This is like int.TryParse. First thing we want to do is make sure they're guessing between one and 100 so we'll say if val is less than one
5:17 or in C# and a lot of the C languages you say || for or, right? In Python you just say or and and, right? And would be the other one there.
5:28 And it would say that or 100 is less than val. Alright, say nope, this number's not in the right place. It won't return Python's equivalent of null.
5:41 We don't have null here. We have none but the meaning is the same thing. And if that works, we were able to parse it into an integer
5:49 and it was like this so we could return the vowel. But just like .NETs and .Parse, this throws an exception if you put in something like that, right?
6:00 That's going to be exception. So we got to do one more layer here. We're going to talk more about error handling in detail
6:06 in a minute. But let's just deal with this like so. So if you try to do this and it fails instead of catch you have except
6:14 I'm going to return None as well. So those are the two cases. Maybe we'll put a little note in here as well like print.
6:21 Whatever that was, it's not an integer, okay? And just so it doesn't freak out, right. It's always defined somehow.
6:28 Okay, so this should get our guess for us here. And the last thing to do is just to evaluate it.
6:35 Let's copy this over so I can not have to type too much. And I'll just put it right here. Again we say def. Now we take the two parameters, input here.
6:46 And just for the sake of time, let me just drop in some code real quick here. And I'll just make that a number.
6:52 So we say if the guess is equal to the number, that's it. I was thinking of the number. If it's too low, too low, too high, and so on.
6:59 And then we're going to return True or False. Is this equal to the number? And by the way I have this type of font
7:05 installed on my system that will convert things like double equals to have, basically look like one long equals.
7:12 And if you say not, like if I say not equals and I put that together, it slashes through it or less than, oops, take that away, less than or equal
7:20 to and so on. So you might see some funky characters. Anyway, it's just that font there. So it just means two equals, right? Nothing funny about that.
7:31 Okay, so this is our evaluate guess. I think our program should run. We've imported our random library. We've called a function on it, got the number
7:40 doing our guesses. Yeah, let's try it. What number am I thinking of? I'm thinking of 50. That's too high. Oh, 25, 15. Jeez it's low.
7:54 10, 5, yes. It was 5. You got 5 in 5 attempts. Thanks for playing, bye. That's a pretty good little program, huh? Not too bad.
8:04 And let me take, let's just account for the fact there are two comment lines there and I think that that's it.
8:13 Right, so what do we have in C# for this? We had 86 lines. Kind of the empty line. In Python, 63. That's 23 fewer lines.
8:28 Format this? No, everything's formatted correctly with the right spacing line separation, all that. Alright, so here is that game.
8:35 Actually, remember, take this two out as well. Super, super-simple, really nice. The way we do arguments is we just pass them in like
8:43 it's almost exactly like C#. Same here, you just don't define the types. There's actually a lot of flexibility when we get to that in the next section
8:52 the very next section. But if you don't care about specifying the types or default values or stuff like that, you just say the parameters here.
8:58 The other thing is you never specify the return type, right? In C# you always have like string or void or list of task of I don't know, int, right?
9:10 Something like that. We don't put any of that. And the reason is one, we don't say the typing necessarily. It's sort of optional these days in Python.
9:21 But the other is every function has a return type. What does this return? Well, this returns a boolean because that's what we're returning.
9:30 But what's less obvious, this one also returns something. This is, there's an implicit return None. If you don't specify a return type for a function
9:42 its return type or the, actually the return value, is None. Every function has a return type. There is no concept of a void function.
9:53 It just happens to be they return None or null when they don't specify anything. All right, so these are the basics
10:00 of passing arguments into, calling, and getting the return values of functions. It's actually super, super-similar to C#
10:07 with the exception of this implicit return type.

Talk Python's Mastodon Michael Kennedy's Mastodon