-1

I just wanted to know what does the square bracket mean in check[] and what does this square bracket do, because all I know that {} are dictionaries and [] are lists.

n = int(input().strip())
check = {True: "Not Weird", False: "Weird"}

print(check[
    n%2==0 and (
        n in range(2,6) or 
        n > 20)
])
Georgy
  • 12,464
  • 7
  • 65
  • 73
  • You're accessing the dictionary's value by key. If this code doesn't need to support Python < 2.5, there are better options: https://stackoverflow.com/questions/394809/does-python-have-a-ternary-conditional-operator – jonrsharpe Jun 02 '20 at 13:32

1 Answers1

2

check is a dictionary and check[k] looks up the key k in this dictionary and returns the associated value.

This is a "clever" way of writing:

n = int(input().strip())

if n%2==0 and (n in range(2,6) or n > 20):
    print("Not Weird")
else:
    print("Weird")
mkrieger1
  • 19,194
  • 5
  • 54
  • 65