Focus mode

Veri Bilimi

Ternary Conditionals

Ternary Conditionals


# cevap olarak "y" (yes->evet) veya "n"(no->hayır) vereceğiz
cevap = input("x in değeri 2 olsun mu? y/n")

if cevap == "y": # cevap == "y" testimiz oluyor
    x = 2
else:
    x = 0

print(x)
> x in değeri 2 olsun mu? y/nn
> 0
  • Bunun aynısını tek satırda şu şekilde yapabilirdik:
cevap = input("x in değeri 2 olsun mu? y/n")
x = 2 if cevap=="y" else 0
print(x)
2
  • Hatta daha düzenli bir şekilde:
cevap = input("x in değeri 2 olsun mu? y/n")


condition = cevap == "y"
x = 2 if condition else 0
x


Comments

You need to enroll in the course to be able to comment!