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


Talk Python's Mastodon Michael Kennedy's Mastodon