+ 2
What does ' if not i%2==0: ' means ?
Python code
15 Respuestas
+ 7
It means if i%2 !=0
+ 3
If i%2==1:
i%2 will only return a 0 or a 1
if not 0, then i%2==1
It could also be written as
if i%2:
PS: I am assuming you understand the % function.
Please advise if this is not clear
+ 3
Shubham Agarwal How can we know a number is an even number, by dividing it by 2 and checking if it completely divides or leaves a remainder.
10 / 2 gives 5 with 0 remainder
(10 is even)
13/2 gives 6 with 1 remainder(odd)
There is an operator (%) just like * / + - it does something. It tells u the remainder. 13%2 = 1.
So we check if something % 2 == 0 then its even, else its not.
+ 2
:
+ 1
If the remainder of I/2 ==0 i.e number is divisible by 2 is an even number.
If the remainder is 1, it's an odd number
+ 1
if number is not even
+ 1
It is the same
if i%2 != 0
for example
i = 5
if not i % 2 == 0:
print("HELLO")
#This code prints 'HELLO'
if i % 2 == 0:
print("HELLO")
#This code prints nothing
if i % 2 != 0:
print("HELLO")
#This code prints 'HELLO'
+ 1
If i divided by 2 doesn't equal 0, then do something ( if i % 2 !=0: )
0
It means if number is odd
0
% is a operator used to find the remainder after dividing two numbers and here in i%2 == 1 will be true if the remainder is 1 after dividing.
0
You must first worry about the order of precedence,
1.- Parentheses
2.- mathematical operators
2.1.- division
2.2.-multiplication
2.3.-division
2.4.- floor division
2.5.-addition and subtraction
3.- bitwise operators
4.- comparison operators
5.- logical operators
5.1.- not
5.2.- and
5.3.- or
After knowing that you apply
1) i should define it before with a number, and i% 2 will be the remainder of its result.
2) if result you compare it if it is equal to zero (i% 2 == 0) its result will be True (it can be seen equal to 1) or False (it can be seen equal to 0)
3) and the not, if it gives True it returns it False and if it is False it returns it True.
4) with the if statement, if it is true, that is, if all the previous result is True, it is carried out, otherwise, it does not allow you to make the statement that is within the if.
I hope it has helped you. 😁😁
0
if not i%2==0 can also be written as,
if (i%2!=0) .
i%2 gives the remainder obtained when i is divided by 2.
So the expression stands true if the remainder of i when divided by 2 is not 0.
Therefore, the expression stands true for all odd numbers because any odd number when divides with 2, leaves a remainder of 1
0
if not i%2==0 can also be written as,
if (i%2!=0) .
i%2 gives the remainder obtained when i is divided by 2.
So the expression stands true if the remainder of i when divided by 2 is not 0.
Therefore, the expression stands true for all odd numbers because any odd number when divides with 2, leaves a remainder of 1
- 1
it means 'if not even'
- 1
If the remainder of i%2 is equal to zero then the number is an even number