The ::
part is called slicing. The general syntax is start:stop[:step]
which will make a new array or string from the original one, beginning from index start
to before stop
every step
(default = 1). It's essentially a syntactic sugar for slice(start, stop, step)
which returns a slice object. If stop
is omitted then all the elements till the end of the string will be extracted
You may want to read An Informal Introduction to Python for more information about slicing
Now (w%2)|(w<=2)
calculates the start index. |
is the bitwise-OR operator. w<=2
is a bool expression, but in Python "Booleans are a subtype of integers", so True and False will be converted to 1 and 0 respectively
- If w is an odd number then
w % 2 == 1
. Regardless of the remaining part, (w%2)|(w<=2)
will always be 1 in this case and the expression becomes "YNEOS"[1::2]
which takes every second letter in the string starting from index 1. The result is the string NO
- If w is an even number then
w % 2 == 0
. According to the problem description 1 ≤ w ≤ 100
the only valid even numbers belong to the range [2, 100]
- If
w == 2
then w <= 2
returns 1 and the output is NO
like above
- If
w > 2
then both the sides of (w%2)|(w<=2)
is 0, and "YNEOS"[0::2]
takes the 1st, 3rd and 5th letters which is YES
Realizing that the expression always returns 0 or 1 we'll have an alternative more readable solution that uses direct addressing instead of slicing
w=int(input())
print(['YES', 'NO'][(w % 2) | (w <= 2)])
Btw, you have a redundant subtraction: (n-2)%2==0
is exactly the same as n % 2 == 0