Python基础—数学运算

首先我们先来打印hello world!

>>>print "Hello, World"
>>>print ("Hello, World")

四则运算

>>> 2+5
    7
    >>> 5-2
    3
    >>> 10/2
    5
    >>> 5*2
    10
    >>> 10/5+1
    3
    >>> 2*3-4
    2

    

上面的运算中,分别涉及到了四个运算符号:加(+)、减(-)、乘(*)、除(/)

>>> 2 + 2
4
>>> 50 - 5*6
20
>>> (50 - 5*6) / 4
5.0
>>> 8 / 5  # division always returns a floating point number
1.6

除法(/)总是返回一个float类型数。要做 floor 除法 并且得到一个整数结果(返回商的整数部分) 可以使用 // 运算符;要计算余数可以使用 %:

>>> 17 / 3  # classic division returns a float
5.666666666666667
>>>
>>> 17 // 3  # floor division discards the fractional part
5
>>> 17 % 3  # the % operator returns the remainder of the division
2
>>> 5 * 3 + 2  # result * divisor + remainder
17

猜你喜欢

转载自blog.csdn.net/knight_zhou/article/details/103943311