Python for .NET Developers Transcripts
Chapter: The Python Language
Lecture: Python ternary expressions

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Python also has a ternary expression and we're going to look at it now. So, let's start with our little ternary app here
0:07 we're going to use my if main live template and it had a while true section in here. So in this it had text like so and it said
0:17 and then you'd have to test it if it was empty so we can do that if not text print, like this later, and a break. Alright, that's cool.
0:32 And then the next thing if it was some kind of content, we assume that we can parse it. We didn't have really error handling here so it's fine.
0:39 We're going to parse it over I'll say the number class is either small or huge. In C# is was test? true expression : false expression.
0:51 Python takes a little bit more to get used to there's some advantages and disadvantages but it tries to be more English-like.
0:57 It starts out with the true case. So we'd say: small, then if, then test so n < 100, else false case, huge. Like this. And I guess that would be num.
1:10 So this right here is the ternary expression in Python. True thing, if test else that. So it tries to be more English-like.
1:19 Small if the num < 100 else huge. We're just going to print out the number is num_class like so. Alright, let's just run this and see what we get.
1:30 Alright, inner number 4, small, 100, it's huge! Boundary test 99, small, something huge like that. Perfect. Empty, it exits. Pretty cool, huh?
1:42 So that's writing this test here in Python. Let's do a quick side-by-side. Like this. Like that, so let's go up to the main there you go.
1:55 And remember you got to ignore that 'cause that's just in another file in the C# one. So here's the comparison of these two.
2:02 While true, go through, get the stuff now mile it, parse it here's the test, the part we really care about right here
2:09 versus that part right there. Pretty cool.


Talk Python's Mastodon Michael Kennedy's Mastodon