-1

In a Clash of Code, I've seen this interesting operator thing:

print(["false","true"][i == n])

I haven't seen this before. What's the name of this and what does it do?

  • 2
    `i == n` will return `True` or `False`. Python will consider `True` as `1` and `False` as `0` and will print the element from list accordingly – Moinuddin Quadri Dec 26 '20 at 20:00

2 Answers2

1

It's not exactly an operator but rather the second condition is being used as an index for the ["false", "true"] list.

In case i == n this will be true. Let me remind you that true in python equals to 1 int(True) // = 1 Therefore if i == n, it will be equal to one and will print the element with index one from the list which is "true".

In case i != n, it will be False, equals to 0 which will print the first element from the array which is "false".

Sami
  • 117
  • 1
  • 3
1

This a comparison Operator, it compares the value or equality of two objects, whereas the Python is operator checks whether two variables point to the same object in memory. In the vast majority of cases, this means you should use the equality operators == and != , except when you're comparing to None.

Output: True or False Usage: Is used to check whether 2 expressions give the same value.

Dev Doshi
  • 160
  • 2
  • 12