不解釋
解釋:非零就是真abs
()
只要有乙個為真,就是true>>> print(all([0,-1,1])) # 0 不為真
false
>>>
>>> print(all([-1,1]))
true
>>>
>>> print(all([-1,1]))
true
>>> print(any()) # 空為假
false
>>> print(any([0]))
false
>>> print(any([0,1]))
true
>>>
>>> print(ascii('我是tt'))
'\u6211\u662ftt'
>>> print(ascii( [1,2,'我是tt'] ))
[1, 2, '\u6211\u662ftt']
>>> print(type(ascii( [1,2,'我是tt'] )))
>>> print(bin(1))
0b1>>> print(bin(33))
0b100001
>>> print(bin(100))
0b1100100
# 0b 是開始,後面是轉化的二進位制數字
>>> print(bool(0)) # 0為假
false
>>> print(bool(1)) # 非0為真
true
>>> print(bool()) #空 假
false
>>> print(bool([0])) # 非空 為真
true
>>> print(bytes('abcde',encoding='utf-8'))
b'abcde'
>>> a = bytearray('abcde',encoding = 'utf-8')
>>> a[1] = 200
>>> print(a)
bytearray(b'a\xc8cde') # 修改之後的結果
>>>
>>> print(callable( )) # 列表不能呼叫
false
>>>
defa
():...
pass
...>>> print(callable(a)) # 函式可以呼叫
true
>>> chr(98)
'b'>>> chr(99)
'c'>>> chr(100)
'd'
一般都是底層用>>> ord('a')
97>>> ord('b')
98>>> ord('c')
99
事實上直接掉用exec(code) 也可以執行>>> code = "for i in range(10):print(i)"
>>> a = compile(code, '', 'exec')
>>> print(a)at 0x000001951bb64ed0, file "", line 1>
>>> exec(a)01
2345
6789
>>>
比如,檢視字典中都有什麼方法
>>> a ={}
>>> dir(a)
['__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']
>>>
>>> divmod(5,2)
(2, 1)
>>> divmod(5,3)
(1, 2) # 第乙個數是商,第二個是餘數
列印:a = [1,2,3,4]
for index,val
in enumerate(a):
print(index,val)
0 1 前面是位置數,後面是值
1 2
2 3
3 4
只能算簡單的加減乘除。>>> x = 1
>>> eval('x+1')
2
複雜的用exec()
一般會結合lambda使用
自己可以貼上執行一下。列印出來看一下區別。a = filter(lambda x:x<5,range(10))
a = map(lambda x:x*2 , range(10))
reduce()方法 在python3 以上 需要import functools
不解釋了。a = functools.reduce(lambda x,y:x+y,range(10))
print(a) # 45
自己宣告乙個,看看有沒有更改方法。
將程式裡面所有的變數,用key-value的格式展現。
不解釋
不解釋>>> hex(17)
'0x11'
# 0x開頭
>>> hex(120)
'0x78'
>>> hex(255)
'0xff'
迭代器的學習:>>> a = iter([1,2,3,4])
>>> print(a.__next__())
1>>> print(a.__next__())
2>>> print(a.__next__())
3
>>> def a ():
... local_1 = 1
... print(locals())
...
>>> a()
>>> max([1,3,5,32])
32>>> min([1,3,5,32])
1
>>> oct(11)
'0o13'
# 0o開頭
>>> oct(8)
'0o10'
>>> oct(122)
'0o172'
>>> pow(2,1) # 2^1
2>>> pow(2,2)
4>>> pow(3,2)
9
>>> pow(2,1)
2>>> pow(2,2)
4>>> pow(3,2)
9
>>> a
[1, 2, 3]
>>> b = reversed(a)
>>> b
0x7fdea0b85b00>
>>> for i in b:
... print(i)
...3
21
字典本來是無序的。>>> round(1.11222,1)
1.1>>> round(1.11222)
1>>> round(1.11222,3)
1.112
上面是按照key排序,還可以按照value排序>>> a =
>>> print(sorted(a.items()))
[(-5, 3), (1, 6), (3, 6), (4, 5)]
>>> a
# 這可以看出a 本來是無序的
>>> print(sorted(a.items(),key=lambda x:x[1]))
[(-5, 3), (4, 5), (1, 6), (3, 6)]
>>> a = [1,2,3,4]
>>> b = ['a','b','c','d']
>>> for i in zip(a,b):
... print(i)
...(1, 'a')
(2, 'b')
(3, 'c')
(4, 'd')
Python學習之路8 內建方法
abs 230 取絕對值 all 0,1,5 如果引數裡面的所有值都為真就返回真,否則返回假 any 0,1,5 如果引數裡面有乙個值為真則返回真,否則返回假 ascii 1,2,fds 浮點數 將引數變成字串 bin 8 十進位制轉二進位制 hex 255 轉十六進製制 oct 4 轉八進位制 b...
Python學習之路10 內建方法
id obj,返回物件的標識,這個標識對每個物件而言是唯一的 type object 返回物件的類別 a 10 b 0.5 c type a class int type b class float type c class list type len class builtin function ...
Python學習之路 內建函式
print all 0,15,3 all全部都是可迭代的元素時返回true print all 1,15,3 print any 1,15,3 any任意乙個是可迭代的元素時返回true print any print ascii 1,2,開掛 轉換成ascii碼 a ascii 1,2,開掛 pr...