VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • Python_常见内置函数

1. 方法

 注:class(类)是具有相同的属性和方法的对象的集合。

2. 例子

 (1)数据/集合类型

  • str(object=''); str(object=b'', encoding='utf-8', errors='strict')
  • int(x, base=10)
  • float(x=0)
  • complex(real=0, imag=0)
复制代码
 1 >>> str(123)
 2 '123'
 3 >>> str(['a', 'b', 'c'])
 4 "['a', 'b', 'c']"
 5 >>> str(123).join(['a', 'b', 'c'])
 6 'a123b123c'
 7 >>> int('123')
 8 123
 9 >>> float('123')
10 123.0
11 >>> (0.75).as_integer_ratio()
12 (3, 4)
13 >>> (1.0).is_integer()
14 True
15 >>> complex(1, 2)
16 (1+2j)
17 >>> complex(1, 2).conjugate()
18 (1-2j)
复制代码

注:str方法,详情见https://www.cnblogs.com/shz-blog/p/12426630.html

  • range(stop), range(start, stop[, step])
  • tuple(iterable=())
  • list([iterable])
  • dict(); dict(mapping); dict(iterable); dict(**kwargs)
复制代码
 1 >>> r = range(40, 1, -3)
 2 >>> t = tuple(r)
 3 >>> l = list(r)
 4 >>> t
 5 (40, 37, 34, 31, 28, 25, 22, 19, 16, 13, 10, 7, 4)
 6 >>> l
 7 [40, 37, 34, 31, 28, 25, 22, 19, 16, 13, 10, 7, 4]
 8 >>> r.count(1)
 9 0
10 >>> r.index(31)
11 3
12 >>> t.count(10)
13 1
14 >>> t.index(31)
15 3
16 >>> l.sort()
17 >>> l
18 [4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34, 37, 40]
19 >>> dict(a=1, b=2, c=3)
20 {'a': 1, 'b': 2, 'c': 3}
21 >>> dict(zip(list('abc'), [1, 2, 3]))
22 {'a': 1, 'b': 2, 'c': 3}
23 >>> dict([('a', 1), ('b', 2), ('c', 3)])
24 {'a': 1, 'b': 2, 'c': 3}
25 >>> dict(a=1, b=2, c=3).items()
26 dict_items([('a', 1), ('b', 2), ('c', 3)])
复制代码

注:list方法,详情见https://www.cnblogs.com/shz-blog/p/12438954.html;

    dict方法,详情见https://www.cnblogs.com/shz-blog/p/12456194.html

  • set([iterable])
  • frozenset([iterable])
复制代码
 1 >>> s1 = set('hello')
 2 >>> s1
 3 {'o', 'h', 'e', 'l'}
 4 >>> s1.add(123)
 5 >>> s1
 6 {'o', 'l', 'h', 'e', 123}
 7 >>> s1.discard('o')
 8 >>> s1
 9 {'l', 'h', 'e', 123}
10 >>> s2 = set('lemon')
11 >>> s2
12 {'o', 'l', 'e', 'm', 'n'}
13 >>> s1.update(s2)
14 >>> s1
15 {'o', 'l', 'h', 'e', 'm', 'n', 123}
复制代码
复制代码
 1 >>> a = frozenset('hello world')
 2 >>> a
 3 frozenset({'w', 'l', ' ', 'r', 'o', 'h', 'd', 'e'})
 4 >>> b = frozenset(range(5))
 5 >>> b
 6 frozenset({0, 1, 2, 3, 4})
 7 >>> c = frozenset(range(2,7))
 8 >>> c
 9 frozenset({2, 3, 4, 5, 6})
10 >>> b.union(c)    # 并集
11 frozenset({0, 1, 2, 3, 4, 5, 6})
12 >>> b.intersection(c)    # 交集
13 frozenset({2, 3, 4})
14 >>> b.difference(c)    # 差集
15 frozenset({0, 1})
16 >>> c.difference(b)    # 差集
17 frozenset({5, 6})
18 >>> b.symmetric_difference(c)    # 对称差集
19 frozenset({0, 1, 5, 6})
20 >>> b.isdisjoint(c)    # 是否没有交集
21 False
22 >>> d = frozenset(range(2,5))
23 >>> d
24 frozenset({2, 3, 4})
25 >>> d.issubset(b)    # 是否被包含
26 True
27 >>> b.issuperset(d)    # 是否包含
28 True
29 >>> e = d.copy()    #复制
30 >>> id(d) == id(e)
31 True
复制代码
  •  bytearray和bytes
