Python's ternary operator

Published: 27 March 2023
on channel: mCoding
39,912
1.9k

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".

― mCoding with James Murphy (https://mcoding.io)

Source code: https://github.com/mCodingLLC/VideosS...
Conditional expressions docs: https://docs.python.org/3/reference/e...
PEP introducing conditional expressions: https://peps.python.org/pep-0308/
How Python determines if x is truthy:    • x to bool conversion in Python, C++, ...  
Default mutable arguments:    • The Best Way to Check for Optional Ar...  

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

Feeling generous but don't have a Patreon? Donate via PayPal! (No sign up needed.)
https://www.paypal.com/donate/?hosted...

Want to donate crypto? Check out the rest of my supported donations on my website!
https://mcoding.io/donate

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 😄
---------------------------------------------------
Discord:   / discord  
Github: https://github.com/mCodingLLC/
Reddit:   / mcoding  
Facebook:   / james.mcoding