What is the meaning of print()[] in Python?
python, python-2.7
Solution
>>> print("Approved","Summer School","Failed")[0]
Approved
>>> print("Approved","Summer School","Failed")[1]
Summer School
>>> print("Approved","Summer School","Failed")[2]
Failed
See the pattern? It's just simple indexing.
By the way we always know `(g<7)+(g<3)` will be `>= 0` (`False + False`) and `<= 2` (`True + True`).
Recall that `bool` is a subclass of `int`, and that `True == 1` and `False == 0` (which is why we can add `bool`s, as is done above).
Finally, it goes without saying that this confusion could have easily been avoided with an extra set of parenthesis:
print(("Approved","Summer School","Failed")[(g<7)+(g<3)])
Problem
While browsing codegolf, I found this : ``` g=input();print("Approved","Summer School","Failed")[(g<7)+(g<3)] ``` I don't understand what the [] means after print()... Any clarification ?