复制代码
 1 >>> bytes()
 2 b''
 3 >>> bytes(3)
 4 b'\x00\x00\x00'
 5 >>> bytes('abc', 'utf-8')
 6 b'abc'
 7 >>> bytes([1, 2, 3])
 8 b'\x01\x02\x03'
 9 >>> b'abcd'.replace(b'bc', b'XY')
10 b'aXYd'
11 
12 >>> B = b'abc'
13 >>> BA = bytearray(B)
14 >>> BA
15 bytearray(b'abc')
16 >>> [i for i in B]
17 [97, 98, 99]
18 >>> [i for i in BA]
19 [97, 98, 99]
20 >>> B[0] = 65
21 Traceback (most recent call last):
22   File "<pyshell#25>", line 1, in <module>
23     B[0] = 65
24 TypeError: 'bytes' object does not support item assignment
25 >>> BA[0] = 65
26 >>> BA
27 bytearray(b'Abc')
复制代码

(2)操作

  • format(value, format_spec='')

  详情见https://www.cnblogs.com/shz-blog/p/12422194.html

  • len(obj)
  • sorted(iterable, key=None, reverse=False)
  • reversed(sequence)
  • slice(stop); slice(start, stop[, step])
复制代码
 1 >>> L = list('abcde')
 2 >>> L
 3 ['a', 'b', 'c', 'd', 'e']
 4 >>> len(L)
 5 5
 6 >>> sorted(L, reverse=True)
 7 ['e', 'd', 'c', 'b', 'a']
 8 >>> list(reversed(L))
 9 ['e', 'd', 'c', 'b', 'a']
10 >>> L[slice(1, 4, 2)]
11 ['b', 'd']
复制代码
  • enumerate(iterable, start=0)
  • zip(iter1 [,iter2 [...]])
  • map(func, *iterables)
复制代码
 1 >>> l1 = [1, 2, 3]
 2 >>> l2 = [4, 5, 6]
 3 >>> l3 = [7, 8, 9, 10]
 4 
 5 >>> list(enumerate(l3))
 6 [(0, 7), (1, 8), (2, 9), (3, 10)]
 7 
 8 >>> list(zip(l1, l2))
 9 [(1, 4), (2, 5), (3, 6)]
10 >>> list(zip(l1, l3))
11 [(1, 7), (2, 8), (3, 9)]
12 >>> list(zip(*zip(l1, l3)))    # *理解为解压
13 [(1, 2, 3), (7, 8, 9)]
14 
15 >>> list(map(lambda x: x * 3, l1))
16 [3, 6, 9]
17 >>> list(map(lambda x, y: x + y, l1, l2))
18 [5, 7, 9]
复制代码

(3)输入输出

  •  input(prompt=None)
  • open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
  • print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
1 >>> age = input('请输入年龄:')
2 请输入年龄:18
3 >>> age
4 '18'
5 >>> type(age)
6 <class 'str'>

 注:open的用法,详情见https://www.cnblogs.com/sesshoumaru/p/6047046.html;

        文件的打开、读取等各种操作,详情见https://www.cnblogs.com/hackpig/p/8215786.html

1 >>> print('hello world', 'hello Bunny', sep='\n', end='_'*10)
2 hello world
3 hello Bunny__________

注:flush的用法,参考https://blog.csdn.net/Zhongjie1986/article/details/91890109

(4)数学函数

  • abs(x)
  • divmod(x, y)
  • pow(x, y, z=None)
  • round(number, ndigits=None)
  • sum(iterable, start=0)
  • max(arg1, arg2, *args, *[, key=func]); max(iterable, *[, default=obj, key=func])
  • min(arg1, arg2, *args, *[, key=func]); min(iterable, *[, default=obj, key=func])
复制代码
 1 >>> abs(-10)
 2 10
 3 >>> divmod(11, 3)
 4 (3, 2)
 5 >>> pow(2, 3)
 6 8
 7 >>> pow(2, 3, 3)
 8 2
 9 >>> round(1.2345, 2)
10 1.23
11 >>> sum(range(5))
12 10
复制代码