DEV Community

Harish raj
Harish raj

Posted on

shift operator, ternary operator / conditional operator in python

print(4 >> 1) → 2
print(4 << 1) → 8
print(4 << 2) → 16
print(4 << 3) → 32

Left Shift: <<
Right Shift: >>.

The ternary operator is a short way of writing an if-else condition in a single line.
result = value_if_true if condition else value_if_false

no1, no2 = 10, 20

result = 100 if no1 > no2 else 200

print(result)
Because 10 > 20 is False, the value 200 is assigned to result.

no1, no2, no3 = 10, 20, 30

result = no3 if no3 > no2 and no3 > no1 else no2

print(result)
Output:30
| Operator | Meaning | Example |
| is | Same object | a is b |
| is not | Different objects | a is not b |
| in | Value exists | x in list |
| not in | Value doesn't exist | x not in list |

Top comments (0)