天天看点

python基础--函数1(abs,max,min,int,str,float,bool,bytes)

1.内容简介:

本节介绍常用的函数,包括abs,max,min,int,str,float,bool,bytes等。

2. 举例:

测试代码:

#abs,max,min,int,str,float,bool,bytes
>>> abs(-10)
10
>>> max(1,6,3)
6
>>> min(1,6,3)
1
>>> int(r)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'r' is not defined
>>> int(10)
10
>>> str(q)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'q' is not defined
>>> str(99)
'99'
>>> float(10.1)
10.1
>>> bool(5)
True
>>> bool(-1)
True
>>> bool(5>5)
False
>>> bytes(qwe)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'qwe' is not defined
>>> bytes('qw')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: string argument without an encoding
>>> bytes('qq', encoding='utf-8')
b'qq'
>>>
           

说明:

1. 如果执行函数错误,请根据错误信息进行排查 。

2. 函数说明:

常用函数总结

函数名 功能 说明
abs 获取绝对值
max 获取最大值
min 获取最小值
int  获取int型数据
str 获取str型数据
float 获取float型数据
bool 获取bool值,返回True or False
bytes 获取字节串

(1)bytes 是 Python 3.x 新增的类型;

(2)以b为前缀,例如b'abc'

当程序员,基础知识要扎实。