Python Beginner Diary Day 013: Conditional Expressions (aka Ternary Operators)

Silent session today.



# today im gonna learn about: CONDITIONAL EXPRESSIONS (aka ternary operators)
# a conditional expression is a 1-line shortcut for the if-else statement
# print or assign one of two values based on a condition
# X if condition else Y

num = 11
a = 3
b = 2
age = 15
temperature = 13
user_role = "guest"

# print("Positive" if num > 0 else "Negative")
# result = "EVEN" if num % 2 == 0 else "ODD"

# so what this one does is find which of the values is greater, a or b, and then it outputs the greater number
# max_num = a if a > b else b

# min_num = a if a < b else b

# status = "Adult" if age >= 18 else "Child"

# weather = "HOT" if temperature > 29 else "NORMAL"

access_level = "Full Access" if user_role == "admin" else "Limited Access"

print(access_level)

Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *