how to set a variable in one line with if and else.

As everybody knows already, when we set a variable we do “something = something else” but what if you needed (actually just wanted too) to make it all in a single line? Well on python, even we learning that every command need a single line to be run, most of the time it is false, well actually 🤓☝️

# n00b stuff (python 3.0+)
age = 10
if age >= 18:
     print("yay! you can join this website XD!!!")
else:
     print("get out kiddo")
# actually pro stuff:
age = 10
status = "yay! you can join this website XD!!!" if age >= 18 else "get out kiddo"
print(status)
# (or print directly, duh)

Leave a Comment