Python for the .NET developer Transcripts
Chapter: The Python Language
Lecture: Python switch statements

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now let's see switch in Python.
0:02 This should be pretty short because you know what
0:04 Python doesn't have a switch statement.
0:05 Literally the language does not have a switch statement.
0:08 Like, it does not have a numerical for loop
0:10 like for i = 0, i < limit.
0:13 There is no switch statement.
0:14 There's only if, else if, else if, else if.
0:18 So why do we even have this part of this chapter?
0:21 Because Python is super flexible
0:23 and we can build our own.
0:24 In fact, what you just saw
0:26 this using statement
0:27 this with context manager
0:30 is all you need to actually build your own switch statement.
0:33 And let's look over here. I did.
0:36 I created this thing called Python switch over on my Github.
0:39 It's public. You can play with it.
0:41 Do what you want. It actually shows you how to do
0:43 that context manager stuff I talked about.
0:45 So let's create a new program here.
0:47 What we had before was a while true
0:50 we've got some text equals input
0:53 enter a number...
0:59 Like this. And it said if not text
1:02 print 'later', break.
1:05 Like that. And then we said
1:07 num was equal to the integer parse of the text.
1:10 I think that's what we did in C#.
1:12 And then we wanted to write switch.
1:14 Well you can see this is an error.
1:16 There is no switch statement.
1:18 Check this out.
1:19 So we can start using this library
1:21 this module over here
1:22 by saying, from switchlang,
1:27 we need to change this to have that be a source's root.
1:32 So here we say switchlang import switch.
1:37 This is the switch statement that I made for the world
1:40 and down here, the way we're going to do it
1:43 is instead of saying
1:44 switch value, case, case, case, case, case
1:48 we're going to use a context manager to say
1:50 with switch(num) as s:
1:55 Looks maybe a little bit weird
1:56 but I think let's roll with it for a minute.
1:58 I think you'll like it.
1:59 So we can go, s.case
2:01 instead of saying case:
2:03 or the value:.
2:05 We can just say the 'key'.
2:07 The key is going to be like 1
2:08 so we'll link this really closely.
2:10 Case 1 is going to be case(1)
2:14 and then you put a function colon here.
2:15 Like a lambda.
2:17 This one's going to do nothing
2:19 take no values.
2:21 And it's going to print out exactly the same value
2:23 we had in C#. One is fun.
2:27 Okay, that's cool. Let's do Case 2.
2:30 Let's put it and see what it said right here.
2:32 So 2 times 2 equals 4
2:36 which is fun. That was that one.
2:38 This one said 3 and free
2:41 or they've got a period is 4.
2:46 Four more and of course the case was 4.
2:48 Now by the way, if I leave it like this
2:50 and I run it right now I put 7
2:55 It doesn't matter what I put.
2:56 It says duplicate case.
2:57 It checks that you
2:59 just like the compiler would check
3:00 that you can't have Case 3, Case 3
3:03 this thing checks, like that.
3:05 Then what did we have before?
3:06 We had a default.
3:07 What happens when there's a default?
3:09 There's a lambda, and the lambda prints.
3:12 Say what? And the value over here was num.
3:19 This, by the way, is using closure to capture this value.
3:23 It seemed not useful, and heck
3:25 here we are in this trivial little case using it already.
3:29 Let's run it and see what we get.
3:30 Number 1, one is fun. 2 and 2 is 4.
3:34 3 and free, four is more.
3:36 If I put 72 or 74, say what? 74.
3:40 If I put enter, later.
3:42 Now that was pretty incredibly easy to write.
3:45 But this switch statement is actually
3:47 more powerful than that.
3:48 It accepts ranges and other stuff like that.
3:52 So there's another thing in here called a closed range
3:55 like that.
3:56 And I could do something like, here.
3:59 I could say
4:00 I would like to have a case that has a closed range
4:02 from 10 to 20.
4:04 This matches all 10, 11, 12, 13, 14 and so on
4:07 and other types of comparisons could be put here
4:10 and instead of having this necessarily do an action
4:13 it could actually return a value
4:15 as part of this switch as well.
4:17 So we could have it return the function return value.
4:20 We'll just have whatever the number is, squared.
4:25 That's going to be the return value.
4:26 Of course, this could be more complicated
4:28 based on inputs and all that.
4:31 And at the end, outside the context manager
4:33 we could print done and got.
4:35 Let's see. It might not always do it
4:37 but sometimes you might get a result.
4:41 Let's run it one more time.
4:43 One, got none, that's fine.
4:45 Two or three, got that.
4:47 But if I put 12, I should get 144.
4:50 Done, right?
4:51 Also we get 11, it hits that case.
4:55 If I get 15, it hits that case.
4:57 If I get 21, it's out of that case, right?
5:01 Don't know what to do with that.
5:02 Isn't that cool?
5:03 Here's a really, really clean way
5:05 to do actually more than C#'s switch statements can do.
5:09 Okay, so pretty sweet.
5:10 Actually maybe they support ranges now as well
5:12 but there's a bunch of interesting stuff
5:14 that we can add to this switch statement.
5:16 'Cause it's not part of the language.
5:17 We control it.
5:18 Alright, side by side, let's see what we've got.
5:20 Does that even fit on the screen?
5:21 Not really, sort of.
5:24 But in our version over here
5:25 of course it fits on the screen.
5:27 Look at that. Look, it's a little bit weird
5:30 but if you look at this switch statement
5:32 it doesn't have the cases and stuff
5:35 or the return value
5:36 it's not bad, huh?
5:37 Remember, Python doesn't even have a switch statement.
5:40 But I added this to the language
5:41 because hey, I thought it needed it.
5:43 I use this for lots of my programs for super gnarly code
5:46 and it really is valuable, right?
5:48 It catches errors because it makes sure
5:50 you can't have two branches
5:52 that might do the same thing
5:53 which would have been okay in say, an if statement
5:55 or something like that.
5:56 So really, really nice.
5:57 I love the way this works.
5:59 I make use of it when it makes sense
6:00 for like a switch statement in the language.
6:03 But we just had our own definition we created here
6:07 and used a context manager to make it work in Python.