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