Python

9、内置函数

2019-04-26  本文已影响1人  代码充电宝
(1)内置函数
>>> abs(-100)
100
>>> abs(100,2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: abs() takes exactly one argument (2 given)
>>> abs('a')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: bad operand type for abs(): 'str'
>>> cmp(1, 2)
-1
>>> cmp(2, 1)
1
>>> cmp(3, 3)
0
>>>range(10) # 从 0 开始到 10
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(1, 11) # 从 1 开始到 11
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> range(0, 30, 5) # 步长为 5
[0, 5, 10, 15, 20, 25]
>>> range(0, 10, 3) # 步长为 3
[0, 3, 6, 9]
>>> range(0, -10, -1) # 负数
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
>>> range(0)
[]
>>> range(1, 0)
[]
>>> int('123')
123
>>> int(12.34)
12
>>> str(123)
'123'
>>> str(1.23)
'1.23'
>>> L = [1,2,3,4,5]
>>> sum(L)
15
print(1) # 1
print('Hello World') # Hello World
print('aaa''bbb')    # aaabbb
print(1,'hello word')# 1 hello word
print("www","runoob","com",sep=".",end="@end")  # www.runoob.com@end
name = '张三'
score = 99
print('%s =  %d' %(name,score)) # 张三 =  99
    * 不支持数字和str混合输出
# TypeError: unsupported operand type(s) for +: 'int' and 'str'
print(11+"aa")
上一篇 下一篇

猜你喜欢

热点阅读