2

[Basically the professor from the video is testing the exact same basic code but his one is printed is "Odd" while my python program is printed "Even" which should be printed "Even" why it is printed differently when using the exact same code?

The video link is here https://youtu.be/Pij6J0HsYFA?t=1942 ]1

x = 15
if (x/2)*2 == x:
 print('Even')
else: print('Odd')
asked Mar 2, 2017 at 14:11
6
  • 4
    It's probably a difference between division behavior in Python 2 vs. 3. In 2, x / 2 is assumed to be integer division, while in 3 it's float division by default. Commented Mar 2, 2017 at 14:15
  • 1
    The video uses python2 while you are using python3 Commented Mar 2, 2017 at 14:16
  • 1
    Python has % (modulo) operator which may save your time from writing if (x/2)*2 == x: Commented Mar 2, 2017 at 14:16
  • I thought of that, but even in python 3, >>> x=100000000000000 >>> (x/2)*2 == x True, even float divide+multiply still is equal (I would bet on that) Commented Mar 2, 2017 at 14:16
  • from the looks of the code it should be "odd" (it would be even if you would cast x as a float - which is why there is a python function for testing if a number is even or odd). try printing (x/2)*2 => if its 15, then there is a hidden float cast - which python version are you using? 2.7 or 3.x? Commented Mar 2, 2017 at 14:17

3 Answers 3

5

Integer division behaves differently in Python 2.7 and Python 3.X.

C:\Users\Kevin\Desktop>py -2
Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec 5 2015, 20:32:19) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 15/2
7
>>> (15/2)*2
14
>>> ^Z
C:\Users\Kevin\Desktop>py -3
Python 3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:38:48) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 15/2
7.5
>>> (15/2)*2
15.0
>>>

The professor is probably using 2.7, and you're probably using 3.X.

In any case, it's much better to use modulus to check the evenness of a number, since it doesn't depend on version-specific behavior.

>>> def is_even(x):
... return x%2 == 0
...
>>> is_even(15)
False
>>> is_even(16)
True
>>> is_even(17)
False
answered Mar 2, 2017 at 14:18
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for mentioning that this is stupid to begin with (+1).
1

Because the lecturer is using Py2 and you are using Py3.

In Python 2, the result of dividing integers is an integer, which gets truncated: 15 / 2 == 7. Since 7 * 2 != 15, the lecturer prints Odd.

In Python 3, the result of dividing integers is a float if need be to preserve the actual value: 15 / 2 == 7.5, so you print Even.

The equivalent Py3 operation to preserve the type and floor the result would be 15 // 2 == 7.

answered Mar 2, 2017 at 14:18

Comments

1

The difference is due to Python2 and Python3.

In Python 3.0, 5 / 2 will return 2.5 .The same in Python2 would generate 2.

The former being floating point division and other integer division.

In the code you mentioned,(15/2)*2 is 14 as 7 is being generated as floor value not 7.5 which is not equal to 15.Hence it will print Odd,due to python2.

On the other hand,your code will generate (15/2)*2 as 15 due to floating point precision and hence print Even. due to python3.

answered Mar 5, 2020 at 18:14

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.