python 内建函数
1、基本数据类型类
1.1 int
- int([x]) -> integer
- int(x, base=10) -> intger 将一个数字或字符串转化成整数。 如果x不是一个数字,那么它必须是和base匹配的整数字符串表达式。
示例:
int(1.1)
1
int('1')
1
int('0b0101',2)
5
int('0x11',16)
17
int('0o11',8)
9
1.2 float
将一个字符串或数字转化为浮点数。
示例:
float(123)
123.0
float('1.2')
1.2
1.3 complex
- complex(real=0, imag=0)
创建一个复数通过实部和虚部
示例:
complex(10,8)
(10+8j)
1.4 str
- str(object='')
通过给定的对象创建一个新的字符串对象。
示例:
str(1)
'1'
str([1, 2, 3])
'[1, 2, 3]'
str({'name':'zlf'})
"{'name': 'zlf'}"
1.5 list
- list(iterable=())
根据传入的可迭代对象创建一个列表,如果没有参数返回空列表
示例:
list()
[]
list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list('abcde')
['a', 'b', 'c', 'd', 'e']
1.6 tuple
- tuple(iterable=())
根据传入的可迭代对象创建一个元组,如果没有参数返回空元组
示例:
tuple()
()
tuple('abcd')
('a', 'b', 'c', 'd')
tuple(range(10))
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
1.7 set
- set(iterable)
根据传入的可迭代对象创建一个集合对象。
示例:
set('abc')
{'a', 'b', 'c'}
set(range(10))
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
1.8 dict
根据传入的参数创建一个字典对象
示例:
# 形如(key,value)的键值对数据
dict([('name', 'zlf'),('age', 18)])
{'name': 'zlf', 'age': 18}
# 关键字参数
dict(name='zlf', age=18)
{'name': 'zlf', 'age': 18}
2、常用内建函数
2.1 print
- print(value,...,sep='',end='\n')
- value 会打印value的字符串形式
- sep 分隔符,当有多个value时默认用空格作为分割
- end 每次打印在最后默认添加回车换行符 打印传入对象的字符串形式
示例:
print(1, 2, 3, sep=",")
1,2,3
print(1, end='')
print(2)
print(3)
12
3
2.2 input
接收用户的输入数据,以字符串的形式返回。
可以接收字符串参数最后提示信息输出
示例:
input('>>>:')
>>>: aaa
'aaa'
2.3 type
- type(object)
返回object的类型
示例:
type(1)
int
type("fsfs")
str
2.4 dir
- dir([object])
返回传入对象的所有属性和方法名的列表
示例:
print(dir(1))
['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'as_integer_ratio', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']
2.5 help
- help(builtins.object)
返回内建对象的帮助信息
示例:
help(input)
Help on method raw_input in module ipykernel.kernelbase:
raw_input(prompt='') method of ipykernel.ipkernel.IPythonKernel instance
Forward raw_input to frontends
Raises
------
StdinNotImplementedError if active frontend doesn't support stdin.
2.6 len
- len(obj)
返回容器的元素个数
示例:
len([1,'s',3,'ds'])
4
2.7 hash
- hash(obj)
返回对象的hash值
示例:
hash('a')
-6804145991118368461
2.8 iter
- iter(iterable)
根据传入的可迭代对象返回一个迭代器
示例:
iter('dsdd')
<str_iterator at 0x1fc370190a0>
2.9 id
- id(obj)
返回传入对象的身份id(虚拟内容地址的整数形式)
示例:
id(1)
2182690531632
2.10 range
- range(stop) -> range object
- range(start, stop[,step])
返回一个range object对象,产生整数序列
示例:
range(10)
range(0, 10)
list(range(2, 10, 2))
[2, 4, 6, 8]