roadmap.sh reposted this
I finally stumped my poll-takers! 🐍 Last week I posted a Python poll: If x = 2, what does this expression return? x == 1 or 2 Almost half of you (48%) said True. That certainly feels like a plausible answer... but Python actually returns 2. 🤪 Here's how it works. When you use the logical or operator, Python returns the first truthy operand. In this case, x == 1 evaluates to False, but 2 is indeed a truthy value. But here's the super important part: Python returns the value itself, 2, not True. This behavior turns out to be really useful for setting default values. You can do something like: user_name = input("Username: ") or "Guest" If the user skips entering their name, the empty string is falsy, so Python sets user_name to "Guest" instead. Just another one of those fun Python gotchas that turns out to be pretty darn useful! 🐍 #Python #Programming #DataScience #DataAnalysis