1.2 内置函数
print函数
输出函数,将内容格式化的去显示在控制台窗口。
>>> a=1
>>> b=2
>>> c=3
>>> print(abc)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'abc' is not defined
>>> print(a,b,c)
1 2 3
print函数原型:print(self, *args ,sep=' ',end='\n',file=None)
-
self:先不管
-
*args:arguments,多参数
>>> print(1) 1 >>> print(1,2) 1 2 >>> print(1,2,3) 1 2 3
-
sep:多个数据之间的间隔默认是一个空格
' '
>>> print(1,2,3,sep = '!') 1!2!3
-
end:结尾默认
\n
>>> print(1,2,3,sep='#',end='哈哈') 1#2#3哈哈>>> print(1,2,3,sep='#',end='哈哈\n') 1#2#3哈哈 >>>
格式化输出
>>> name = "旺财"
>>> age = 18
>>> print(name,age)
旺财 18
>>> print("你叫旺财,你是18岁")
你叫旺财,你是18岁
>>> print("abc"+"ABC")
abcABC
>>> print("你叫"+name+",你是"+age+"岁")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str
>>> print("你叫%s,你是%d岁"%(name,age))
你叫旺财,你是18岁
-
%s:表示字符串数据
-
%d:表示整数数据
-
%f:表示浮点型数据
input函数
获取用户的输入数据,输入的数据是一个字符串
>>> a = input("请输入你的年龄:")
请输入你的年龄:12
>>> a + 1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str
如何获取多个数据的输入?
#错误输出
>>> a,b,c = input("请输入三个数字:")
请输入三个数字:12,13,14
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: too many values to unpack (expected 3)
>>> input("请输入三个数字:")
请输入三个数字:1,2,3
'1,2,3'
#正确输出
>>> a,b,c = eval(input("请输入三个数字:"))
请输入三个数字:1,2,3
>>> a+b+c
6
eval函数
解析字符串中的数字,整数,小数,其他进制的整数。
>>> eval("3")
3
>>> eval("3.14")
3.14
>>> eval("WYZ")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'WYZ' is not defined
>>> eval("0xABC")
2748
>>> eval("0x1001")
4097
>>> eval("0b1001")
9
int 函数
将整数字符串或浮点数转为整数。
>>> int("123")
123
>>> int("3.14")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '3.14'
>>> int(3.14)
3
也可以去指定进制:
>>> int("1001")
1001
>>> int("1001",2)
9
>>> int("1001",100)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: int() base must be >= 2 and <= 36, or 0
>>> int("1001",3)
28
>>> int("1919",6)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 6: '1919'
float 函数
将小数字符串或整数转换为浮点数。
>>> float("3.14")
3.14
>>> float(3)
3.0
>>> float("3.1a4")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: '3.1a4'
str函数
将对象转换成字符串类型。
>>> a = 10
>>> str(a)
'10'
>>> name = "旺财"
>>> age = 10
>>> print(name+age)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str
>>> print(name+str(age))
旺财10
abs函数
求绝对值。
>>> abs(-10)
10
>>> abs(-10.2)
10.2
>>>
sum函数
求序列和。
>>> sum([1,2,3])
6
>>> sum("abc")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>> sum(["abc","ABC","lala"])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'