万物皆对象
In [ ]:
anInt = 5
aFloat = 5.0
aLong = 555555555555555555555555555555555L
aComplex = 1.23+4.56J
print "Int:", anInt
print "Float:", aFloat
print "Long:", aLong
print "Complex", aComplex
In [ ]:
# 更新
anInt += 1
print anInt
anInt = anInt + 1
anInt
In [ ]:
5 == 5
In [ ]:
4.9 >= 5
In [ ]:
1 < 3 < 5 # 等同于 (1 < 3) and (3 < 5)
In [ ]:
1 < 5.0 == 5 # 等同于 (1 < 5.0) and (5.0 == 5)
In [ ]:
# 支持算术操作符
print +5, -5
print 5 + 1, 5 - 1, 2 * 4, 9 % 3, 2**2
In [ ]:
# 传统除法
print 5 / 2
print 5.0 / 2 # 等同于 5 / 2.0
In [ ]:
# 正确的除法
from __future__ import division
print 5 / 2
In [ ]:
2 ** 4
In [ ]:
-2 ** 4
In [ ]:
(-2) ** 4
In [ ]:
1 + 2 * 3
In [ ]:
(1 + 2) * 3
In [ ]:
print anInt
float(anInt)
In [ ]:
print aFloat
int(aFloat)
In [ ]:
str(anInt)
In [ ]:
# 绝对值
myInt = -5
print abs(myInt)
myInt = -5.0
print abs(myInt)
myInt = 5
print abs(myInt)
In [ ]:
x, y = 9, 3
print divmod(x, y)
print x // y, x % y
In [ ]:
print pow(2, 4)
2 ** 4
In [ ]:
myFloat = 3.14159265359
print myFloat
round(myFloat, 3)
In [ ]:
import math
In [ ]:
math.pow(2, 3)
In [ ]:
math.sqrt(9)
In [ ]:
# --------------------------------------------------------------------------------
# Copyright (c) 2013 Mack Stone. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# --------------------------------------------------------------------------------