0

This is the entire code. What does 'and not' mean in the code. I understand it to mean that only a number that will equal to 0 when the number modulus 2 is carried out.

That is if 10 is entered by the user, 2,4,6,8 will be sum to get 20.

the_max = int(input("Enter the upper limit:"))

the_sum = 0
extra = 0

for number in range(1,the_max):
    if number%2 and not number%3:
        the_sum = the_sum + number
    else:
        extra = extra + 1 # Line 1

print(the_sum) # Line 2
print(extra) # Line 3
Ashutosh
  • 917
  • 10
  • 19
dhrey112
  • 17
  • 5

2 Answers2

2

It means that the number is not a multiple of 2 but a multiple of 3;

the if statement has two conditions:

number % 2

since the % returns the remainder of a a number divided by 2, it will return 0 on a multiple of 2 and will result in rejection of if condition.

and not number % 3

This means that we need both condition to be good. But with this one the not operand reverses it.

So this time any number % 3 in which number is a multiple of 3 will result in 0 and will be reversed to 1;

aviya.developer
  • 3,343
  • 2
  • 15
  • 41
0

You're parsing it wrong. The correct interpretation is

if (number % 2 != 0) and (number % 3 == 0):

This code is taking shortcuts by omitting the explicit comparison to zero, since 0 evaluates to False in a boolean expression like this one, whereas any other integer value evaluates to True.

The second clause, thus, is given a not to flip its polarity. not (number % 3) is equivalent to (number % 3 == 0).

Green Cloak Guy
  • 23,793
  • 4
  • 33
  • 53
  • the Boolean expression number % 2 == 1 is the same as number % 2 . Because 1 is considered to be True for a Boolean expression. Now I understand so many things. Thank all – dhrey112 Feb 02 '20 at 15:57