Python for the .NET developer Transcripts
Chapter: The Python Language
Lecture: C# ternary expressions
0:00 Ternary expressions are like
0:01 short one line if statements.
0:04 So here we have a program that goes around and asks the user
0:06 to enter a number between 1 and 1000.
0:09 And then it parses it, and then
0:12 here is a little if statement that's all on one line.
0:14 A ternary expression.
0:16 First has the test, num < 100
0:19 if that's true, it's going to print out the first value
0:22 or execute this command here, small.
0:26 If for some reason it's bigger or equal to 100
0:28 it's going to say huge.
0:30 And then we just use that to print out
0:31 The number is small or the number is huge
0:34 things like that.
0:35 It's kind of a silly example, but you can
0:37 definitely see what's happening here, right
0:39 the idea is just use this one-liner
0:41 probably it's better in some kind of if statement
0:44 but you know maybe not in this case.
0:47 Let's run it real quick just to see what's happening.
0:50 We can go over here and say 10. The number's small.
0:53 99, 98, small. 100 and I'd don't know, 101.
0:59 It's huge. Okay, if we hit enter, just leave.
1:03 All right looks like our program is working as expected.
1:06 So here's the ternary expression, C#.
1:08 Test? true : false
1:13 How do you know that it's true and then false
1:14 and not false then true?
1:16 You memorize it. That's how it works.
1:18 There's not a whole lot of obvious reason
1:20 it's not like if you didn't know about this
1:22 you would know what this meant, you just
1:24 have to study it and learn it.
1:25 Not too bad, it's really good if you like
1:27 little lambda expressions and stuff that you
1:29 want to have, just a little test and then
1:30 some kind of conditional and as an expression
1:32 and the value comes back, pretty nice for that.