Start learning Python the day before yesterday, starting today with a daily update.
The original version is certainly very detailed, and even some unnecessary things, I have recorded down, there is time, will be modified to delete, after all, is a novice, even programming how to learn is not very clear, slowly groping to come. Thanks to the great gods for their tutorials and books.
"Slicing" is a very useful tool, which is to cut the contents of a list or a tuple, and then cut it back and forth or cut it from the middle.
EX:
>>>l=list (Range (5))
>>>l[:5]
[0,1,2,3,4]
>>>l[:5:2]
[2,4]
>>>L[:-1]
[0,1,2,3,4]
"Iteration"
>>> d={' A ': 1, ' B ': 2, ' C ': 3}
>>> for key in D:
Print (key)
B
A
C
>>> for value in D.values ():
Print (value)
2
1
3
>>> for K,v in D.items ():
Print (K,V)
B 2
A 1
C 3
>>> for ch in ' ABC ':
Print (CH)
A
B
C
>>> from collections Import iterable
>>> isinstance (' abc ', iterable)
True
>>> isinstance ([1,2,3],iterable)
True
>>> isinstance (123,iterable)
False
>>>
>>> #为判断一个对象是否可以迭代
>>>
>>> for I,value in enumerate ([' A ', ' B ', ' C ']):
Print (I,value)
0 A
2 A
2 C
>>> #对list实现类似Java那样的下标循环, using the Enumrate function
>>>
>>> for X, y
Syntaxerror:invalid syntax
>>> for X, y in[(2,4), (3,9)]:
Print (x, y)
1 1
2 4
3 9
>>> #同时引入两个变量
>>>
>>> #小结: Any iteration object can be used as a for loop, including our custom data types, and can be used for loops as long as the iteration criteria are met
>>>
>>>
"List-generated"
>>>
>>> list (range (1,11))
[1, 2, 3, 4, 5, 6, 7, 8, 9, ten]
>>> #简单的生成list [1,2,3,4,5 , 6,7,8,9,10]
>>> l=[]
>>> for x in range (1,11):
L.append (x*x)
>>> L
[1, 4, 9, +, A,, +,, Bayi, +]
>>> #生成 [1*1,2*2,..., 10*10]
>>> [x* X for x in range (1,11)]
[1, 4, 9, X, +,, +,, Bayi, +]
>>> #用列表生成式代替上面的语句
>>> [x*x for X in range (1,11) if x%2 ==0]
[4, 2, approx, +, +]
>>> #筛选出仅偶数的平方
>>> +
>>> [ M+n for M in ' ABC ' for n ' XYZ ' "
[' AX ', ' AY ', ' AZ ', ' BX ', ' by ', ' BZ ', ' CX ', ' CY ', ' CZ ']
>>> #使用两层循环生成全排列 br>>>> import OS #导入OS模块, module concept followed by
>>> [D for D in Os.listdir ('. ')]
[' DLLs ', ' Doc ', ' include ', ' Lib ', ' Libs ', ' LICENSE.txt ', ' NEWS.txt ', ' python.exe ', ' pythonw.exe ', ' README.txt ', ' Scripts ', ' tcl ', ' Tools ']
>>> #列出当前目录下所有的文件和目录名
>>> d={' x ': ' A ', ' y ': ' B ', ' z ': ' C '}
> >> for K,v in D,items ():
Print (k, ' = ', V)
Traceback (most recent):
File "<pyshell#75>", line 1, in <module>
>>> for K,v in D.items ():
Print (k, ' = ', V)
z = C
y = B
x = A
>>> #dict items () can iterate both key and value at the same time
>>> d={' x ': ' A ', ' y ': ' B ', ' z ': ' C '}
>>> [k + ' = ' + V for K,v in D.items ()]
[' Z=c ', ' y=b ', ' x=a ']
>>> l=[' Hello ', ' World ', ' IBM ', ' Apple ']
>>> [S.lower () for s in L]
[' Hello ', ' World ', ' IBM ', ' Apple ']
>>> #把list中所有的字符串变成小写
>>> l1=[' Hello ', ' World ', ' Apple ', None]
>>> L2=[x.lower () for x in L1 if Isinstance (X,STR)]
>>> Print (L2)
[' Hello ', ' world ', ' Apple ']
>>> #用列表生成式快速生成一个只含有小写字母的list, very simple and convenient
>>>
"Generator"
Using generators to calculate Fibonacci sequences
"Iterator"
>>> from collections Import Iterator
>>> Isinstance ((x for X in range), Iterator)
True
>>> isinstance ([],iterator)
False
>>> isinstance ({},iterator)
False
>>> isinstance (' abc ', Iterator)
False
>>> #可以被next () function calls and continuously returns the next value of the object as an iterator; Iterator
>>>
>>> Isinstance (ITER ([]), Iterator)
True
>>> isinstance (ITER (' abc '), Iterator)
True
>>> #吧list, Dict,str etc iterable into iterator can use the ITER () function
>>>
{Functional Programming}
"Higher order Functions"
>>> F=abs
>>> F (-10)
10
>>> F
<built-in function abs>
>>> #变量可以指向函数
>>> def add (x,y,f);
Syntaxerror:invalid character in identifier
>>> def Add (x,y,f):
return f (x) + f (Y)
>>> Add ( -5,6,abs)
11
>>> #传入函数, the simplest high-order function
"Map/reduce"
>>> list (map (str,[1,2,3,4,5,6,7,8,9))
[' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ']
>>> #map的用法
>>> from functools Import reduce
>>> def add (x, y):
Return X+y
>>> reduce (add,[1,3,5,7,9])
25
>>> #用reduce实现对一个序列求和
>>> from functools Import reduce
>>> def fn (x, y):
Return X*10+y
>>> reduce (fn,[1,3,5,7,9])
13579
>>> #把序列 [1,3,5,7,9] into integers 13579
>>> from functools Import reduce
>>> def fn (x, y):
Return X*10+y
>>> def char2num (s):
return {' 0 ': 0, ' 1 ': 1, ' 2 ': 2, ' 3 ': 3, ' 4 ': 4, ' 5 ': 5, ' 6 ': 6, ' 7 ': 7, ' 8 ': 9}[s]
>>> Reduce (Fn,map (char2num, ' 13579 '))
13579
>>> #将str转换为int的函数
>>>!!! The exercises are difficult, not done.
"Filter"
"Sorted"
Learning the path to Python (1)