Python for the .NET developer Transcripts
Chapter: The Python Language
Lecture: Python ternary expressions
0:00 Python also has a ternary expression
0:02 and we're going to look at it now.
0:03 So, let's start with our little ternary app here
0:06 we're going to use my if main live template
0:08 and it had a while true section in here.
0:12 So in this it had text like so and it said
0:16 and then you'd have to test it if it was empty
0:18 so we can do that if not text
0:23 print, like this later, and a break.
0:30 Alright, that's cool.
0:31 And then the next thing if it was some kind of
0:33 content, we assume that we can parse it.
0:35 We didn't have really error handling here so it's fine.
0:38 We're going to parse it over
0:39 I'll say the number class is either small or huge.
0:43 In C# is was test? true expression : false expression.
0:50 Python takes a little bit more to get used to
0:52 there's some advantages and disadvantages
0:54 but it tries to be more English-like.
0:56 It starts out with the true case.
0:58 So we'd say: small, then if, then test
1:01 so n < 100, else false case, huge.
1:05 Like this. And I guess that would be num.
1:09 So this right here is the ternary expression in Python.
1:14 True thing, if test else that.
1:16 So it tries to be more English-like.
1:18 Small if the num < 100 else huge.
1:20 We're just going to print out the number is num_class
1:25 like so. Alright, let's just run this and see what we get.
1:29 Alright, inner number 4, small, 100, it's huge!
1:33 Boundary test 99, small, something huge like that.
1:37 Perfect. Empty, it exits.
1:40 Pretty cool, huh?
1:41 So that's writing this test here in Python.
1:45 Let's do a quick side-by-side.
1:48 Like this. Like that, so
1:52 let's go up to the main there you go.
1:54 And remember you got to ignore that 'cause that's
1:56 just in another file in the C# one.
1:59 So here's the comparison of these two.
2:01 While true, go through, get the stuff
2:04 now mile it, parse it
2:05 here's the test, the part we really care about right here
2:08 versus that part right there. Pretty cool.