Python's ternary operator

preview_player
Показать описание
It's not so hidden!

Yes, like nearly every modern programming language Python also has a ternary operator, although it doesn't refer to it as such. While other languages typically use "?", Python chose to use "if".

SUPPORT ME ⭐
---------------------------------------------------
Sign up on Patreon to get your donor role and early access to videos!

Feeling generous but don't have a Patreon? Donate via PayPal! (No sign up needed.)

Want to donate crypto? Check out the rest of my supported donations on my website!

Top patrons and donors: Jameson, Laura M, Dragos C, Vahnekie, Neel R, Matt R, Johan A, Casey G, Mark M, Mutual Information

BE ACTIVE IN MY COMMUNITY 😄
---------------------------------------------------
Рекомендации по теме
Комментарии
Автор

The "ter" comes from Latin, where "ter" means "three times" and "tres" just means "three", "ternus" is treefold and so on. So ternary ("ternarius") is three-ish, having the quality of three, being composed of partes tres (three parts). The root of "tri" lies in ancient Greek. So it's just a matter of which fancy dead language you find cooler to flex your smarts, when naming things.

teoconserv
Автор

It's worth noting that there is merit to calling ?: in any form a "conditional expression." 'Ternary operators' are operators which accept 3 arguments, no matter what they do. For example, I could define a mathematical ternary operator [a $ b $ c ↦a * c - b]. The only reason programmers know what "the" ternary operator does is because afaik there is only one such operator (up to operator symbol and argument order) that is ever defined in any practical language, that being [a ? b : c].

delta
Автор

What's funny is that the python community held a vote for the ternary operator syntax. They came to a consensus on what syntax to use, then Guido came in and used his own.

indigojones
Автор

It didn't take me too long to learn to check date stamps on stack overflow for this very reason.

dixonstroi
Автор

I literally had the same idea of flagging SO posts as outdated sometime ago, it would solve so many issues with outdated answers!

juhotuho
Автор

"Ternary (from Latin ternarius) or trinary is an adjective meaning 'composed of three items'." From Wikipedia

vresod
Автор

It's called ternary because it comes from Latin.
"bīnus" is twofold, "ternus" is threefold, "quaternārius" is fourfold and so on

sy-py
Автор

Stale info is turning into a huge annoyance on the internet. Now when searching stack overflow you often times have to pay attention to the dates just to make sure the answers aren't over a decade old! Sometimes that's fine, with python that's usually a huge problem.

gicknardner
Автор

How to learn Python? Many tutorials I don`t know where to get started

williamsusa
Автор

2:35 The one good place for these is golf code, where ``[a, b][cond]`` is the shortest way to evaluate conditionals in python. If you don't want b to be evaluated when not returned (maybe for recursion), you can use ``a * cond or b``. If a also can't be evaluated, you use ``cond and a or b``, which still saves 2 characters over the if-else syntax.

That said, golf code != good code, don't put this in your projects!

-
Автор

Lol sometimes the appropriate edit for a very old SO post.. is to delete it. I don't think people have the heart for that

Mutual_Information
Автор

Great video! An important discussion is when to use conditional expressions (ternary operators) in Python? There is a tradeoff between efficiency vs. readability. For short expressions, the conditional expression is certainly a great tool. But if the expressions are long, I prefer to write out the if-else on separate lines as this improves the readability. I also never use the conditional expressions in my own videos as not everyone is aware of them, to avoid alianating people.

TMQuest
Автор

Seeing your facial expressions while discussing the oddities of Python is very satisfying hehe

kalelsoffspring
Автор

“Ter” is the prefix used in *ordinal* numbers. Same as we say _third, _ not _threed._ So… one, two, three vs. primary, secondary, tertiary.

Somebody must have conceptualized the operation as ordinal (e.g., taking a third value) rather than cardinal (e.g., taking 3 values).

j
Автор

The third answer on StackOverflow mentiones that the answer is for Python versions before 2.5 so it's ok

guiorgy
Автор

Oh, I'd been using the wrong ones for a while now. Didn't know there was an official version now! 😅

fazzitron
Автор

Valid criticism of stackoverflow at the end. I wish you'd explained exactly why those answers are wrong.

TimL_
Автор

Great examples of use cases where conditional expressions can be used. E.g. for setting a default immutable value.

POINTS
Автор

Thank you for another great video! And please tune up your volume a bit, you sound way quieter than most youtubers.

ilyam.
Автор

"Deprecated" might do the job of "Severely Out Of Date" without negative impact on reputation.

bakedbeings