-3
items = []
num = [x for x in input().split(',')]
for p in num:
    x = int(p, 2)
    if x%5:
        items.append(p)
print(','.join(items))

Doubt In the above question in line 5 how is x%5 a condition? On what basis do we get True or False from this condition?

Neil
  • 3
  • 4
  • 1
    `if not x%5` is short for `if x%5 == 0` – jdaz Jul 29 '20 at 07:28
  • in general, 0 means `false` and any other number is `true`, so it's like checking if `x%5==0` – maha Jul 29 '20 at 07:28
  • 1
    The result of `x%5` is an integer. It then gets cast to a boolean. When it's 0, it's false, otherwise it's true. So the condition passes whenever 5 is a divisor of x. – b9s Jul 29 '20 at 07:28
  • I'd deem `if x%5 == 0` to be more readable actually... – FObersteiner Jul 29 '20 at 07:28
  • Maybe this will help you. [python - What is Truthy and Falsy?](https://stackoverflow.com/a/39984051/) – Deadbeef Jul 29 '20 at 07:29
  • This should help: a tutorial on [Truthy and Falsy Values in Python](https://www.freecodecamp.org/news/truthy-and-falsy-values-in-python/) – DarrylG Jul 29 '20 at 07:29
  • With the `not` removed, `if x%5:` is equivalent to `if x%5 != 0:` – Tom Karzes Jul 29 '20 at 07:33
  • Relevant piece of offical documentation: [Truth Value Testing](https://docs.python.org/3/library/stdtypes.html#truth-value-testing) – Daweo Jul 29 '20 at 07:59

1 Answers1

1

Almost anything in Python can be evaluated into a boolean value. In fact, this is often 'abused' by programmers.

These are some examples of values that evaluate to False:

false_string = ''
false_int = 0
false_float = 0.0
false_list = []
false_bool = False

if false_string or false_int or false_float or false_list or false_bool:
    print('this never gets printed')

However, in most cases there's only one value that evaluates to False and all other values will evaluate to True:

true_string = 'false'
true_int = -1
true_float = 0.000001
true_list = [0]
true_bool = True

if true_string and true_int and true_float and true_list and true_bool:
    print('this gets printed')

Note that this isn't always obvious. You might think [0] should evaluate to false, but the list is not empty, so it's True, even though 0 by itself would be False.

Whenever you expect a boolean value, just read it as if the value is wrapped in a call to bool(), i.e.

if x%5:
    print('ok')
# is the same as
if bool(x%5):
    print('ok')
Grismar
  • 27,561
  • 4
  • 31
  • 54