Python division

前端 未结 12 986
挽巷
挽巷 2020-11-21 06:23

I was trying to normalize a set of numbers from -100 to 0 to a range of 10-100 and was having problems only to notice that even with no variables at all, this does not evalu

12条回答
  •  北海茫月
    2020-11-21 07:07

    In Python 2.7, the / operator is an integer division if inputs are integers:

    >>>20/15
    1
    
    >>>20.0/15.0
    1.33333333333
    
    >>>20.0/15
    1.33333333333
    

    In Python 3.3, the / operator is a float division even if the inputs are integer.

    >>> 20/15
    1.33333333333
    
    >>>20.0/15
    1.33333333333
    

    For integer division in Python 3, we will use the // operator.

    The // operator is an integer division operator in both Python 2.7 and Python 3.3.

    In Python 2.7 and Python 3.3:

    >>>20//15
    1
    

    Now, see the comparison

    >>>a = 7.0/4.0
    >>>b = 7/4
    >>>print a == b
    

    For the above program, the output will be False in Python 2.7 and True in Python 3.3.

    In Python 2.7 a = 1.75 and b = 1.

    In Python 3.3 a = 1.75 and b = 1.75, just because / is a float division.

提交回复
热议问题