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

复制代码
 1 >>> max(1, 2, 3)
 2 3
 3 >>> max(1, 2, '3')
 4 Traceback (most recent call last):
 5   File "<pyshell#1>", line 1, in <module>
 6     max(1, 2, '3')
 7 TypeError: '>' not supported between instances of 'str' and 'int'
 8 >>> max(1, 2, '3', key=int)
 9 '3'
10 >>> max(-3, 1, 2, key=abs)
11 -3
12 >>> max('123')
13 '3'
14 >>> max([1, 8], [2, 6], [3, 4])
15 [3, 4]
16 >>> couple = ({'name': 'Bunny', 'age': 18, 'salary': 888}, {'name': 'Twan', 'age': 20, 'salary': 666})
17 >>> max(couple, key=lambda x: x['age'])
18 {'name': 'Twan', 'age': 20, 'salary': 666}
19 >>> max((), default=0)
20 0
复制代码

 (5)编码

复制代码
 1 >>> bin(10)
 2 '0b1010'
 3 >>> oct(10)
 4 '0o12'
 5 >>> hex(10)
 6 '0xa'
 7 >>> chr(65)
 8 'A'
 9 >>> ord('A')
10 65
11 >>> ascii('hello world')
12 "'hello world'"
13 >>> repr('hello world')
14 "'hello world'"
15 >>> ascii('你好,世界')
16 "'\\u4f60\\u597d\\uff0c\\u4e16\\u754c'"
17 >>> repr('你好,世界')
18 "'你好,世界'"
复制代码

(6)判断

复制代码
 1 >>> all(['a', 'b', 'c'])
 2 True
 3 >>> all(['a', 'b', '', 'c'])
 4 False
 5 >>> all([])
 6 True
 7 >>> any([0, '', False])
 8 False
 9 >>> any([])
10 False
11 >>> callable(str)
12 True
13 >>> callable('hello world')
14 False
复制代码

(7)迭代器

复制代码
 1 >>> for i in iter(list('abc')):
 2       print(i)
 3     
 4 a
 5 b
 6 c
 7 
 8 >>> from random import randint
 9 >>> def guess():
10     return randint(0,10)
11 >>> num = 1
12 >>> for i in iter(guess, 5):
13     print('第%s次猜测,猜测数字为:%s' % (num, i))
14     num += 1
15     
16 第1次猜测,猜测数字为:3
17 第2次猜测,猜测数字为:1
复制代码

注:猜数字的例子来自http://www.imooc.com/article/287997

复制代码
 1 >>> i = iter(list('abc'))
 2 >>> next(i)
 3 'a'
 4 >>> next(i)
 5 'b'
 6 >>> next(i)
 7 'c'
 8 >>> next(i)
 9 Traceback (most recent call last):
10   File "<pyshell#27>", line 1, in <module>
11     next(i)
12 StopIteration
13 >>> next(i, 0)
14 0
复制代码
复制代码
1 >>> def is_odd(n):
2     return n % 2 == 1
3 
4 >>> oldlist = [i for i in range(1,11)]
5 >>> oldlist
6 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
7 >>> newlist = list(filter(is_odd, oldlist))
8 >>> newlist
9 [1, 3, 5, 7, 9]
复制代码

 (8)属性操作

复制代码
 1 >>> class Person:
 2       name = 'Bunny'
 3       age = 18
 4       sex = ''
 5 
 6 >>> Person.name
 7 'Bunny'
 8 >>> Person.country
 9 Traceback (most recent call last):
10   File "<pyshell#6>", line 1, in <module>
11     Person.country
12 AttributeError: type object 'Person' has no attribute 'country'
13 >>> getattr(Person, 'age', 0)
14 18
15 >>> getattr(Person, 'country', 0)
16 0
17 >>> setattr(Person, 'country', 'China')
18 >>> getattr(Person, 'country', 0)
19 'China'
20 >>> delattr(Person, 'sex')
21 >>> hasattr(Person, 'sex')
22 False
复制代码

(9)辅助函数

复制代码
1 >>> dir()
2 ['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__']
3 >>> dir(dict)
4 ['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']
复制代码
复制代码
1 >>> help(hash)
2 Help on built-in function hash in module builtins:
3 
4 hash(obj, /)
5     Return the hash value for the given object.
6     
7     Two objects that compare equal must also have the same hash value, but the
8     reverse is not necessarily true.
复制代码
1 >>> hash('hello world')
2 -8331809543453374991
3 >>> hash(tuple('abcde'))
4 5996617995451668254

  哈希的相关知识点:https://www.cnblogs.com/abdm-989/p/11329122.html

1 >>> a = 'hello world'
2 >>> b = a
3 >>> id(a)
4 1873301041520
5 >>> id(b)
6 1873301041520
复制代码
1 >>> a = memoryview(bytearray('abcde', 'utf-8'))
2 >>> a[1]
3 98
4 >>> a[1:3]
5 <memory at 0x0000017F63B83408>
6 >>> a[1:3].tobytes()
7 b'bc'
8 >>> a[1:3].tolist()
9 [98, 99]
复制代码

优点:memoryview减少内存拷贝,优化效率(详情可参考https://www.hustyx.com/python/222/)

复制代码
1 >>> a = 2
2 >>> type(a)
3 <class 'int'>
4 >>> isinstance(a, int)
5 True
6 >>> isinstance(a, str)
7 False
8 >>> isinstance(a, (str, int, list))    # 是元组中的一个就返回True
9 True
复制代码
复制代码
 1 >>> class A:
 2    pass
 3 
 4 >>> class B(A):
 5    pass
 6 
 7 >>> issubclass(B, A)
 8 True
 9 >>> isinstance(B(), A)
10 True
11 >>> type(B()) == A
12 False
复制代码

(10)面向对象

复制代码

      



  

相关教程
关于我们--广告服务--免责声明--本站帮助-友情链接--版权声明--联系我们       黑ICP备07002182号