AI-Learning-Note

AI学习笔记

python3 学习笔记

一. Python的沟通方式

1. 很随意

命令行运行python,进入后就可以输命令

命令行交互

还可以当计算器用

1571579739829

2. 也很严谨

在python源文件的开头,有些需要加编码声明,防止乱码,如下:

# -*- coding: utf-8 -*-

python源文件需要严格的空格对齐,通过对 齐来区分不同的模块,也让程序更具有可读性,如下:

# -*- coding:utf-8 -*-
#类定义,这时一个人
class person:
    #这个人的基本属性,姓名,年龄
    name = ''
    age = 0
    #私有属性,体重
    __weight=0
    #初始化,给这些属性赋值的方法,也叫定义构造方法
    def __init__(self,n,a,w):
        self.name=n
        self.age=a
        self.__weight=w
    def selfintro(self):
        print('My name is %s.I am %d years old'%(self.name,self.age))
#实例化类,就是把抽象的人具体成有名字、年龄、体重等具体信息的人
p=person('John',10,80)
p.selfintro()
My name is John.I am 10 years old

二. 跟Python随便聊两句

1. 关于Python3语法的一些注意

# -*- coding: cp-1252 -*-

2. 用Jupyter lab编程

a=10
print(a)
10
import time
time.sleep(10)
print('hello')
hello
from __future__ import print_function
print('hi, stderr', file=sys.stderr)
hi, stderr
import time
for i in range(10):
    print(i)
    time.sleep(0.5)
0
1
2
3
4
5
6
7
8
9
for i in range(500):
    print(i)
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499


点击左侧蓝色竖线可以折叠
for i in range(500):
    print(2**i-1)
0
1
3
7
15
31
63
127
255
511
1023
2047
4095
8191
16383
32767
65535
131071
262143
524287
1048575
2097151
4194303
8388607
16777215
33554431
67108863
134217727
268435455
536870911
1073741823
2147483647
4294967295
8589934591
17179869183
34359738367
68719476735
137438953471
274877906943
549755813887
1099511627775
2199023255551
4398046511103
8796093022207
17592186044415
35184372088831
70368744177663
140737488355327
281474976710655
562949953421311
1125899906842623
2251799813685247
4503599627370495
9007199254740991
18014398509481983
36028797018963967
72057594037927935
144115188075855871
288230376151711743
576460752303423487
1152921504606846975
2305843009213693951
4611686018427387903
9223372036854775807
18446744073709551615
36893488147419103231
73786976294838206463
147573952589676412927
295147905179352825855
590295810358705651711
1180591620717411303423
2361183241434822606847
4722366482869645213695
9444732965739290427391
18889465931478580854783
37778931862957161709567
75557863725914323419135
151115727451828646838271
302231454903657293676543
604462909807314587353087
1208925819614629174706175
2417851639229258349412351
4835703278458516698824703
9671406556917033397649407
19342813113834066795298815
38685626227668133590597631
77371252455336267181195263
154742504910672534362390527
309485009821345068724781055
618970019642690137449562111
1237940039285380274899124223
2475880078570760549798248447
4951760157141521099596496895
9903520314283042199192993791
19807040628566084398385987583
39614081257132168796771975167
79228162514264337593543950335
158456325028528675187087900671
316912650057057350374175801343
633825300114114700748351602687
1267650600228229401496703205375
2535301200456458802993406410751
5070602400912917605986812821503
10141204801825835211973625643007
20282409603651670423947251286015
40564819207303340847894502572031
81129638414606681695789005144063
162259276829213363391578010288127
324518553658426726783156020576255
649037107316853453566312041152511
1298074214633706907132624082305023
2596148429267413814265248164610047
5192296858534827628530496329220095
10384593717069655257060992658440191
20769187434139310514121985316880383
41538374868278621028243970633760767
83076749736557242056487941267521535
166153499473114484112975882535043071
332306998946228968225951765070086143
664613997892457936451903530140172287
1329227995784915872903807060280344575
2658455991569831745807614120560689151
5316911983139663491615228241121378303
10633823966279326983230456482242756607
21267647932558653966460912964485513215
42535295865117307932921825928971026431
85070591730234615865843651857942052863
170141183460469231731687303715884105727
340282366920938463463374607431768211455
680564733841876926926749214863536422911
1361129467683753853853498429727072845823
2722258935367507707706996859454145691647
5444517870735015415413993718908291383295
10889035741470030830827987437816582766591
21778071482940061661655974875633165533183
43556142965880123323311949751266331066367
87112285931760246646623899502532662132735
174224571863520493293247799005065324265471
348449143727040986586495598010130648530943
696898287454081973172991196020261297061887
1393796574908163946345982392040522594123775
2787593149816327892691964784081045188247551
5575186299632655785383929568162090376495103
11150372599265311570767859136324180752990207
22300745198530623141535718272648361505980415
44601490397061246283071436545296723011960831
89202980794122492566142873090593446023921663
178405961588244985132285746181186892047843327
356811923176489970264571492362373784095686655
713623846352979940529142984724747568191373311
1427247692705959881058285969449495136382746623
2854495385411919762116571938898990272765493247
5708990770823839524233143877797980545530986495
11417981541647679048466287755595961091061972991
22835963083295358096932575511191922182123945983
45671926166590716193865151022383844364247891967
91343852333181432387730302044767688728495783935
182687704666362864775460604089535377456991567871
365375409332725729550921208179070754913983135743
730750818665451459101842416358141509827966271487
1461501637330902918203684832716283019655932542975
2923003274661805836407369665432566039311865085951
5846006549323611672814739330865132078623730171903
11692013098647223345629478661730264157247460343807
23384026197294446691258957323460528314494920687615
46768052394588893382517914646921056628989841375231
93536104789177786765035829293842113257979682750463
187072209578355573530071658587684226515959365500927
374144419156711147060143317175368453031918731001855
748288838313422294120286634350736906063837462003711
1496577676626844588240573268701473812127674924007423
2993155353253689176481146537402947624255349848014847
5986310706507378352962293074805895248510699696029695
11972621413014756705924586149611790497021399392059391
23945242826029513411849172299223580994042798784118783
47890485652059026823698344598447161988085597568237567
95780971304118053647396689196894323976171195136475135
191561942608236107294793378393788647952342390272950271
383123885216472214589586756787577295904684780545900543
766247770432944429179173513575154591809369561091801087
1532495540865888858358347027150309183618739122183602175
3064991081731777716716694054300618367237478244367204351
6129982163463555433433388108601236734474956488734408703
12259964326927110866866776217202473468949912977468817407
24519928653854221733733552434404946937899825954937634815
49039857307708443467467104868809893875799651909875269631
98079714615416886934934209737619787751599303819750539263
196159429230833773869868419475239575503198607639501078527
392318858461667547739736838950479151006397215279002157055
784637716923335095479473677900958302012794430558004314111
1569275433846670190958947355801916604025588861116008628223
3138550867693340381917894711603833208051177722232017256447
6277101735386680763835789423207666416102355444464034512895
12554203470773361527671578846415332832204710888928069025791
25108406941546723055343157692830665664409421777856138051583
50216813883093446110686315385661331328818843555712276103167
100433627766186892221372630771322662657637687111424552206335
200867255532373784442745261542645325315275374222849104412671
401734511064747568885490523085290650630550748445698208825343
803469022129495137770981046170581301261101496891396417650687
1606938044258990275541962092341162602522202993782792835301375
3213876088517980551083924184682325205044405987565585670602751
6427752177035961102167848369364650410088811975131171341205503
12855504354071922204335696738729300820177623950262342682411007
25711008708143844408671393477458601640355247900524685364822015
51422017416287688817342786954917203280710495801049370729644031
102844034832575377634685573909834406561420991602098741459288063
205688069665150755269371147819668813122841983204197482918576127
411376139330301510538742295639337626245683966408394965837152255
822752278660603021077484591278675252491367932816789931674304511
1645504557321206042154969182557350504982735865633579863348609023
3291009114642412084309938365114701009965471731267159726697218047
6582018229284824168619876730229402019930943462534319453394436095
13164036458569648337239753460458804039861886925068638906788872191
26328072917139296674479506920917608079723773850137277813577744383
52656145834278593348959013841835216159447547700274555627155488767
105312291668557186697918027683670432318895095400549111254310977535
210624583337114373395836055367340864637790190801098222508621955071
421249166674228746791672110734681729275580381602196445017243910143
842498333348457493583344221469363458551160763204392890034487820287
1684996666696914987166688442938726917102321526408785780068975640575
3369993333393829974333376885877453834204643052817571560137951281151
6739986666787659948666753771754907668409286105635143120275902562303
13479973333575319897333507543509815336818572211270286240551805124607
26959946667150639794667015087019630673637144422540572481103610249215
53919893334301279589334030174039261347274288845081144962207220498431
107839786668602559178668060348078522694548577690162289924414440996863
215679573337205118357336120696157045389097155380324579848828881993727
431359146674410236714672241392314090778194310760649159697657763987455
862718293348820473429344482784628181556388621521298319395315527974911
1725436586697640946858688965569256363112777243042596638790631055949823
3450873173395281893717377931138512726225554486085193277581262111899647
6901746346790563787434755862277025452451108972170386555162524223799295
13803492693581127574869511724554050904902217944340773110325048447598591
27606985387162255149739023449108101809804435888681546220650096895197183
55213970774324510299478046898216203619608871777363092441300193790394367
110427941548649020598956093796432407239217743554726184882600387580788735
220855883097298041197912187592864814478435487109452369765200775161577471
441711766194596082395824375185729628956870974218904739530401550323154943
883423532389192164791648750371459257913741948437809479060803100646309887
1766847064778384329583297500742918515827483896875618958121606201292619775
3533694129556768659166595001485837031654967793751237916243212402585239551
7067388259113537318333190002971674063309935587502475832486424805170479103
14134776518227074636666380005943348126619871175004951664972849610340958207
28269553036454149273332760011886696253239742350009903329945699220681916415
56539106072908298546665520023773392506479484700019806659891398441363832831
113078212145816597093331040047546785012958969400039613319782796882727665663
226156424291633194186662080095093570025917938800079226639565593765455331327
452312848583266388373324160190187140051835877600158453279131187530910662655
904625697166532776746648320380374280103671755200316906558262375061821325311
1809251394333065553493296640760748560207343510400633813116524750123642650623
3618502788666131106986593281521497120414687020801267626233049500247285301247
7237005577332262213973186563042994240829374041602535252466099000494570602495
14474011154664524427946373126085988481658748083205070504932198000989141204991
28948022309329048855892746252171976963317496166410141009864396001978282409983
57896044618658097711785492504343953926634992332820282019728792003956564819967
115792089237316195423570985008687907853269984665640564039457584007913129639935
231584178474632390847141970017375815706539969331281128078915168015826259279871
463168356949264781694283940034751631413079938662562256157830336031652518559743
926336713898529563388567880069503262826159877325124512315660672063305037119487
1852673427797059126777135760139006525652319754650249024631321344126610074238975
3705346855594118253554271520278013051304639509300498049262642688253220148477951
7410693711188236507108543040556026102609279018600996098525285376506440296955903
14821387422376473014217086081112052205218558037201992197050570753012880593911807
29642774844752946028434172162224104410437116074403984394101141506025761187823615
59285549689505892056868344324448208820874232148807968788202283012051522375647231
118571099379011784113736688648896417641748464297615937576404566024103044751294463
237142198758023568227473377297792835283496928595231875152809132048206089502588927
474284397516047136454946754595585670566993857190463750305618264096412179005177855
948568795032094272909893509191171341133987714380927500611236528192824358010355711
1897137590064188545819787018382342682267975428761855001222473056385648716020711423
3794275180128377091639574036764685364535950857523710002444946112771297432041422847
7588550360256754183279148073529370729071901715047420004889892225542594864082845695
15177100720513508366558296147058741458143803430094840009779784451085189728165691391
30354201441027016733116592294117482916287606860189680019559568902170379456331382783
60708402882054033466233184588234965832575213720379360039119137804340758912662765567
121416805764108066932466369176469931665150427440758720078238275608681517825325531135
242833611528216133864932738352939863330300854881517440156476551217363035650651062271
485667223056432267729865476705879726660601709763034880312953102434726071301302124543
971334446112864535459730953411759453321203419526069760625906204869452142602604249087
1942668892225729070919461906823518906642406839052139521251812409738904285205208498175
3885337784451458141838923813647037813284813678104279042503624819477808570410416996351
7770675568902916283677847627294075626569627356208558085007249638955617140820833992703
15541351137805832567355695254588151253139254712417116170014499277911234281641667985407
31082702275611665134711390509176302506278509424834232340028998555822468563283335970815
62165404551223330269422781018352605012557018849668464680057997111644937126566671941631
124330809102446660538845562036705210025114037699336929360115994223289874253133343883263
248661618204893321077691124073410420050228075398673858720231988446579748506266687766527
497323236409786642155382248146820840100456150797347717440463976893159497012533375533055
994646472819573284310764496293641680200912301594695434880927953786318994025066751066111
1989292945639146568621528992587283360401824603189390869761855907572637988050133502132223
3978585891278293137243057985174566720803649206378781739523711815145275976100267004264447
7957171782556586274486115970349133441607298412757563479047423630290551952200534008528895
15914343565113172548972231940698266883214596825515126958094847260581103904401068017057791
31828687130226345097944463881396533766429193651030253916189694521162207808802136034115583
63657374260452690195888927762793067532858387302060507832379389042324415617604272068231167
127314748520905380391777855525586135065716774604121015664758778084648831235208544136462335
254629497041810760783555711051172270131433549208242031329517556169297662470417088272924671
509258994083621521567111422102344540262867098416484062659035112338595324940834176545849343
1018517988167243043134222844204689080525734196832968125318070224677190649881668353091698687
2037035976334486086268445688409378161051468393665936250636140449354381299763336706183397375
4074071952668972172536891376818756322102936787331872501272280898708762599526673412366794751
8148143905337944345073782753637512644205873574663745002544561797417525199053346824733589503
16296287810675888690147565507275025288411747149327490005089123594835050398106693649467179007
32592575621351777380295131014550050576823494298654980010178247189670100796213387298934358015
65185151242703554760590262029100101153646988597309960020356494379340201592426774597868716031
130370302485407109521180524058200202307293977194619920040712988758680403184853549195737432063
260740604970814219042361048116400404614587954389239840081425977517360806369707098391474864127
521481209941628438084722096232800809229175908778479680162851955034721612739414196782949728255
1042962419883256876169444192465601618458351817556959360325703910069443225478828393565899456511
2085924839766513752338888384931203236916703635113918720651407820138886450957656787131798913023
4171849679533027504677776769862406473833407270227837441302815640277772901915313574263597826047
8343699359066055009355553539724812947666814540455674882605631280555545803830627148527195652095
16687398718132110018711107079449625895333629080911349765211262561111091607661254297054391304191
33374797436264220037422214158899251790667258161822699530422525122222183215322508594108782608383
66749594872528440074844428317798503581334516323645399060845050244444366430645017188217565216767
133499189745056880149688856635597007162669032647290798121690100488888732861290034376435130433535
266998379490113760299377713271194014325338065294581596243380200977777465722580068752870260867071
533996758980227520598755426542388028650676130589163192486760401955554931445160137505740521734143
1067993517960455041197510853084776057301352261178326384973520803911109862890320275011481043468287
2135987035920910082395021706169552114602704522356652769947041607822219725780640550022962086936575
4271974071841820164790043412339104229205409044713305539894083215644439451561281100045924173873151
8543948143683640329580086824678208458410818089426611079788166431288878903122562200091848347746303
17087896287367280659160173649356416916821636178853222159576332862577757806245124400183696695492607
34175792574734561318320347298712833833643272357706444319152665725155515612490248800367393390985215
68351585149469122636640694597425667667286544715412888638305331450311031224980497600734786781970431
136703170298938245273281389194851335334573089430825777276610662900622062449960995201469573563940863
273406340597876490546562778389702670669146178861651554553221325801244124899921990402939147127881727
546812681195752981093125556779405341338292357723303109106442651602488249799843980805878294255763455
1093625362391505962186251113558810682676584715446606218212885303204976499599687961611756588511526911
2187250724783011924372502227117621365353169430893212436425770606409952999199375923223513177023053823
4374501449566023848745004454235242730706338861786424872851541212819905998398751846447026354046107647
8749002899132047697490008908470485461412677723572849745703082425639811996797503692894052708092215295
17498005798264095394980017816940970922825355447145699491406164851279623993595007385788105416184430591
34996011596528190789960035633881941845650710894291398982812329702559247987190014771576210832368861183
69992023193056381579920071267763883691301421788582797965624659405118495974380029543152421664737722367
139984046386112763159840142535527767382602843577165595931249318810236991948760059086304843329475444735
279968092772225526319680285071055534765205687154331191862498637620473983897520118172609686658950889471
559936185544451052639360570142111069530411374308662383724997275240947967795040236345219373317901778943
1119872371088902105278721140284222139060822748617324767449994550481895935590080472690438746635803557887
2239744742177804210557442280568444278121645497234649534899989100963791871180160945380877493271607115775
4479489484355608421114884561136888556243290994469299069799978201927583742360321890761754986543214231551
8958978968711216842229769122273777112486581988938598139599956403855167484720643781523509973086428463103
17917957937422433684459538244547554224973163977877196279199912807710334969441287563047019946172856926207
35835915874844867368919076489095108449946327955754392558399825615420669938882575126094039892345713852415
71671831749689734737838152978190216899892655911508785116799651230841339877765150252188079784691427704831
143343663499379469475676305956380433799785311823017570233599302461682679755530300504376159569382855409663
286687326998758938951352611912760867599570623646035140467198604923365359511060601008752319138765710819327
573374653997517877902705223825521735199141247292070280934397209846730719022121202017504638277531421638655
1146749307995035755805410447651043470398282494584140561868794419693461438044242404035009276555062843277311
2293498615990071511610820895302086940796564989168281123737588839386922876088484808070018553110125686554623
4586997231980143023221641790604173881593129978336562247475177678773845752176969616140037106220251373109247
9173994463960286046443283581208347763186259956673124494950355357547691504353939232280074212440502746218495
18347988927920572092886567162416695526372519913346248989900710715095383008707878464560148424881005492436991
36695977855841144185773134324833391052745039826692497979801421430190766017415756929120296849762010984873983
73391955711682288371546268649666782105490079653384995959602842860381532034831513858240593699524021969747967
146783911423364576743092537299333564210980159306769991919205685720763064069663027716481187399048043939495935
293567822846729153486185074598667128421960318613539983838411371441526128139326055432962374798096087878991871
587135645693458306972370149197334256843920637227079967676822742883052256278652110865924749596192175757983743
1174271291386916613944740298394668513687841274454159935353645485766104512557304221731849499192384351515967487
2348542582773833227889480596789337027375682548908319870707290971532209025114608443463698998384768703031934975
4697085165547666455778961193578674054751365097816639741414581943064418050229216886927397996769537406063869951
9394170331095332911557922387157348109502730195633279482829163886128836100458433773854795993539074812127739903
18788340662190665823115844774314696219005460391266558965658327772257672200916867547709591987078149624255479807
37576681324381331646231689548629392438010920782533117931316655544515344401833735095419183974156299248510959615
75153362648762663292463379097258784876021841565066235862633311089030688803667470190838367948312598497021919231
150306725297525326584926758194517569752043683130132471725266622178061377607334940381676735896625196994043838463
300613450595050653169853516389035139504087366260264943450533244356122755214669880763353471793250393988087676927
601226901190101306339707032778070279008174732520529886901066488712245510429339761526706943586500787976175353855
1202453802380202612679414065556140558016349465041059773802132977424491020858679523053413887173001575952350707711
2404907604760405225358828131112281116032698930082119547604265954848982041717359046106827774346003151904701415423
4809815209520810450717656262224562232065397860164239095208531909697964083434718092213655548692006303809402830847
9619630419041620901435312524449124464130795720328478190417063819395928166869436184427311097384012607618805661695
19239260838083241802870625048898248928261591440656956380834127638791856333738872368854622194768025215237611323391
38478521676166483605741250097796497856523182881313912761668255277583712667477744737709244389536050430475222646783
76957043352332967211482500195592995713046365762627825523336510555167425334955489475418488779072100860950445293567
153914086704665934422965000391185991426092731525255651046673021110334850669910978950836977558144201721900890587135
307828173409331868845930000782371982852185463050511302093346042220669701339821957901673955116288403443801781174271
615656346818663737691860001564743965704370926101022604186692084441339402679643915803347910232576806887603562348543
1231312693637327475383720003129487931408741852202045208373384168882678805359287831606695820465153613775207124697087
2462625387274654950767440006258975862817483704404090416746768337765357610718575663213391640930307227550414249394175
4925250774549309901534880012517951725634967408808180833493536675530715221437151326426783281860614455100828498788351
9850501549098619803069760025035903451269934817616361666987073351061430442874302652853566563721228910201656997576703
19701003098197239606139520050071806902539869635232723333974146702122860885748605305707133127442457820403313995153407
39402006196394479212279040100143613805079739270465446667948293404245721771497210611414266254884915640806627990306815
78804012392788958424558080200287227610159478540930893335896586808491443542994421222828532509769831281613255980613631
157608024785577916849116160400574455220318957081861786671793173616982887085988842445657065019539662563226511961227263
315216049571155833698232320801148910440637914163723573343586347233965774171977684891314130039079325126453023922454527
630432099142311667396464641602297820881275828327447146687172694467931548343955369782628260078158650252906047844909055
1260864198284623334792929283204595641762551656654894293374345388935863096687910739565256520156317300505812095689818111
2521728396569246669585858566409191283525103313309788586748690777871726193375821479130513040312634601011624191379636223
5043456793138493339171717132818382567050206626619577173497381555743452386751642958261026080625269202023248382759272447
10086913586276986678343434265636765134100413253239154346994763111486904773503285916522052161250538404046496765518544895
20173827172553973356686868531273530268200826506478308693989526222973809547006571833044104322501076808092993531037089791
40347654345107946713373737062547060536401653012956617387979052445947619094013143666088208645002153616185987062074179583
80695308690215893426747474125094121072803306025913234775958104891895238188026287332176417290004307232371974124148359167
161390617380431786853494948250188242145606612051826469551916209783790476376052574664352834580008614464743948248296718335
322781234760863573706989896500376484291213224103652939103832419567580952752105149328705669160017228929487896496593436671
645562469521727147413979793000752968582426448207305878207664839135161905504210298657411338320034457858975792993186873343
1291124939043454294827959586001505937164852896414611756415329678270323811008420597314822676640068915717951585986373746687
2582249878086908589655919172003011874329705792829223512830659356540647622016841194629645353280137831435903171972747493375
5164499756173817179311838344006023748659411585658447025661318713081295244033682389259290706560275662871806343945494986751
10328999512347634358623676688012047497318823171316894051322637426162590488067364778518581413120551325743612687890989973503
20657999024695268717247353376024094994637646342633788102645274852325180976134729557037162826241102651487225375781979947007
41315998049390537434494706752048189989275292685267576205290549704650361952269459114074325652482205302974450751563959894015
82631996098781074868989413504096379978550585370535152410581099409300723904538918228148651304964410605948901503127919788031
165263992197562149737978827008192759957101170741070304821162198818601447809077836456297302609928821211897803006255839576063
330527984395124299475957654016385519914202341482140609642324397637202895618155672912594605219857642423795606012511679152127
661055968790248598951915308032771039828404682964281219284648795274405791236311345825189210439715284847591212025023358304255
1322111937580497197903830616065542079656809365928562438569297590548811582472622691650378420879430569695182424050046716608511
2644223875160994395807661232131084159313618731857124877138595181097623164945245383300756841758861139390364848100093433217023
5288447750321988791615322464262168318627237463714249754277190362195246329890490766601513683517722278780729696200186866434047
10576895500643977583230644928524336637254474927428499508554380724390492659780981533203027367035444557561459392400373732868095
21153791001287955166461289857048673274508949854856999017108761448780985319561963066406054734070889115122918784800747465736191
42307582002575910332922579714097346549017899709713998034217522897561970639123926132812109468141778230245837569601494931472383
84615164005151820665845159428194693098035799419427996068435045795123941278247852265624218936283556460491675139202989862944767
169230328010303641331690318856389386196071598838855992136870091590247882556495704531248437872567112920983350278405979725889535
338460656020607282663380637712778772392143197677711984273740183180495765112991409062496875745134225841966700556811959451779071
676921312041214565326761275425557544784286395355423968547480366360991530225982818124993751490268451683933401113623918903558143
1353842624082429130653522550851115089568572790710847937094960732721983060451965636249987502980536903367866802227247837807116287
2707685248164858261307045101702230179137145581421695874189921465443966120903931272499975005961073806735733604454495675614232575
5415370496329716522614090203404460358274291162843391748379842930887932241807862544999950011922147613471467208908991351228465151
10830740992659433045228180406808920716548582325686783496759685861775864483615725089999900023844295226942934417817982702456930303
21661481985318866090456360813617841433097164651373566993519371723551728967231450179999800047688590453885868835635965404913860607
43322963970637732180912721627235682866194329302747133987038743447103457934462900359999600095377180907771737671271930809827721215
86645927941275464361825443254471365732388658605494267974077486894206915868925800719999200190754361815543475342543861619655442431
173291855882550928723650886508942731464777317210988535948154973788413831737851601439998400381508723631086950685087723239310884863
346583711765101857447301773017885462929554634421977071896309947576827663475703202879996800763017447262173901370175446478621769727
693167423530203714894603546035770925859109268843954143792619895153655326951406405759993601526034894524347802740350892957243539455
1386334847060407429789207092071541851718218537687908287585239790307310653902812811519987203052069789048695605480701785914487078911
2772669694120814859578414184143083703436437075375816575170479580614621307805625623039974406104139578097391210961403571828974157823
5545339388241629719156828368286167406872874150751633150340959161229242615611251246079948812208279156194782421922807143657948315647
11090678776483259438313656736572334813745748301503266300681918322458485231222502492159897624416558312389564843845614287315896631295
22181357552966518876627313473144669627491496603006532601363836644916970462445004984319795248833116624779129687691228574631793262591
44362715105933037753254626946289339254982993206013065202727673289833940924890009968639590497666233249558259375382457149263586525183
88725430211866075506509253892578678509965986412026130405455346579667881849780019937279180995332466499116518750764914298527173050367
177450860423732151013018507785157357019931972824052260810910693159335763699560039874558361990664932998233037501529828597054346100735
354901720847464302026037015570314714039863945648104521621821386318671527399120079749116723981329865996466075003059657194108692201471
709803441694928604052074031140629428079727891296209043243642772637343054798240159498233447962659731992932150006119314388217384402943
1419606883389857208104148062281258856159455782592418086487285545274686109596480318996466895925319463985864300012238628776434768805887
2839213766779714416208296124562517712318911565184836172974571090549372219192960637992933791850638927971728600024477257552869537611775
5678427533559428832416592249125035424637823130369672345949142181098744438385921275985867583701277855943457200048954515105739075223551
11356855067118857664833184498250070849275646260739344691898284362197488876771842551971735167402555711886914400097909030211478150447103
22713710134237715329666368996500141698551292521478689383796568724394977753543685103943470334805111423773828800195818060422956300894207
45427420268475430659332737993000283397102585042957378767593137448789955507087370207886940669610222847547657600391636120845912601788415
90854840536950861318665475986000566794205170085914757535186274897579911014174740415773881339220445695095315200783272241691825203576831
181709681073901722637330951972001133588410340171829515070372549795159822028349480831547762678440891390190630401566544483383650407153663
363419362147803445274661903944002267176820680343659030140745099590319644056698961663095525356881782780381260803133088966767300814307327
726838724295606890549323807888004534353641360687318060281490199180639288113397923326191050713763565560762521606266177933534601628614655
1453677448591213781098647615776009068707282721374636120562980398361278576226795846652382101427527131121525043212532355867069203257229311
2907354897182427562197295231552018137414565442749272241125960796722557152453591693304764202855054262243050086425064711734138406514458623
5814709794364855124394590463104036274829130885498544482251921593445114304907183386609528405710108524486100172850129423468276813028917247
11629419588729710248789180926208072549658261770997088964503843186890228609814366773219056811420217048972200345700258846936553626057834495
23258839177459420497578361852416145099316523541994177929007686373780457219628733546438113622840434097944400691400517693873107252115668991
46517678354918840995156723704832290198633047083988355858015372747560914439257467092876227245680868195888801382801035387746214504231337983
93035356709837681990313447409664580397266094167976711716030745495121828878514934185752454491361736391777602765602070775492429008462675967
186070713419675363980626894819329160794532188335953423432061490990243657757029868371504908982723472783555205531204141550984858016925351935
372141426839350727961253789638658321589064376671906846864122981980487315514059736743009817965446945567110411062408283101969716033850703871
744282853678701455922507579277316643178128753343813693728245963960974631028119473486019635930893891134220822124816566203939432067701407743
1488565707357402911845015158554633286356257506687627387456491927921949262056238946972039271861787782268441644249633132407878864135402815487
2977131414714805823690030317109266572712515013375254774912983855843898524112477893944078543723575564536883288499266264815757728270805630975
5954262829429611647380060634218533145425030026750509549825967711687797048224955787888157087447151129073766576998532529631515456541611261951
11908525658859223294760121268437066290850060053501019099651935423375594096449911575776314174894302258147533153997065059263030913083222523903
23817051317718446589520242536874132581700120107002038199303870846751188192899823151552628349788604516295066307994130118526061826166445047807
47634102635436893179040485073748265163400240214004076398607741693502376385799646303105256699577209032590132615988260237052123652332890095615
95268205270873786358080970147496530326800480428008152797215483387004752771599292606210513399154418065180265231976520474104247304665780191231
190536410541747572716161940294993060653600960856016305594430966774009505543198585212421026798308836130360530463953040948208494609331560382463
381072821083495145432323880589986121307201921712032611188861933548019011086397170424842053596617672260721060927906081896416989218663120764927
762145642166990290864647761179972242614403843424065222377723867096038022172794340849684107193235344521442121855812163792833978437326241529855
1524291284333980581729295522359944485228807686848130444755447734192076044345588681699368214386470689042884243711624327585667956874652483059711
3048582568667961163458591044719888970457615373696260889510895468384152088691177363398736428772941378085768487423248655171335913749304966119423
6097165137335922326917182089439777940915230747392521779021790936768304177382354726797472857545882756171536974846497310342671827498609932238847
12194330274671844653834364178879555881830461494785043558043581873536608354764709453594945715091765512343073949692994620685343654997219864477695
24388660549343689307668728357759111763660922989570087116087163747073216709529418907189891430183531024686147899385989241370687309994439728955391
48777321098687378615337456715518223527321845979140174232174327494146433419058837814379782860367062049372295798771978482741374619988879457910783
97554642197374757230674913431036447054643691958280348464348654988292866838117675628759565720734124098744591597543956965482749239977758915821567
195109284394749514461349826862072894109287383916560696928697309976585733676235351257519131441468248197489183195087913930965498479955517831643135
390218568789499028922699653724145788218574767833121393857394619953171467352470702515038262882936496394978366390175827861930996959911035663286271
780437137578998057845399307448291576437149535666242787714789239906342934704941405030076525765872992789956732780351655723861993919822071326572543
1560874275157996115690798614896583152874299071332485575429578479812685869409882810060153051531745985579913465560703311447723987839644142653145087
3121748550315992231381597229793166305748598142664971150859156959625371738819765620120306103063491971159826931121406622895447975679288285306290175
6243497100631984462763194459586332611497196285329942301718313919250743477639531240240612206126983942319653862242813245790895951358576570612580351
12486994201263968925526388919172665222994392570659884603436627838501486955279062480481224412253967884639307724485626491581791902717153141225160703
24973988402527937851052777838345330445988785141319769206873255677002973910558124960962448824507935769278615448971252983163583805434306282450321407
49947976805055875702105555676690660891977570282639538413746511354005947821116249921924897649015871538557230897942505966327167610868612564900642815
99895953610111751404211111353381321783955140565279076827493022708011895642232499843849795298031743077114461795885011932654335221737225129801285631
199791907220223502808422222706762643567910281130558153654986045416023791284464999687699590596063486154228923591770023865308670443474450259602571263
399583814440447005616844445413525287135820562261116307309972090832047582568929999375399181192126972308457847183540047730617340886948900519205142527
799167628880894011233688890827050574271641124522232614619944181664095165137859998750798362384253944616915694367080095461234681773897801038410285055
1598335257761788022467377781654101148543282249044465229239888363328190330275719997501596724768507889233831388734160190922469363547795602076820570111
3196670515523576044934755563308202297086564498088930458479776726656380660551439995003193449537015778467662777468320381844938727095591204153641140223
6393341031047152089869511126616404594173128996177860916959553453312761321102879990006386899074031556935325554936640763689877454191182408307282280447
12786682062094304179739022253232809188346257992355721833919106906625522642205759980012773798148063113870651109873281527379754908382364816614564560895
25573364124188608359478044506465618376692515984711443667838213813251045284411519960025547596296126227741302219746563054759509816764729633229129121791
51146728248377216718956089012931236753385031969422887335676427626502090568823039920051095192592252455482604439493126109519019633529459266458258243583
102293456496754433437912178025862473506770063938845774671352855253004181137646079840102190385184504910965208878986252219038039267058918532916516487167
204586912993508866875824356051724947013540127877691549342705710506008362275292159680204380770369009821930417757972504438076078534117837065833032974335
409173825987017733751648712103449894027080255755383098685411421012016724550584319360408761540738019643860835515945008876152157068235674131666065948671
818347651974035467503297424206899788054160511510766197370822842024033449101168638720817523081476039287721671031890017752304314136471348263332131897343
1636695303948070935006594848413799576108321023021532394741645684048066898202337277441635046162952078575443342063780035504608628272942696526664263794687

三. IPython交互式python

1. IPython的魔法命令

%lsmagic
Available line magics:
%alias  %alias_magic  %autoawait  %autocall  %automagic  %autosave  %bookmark  %cat  %cd  %clear  %colors  %conda  %config  %connect_info  %cp  %debug  %dhist  %dirs  %doctest_mode  %ed  %edit  %env  %gui  %hist  %history  %killbgscripts  %ldir  %less  %lf  %lk  %ll  %load  %load_ext  %loadpy  %logoff  %logon  %logstart  %logstate  %logstop  %ls  %lsmagic  %lx  %macro  %magic  %man  %matplotlib  %mkdir  %more  %mv  %notebook  %page  %pastebin  %pdb  %pdef  %pdoc  %pfile  %pinfo  %pinfo2  %pip  %popd  %pprint  %precision  %prun  %psearch  %psource  %pushd  %pwd  %pycat  %pylab  %qtconsole  %quickref  %recall  %rehashx  %reload_ext  %rep  %rerun  %reset  %reset_selective  %rm  %rmdir  %run  %save  %sc  %set_env  %store  %sx  %system  %tb  %time  %timeit  %unalias  %unload_ext  %who  %who_ls  %whos  %xdel  %xmode

Available cell magics:
%%!  %%HTML  %%SVG  %%bash  %%capture  %%debug  %%file  %%html  %%javascript  %%js  %%latex  %%markdown  %%perl  %%prun  %%pypy  %%python  %%python2  %%python3  %%ruby  %%script  %%sh  %%svg  %%sx  %%system  %%time  %%timeit  %%writefile

Automagic is ON, % prefix IS NOT needed for line magics.
%timeit np.linalg.eigvals(np.random.rand(100,100))
6.1 ms ± 92.4 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
%%timeit a = np.random.rand(100, 100)
np.linalg.eigvals(a)
6.15 ms ± 254 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
%%capture capt
from __future__ import print_function
import sys
print('Hello stdout')
print('and stderr', file=sys.stderr)
capt.stdout,capt.stderr
('Hello stdout\n', 'and stderr\n')
capt.show()
Hello stdout


and stderr
%%bash --out output --err error
echo "hi,stdout"
echo "hello,stdderr" >&2
print(output)
print(error)
hi,stdout

hello,stdderr
%%writefile foo.py
print('Hello,world!')
Writing foo.py
%run foo.py
Hello,world!
%%python3 --bg --out bgout
import time
for i in range(10):
    time.sleep(0.5)
    print("line %s"%i)
print(bgout.read())
b'line 0\nline 1\nline 2\nline 3\nline 4\nline 5\nline 6\nline 7\nline 8\nline 9\n'
%%script python2
import sys
print 'Hello from Python %s'%sys.version
Hello from Python 2.7.15+ (default, Oct  7 2019, 17:39:04) 
[GCC 7.4.0]
%%script python3
import sys
print('Hellow from Python %s'%sys.version)
Hellow from Python 3.7.3 | packaged by conda-forge | (default, Jul  1 2019, 21:52:21) 
[GCC 7.3.0]


可以简写直接%%ruby,%%perl,%%bash等
%%ruby
puts 'Hello from Ruby #{RUBY_VERSION}'
Couldn't find program: 'ruby'
%%bash
echo "Hello from $BASH"
ls
Hello from /bin/bash
17MindMap.md
foo.py
images
python-learning-node.ipynb
python-learning-node.md
README.md

2. 丰富的显示模块

from IPython.display import Image
i=Image(filename='images/map.png')
i

png

display(i)

png

Image(url='http://python.org/images/python-logo.gif')

from IPython.display import HTML
s="""
<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
</tr>
</table>
"""
display(HTML(s))
Header 1 Header 2
row 1, cell 1 row 1, cell 2
row 2, cell 1 row 2, cell 2
%%html
<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
</tr>
</table>
Header 1 Header 2
row 1, cell 1 row 1, cell 2
row 2, cell 1 row 2, cell 2
%%javascript
console.log('hi')
//alert('hi')
from IPython.display import Javascript
js=Javascript('alert("hi")')
//display(js)
from IPython.display import IFrame
IFrame("https://www.baidu.com",width='100%',height='250px')
from IPython.display import FileLink,FileLinks
FileLink('foo.py')
FileLinks('.')

./
  17MindMap.md
  README.md
  foo.py
  python-learning-node.md
  python-learning-node.ipynb
./.ipynb_checkpoints/
  python-learning-node-checkpoint.md
  17MindMap-checkpoint.md
  python-learning-node-checkpoint.ipynb
  README-checkpoint.md
  foo-checkpoint.py
./images/
  函数_模块.png
  模块.png
  异常处理.png
  文件对象.png
  标准数据类型1数值_字典_集合.png
  标准类型补充.png
  计算机基础.png
  标准数据类型2.png
  进阶_补充知识.png
  进阶_面向对象编程.png
  1571579739829.png
  map.png
  进阶_条件_循环.png
  1571579617096.png
  条件_循环.png
  测试调试.png
  标准数据类型3.png
  面向对象编程.png
  python语言基础.png
  进阶函数.png
./images/.ipynb_checkpoints/
  1571579739829-checkpoint.png
  map-checkpoint.png
./images/.ipynb_checkpoints/.ipynb_checkpoints/
  1571579739829-checkpoint-checkpoint.png

四. Python基本语法

1. 变量、运算符和内置函数

x=2
y=3
xy='hhh'
print(x+y)
print(xy)
5
hhh
x=y=3
print(x+y,x*y,x,y)
6 9 3 3
符号 描述
+
-
*
/
% 模,求余数
// 整后floor
** 幂,power
%%python2
print 1+2
print 3-2
print 4*5
print 6/2
print 6.0/4
print 6//4
print 6%4
print 2**3
3
1
20
3
1.5
1
2
8
符号 描述
== 相等返回True
!= 不等返回True
< 小于
> 大于
<= 小于等于
>= 大于等于
符号 描述
& 逻辑与
| 逻辑或
^ 异或,xor
~
» 右移
« 左移
a=6
b=5
print(bin(a),bin(b))
print(a&b)
print(bin(a&b))
print(a|b)
print(bin(a|b))
0b110 0b101
4
0b100
7
0b111
print(5>>1) #二进制数字右移一位,最左边位补零
print(5<<1) #二进制数字左移一位,最右边位补零
bin(5)
2
10





'0b101'

16进制转换函数hex(),8进制转换函数oct(),2进制转换函数bin().16进制以’0x’开头,8进制以’0o’开头,2进制以’0b’开头

hex(800)
'0x320'
oct(800)
'0o1440'
bin(800)
'0b1100100000'

int()函数可将任意进制转换成10进制数字,可以把字符串转换成数字

print(int('1440',8))
800
print(int('0x320',16))
800
print(int('1100100000',2))
800
print(int('38883'))
38883

chr()函数将ASCII数值转换成字符,ord()将字符转换成ASCII数值

chr(78)
'N'
ord('a')
97
round(3.14159),round(3.14159,3) #round有四舍五入
(3, 3.142)
c=complex('3+4j') #定义一个复数
print(abs(c)) #abs求绝对值,复数的绝对值是 实数与虚数的平方和再开根号
5.0
aaa=input('输入一个字符串 \t') #python3没有raw_input
输入一个字符串 	 1111
print(aaa,type(aaa))
1111 <class 'str'>

2. 字符串骚操作o(∩_∩)o 哈哈

print('小明说:"明天放假"')
print("小明说:\"明天放假\"")
print(r'小明说:"明天放假"')
print("""
小明说:"明天放假"
""")
小明说:"明天放假"
小明说:"明天放假"
小明说:"明天放假"

小明说:"明天放假"
print(5*'小明说:"明天放假"\n'+'老师说:"滚出去!"\n')
小明说:"明天放假"
小明说:"明天放假"
小明说:"明天放假"
小明说:"明天放假"
小明说:"明天放假"
老师说:"滚出去!"
word='python'
l=len(word)
for i in range(l):
    print("%d:%s"%(i,word[i]))
    print("%d:%s"%(-1-i,word[-1-i]))
word='ppythonn'
print(word.capitalize(),word.casefold())
0:p
-1:n
1:y
-2:o
2:t
-3:h
3:h
-4:t
4:o
-5:y
5:n
-6:p
Ppythonn ppythonn
"{0}说:{1}滚出去!".format('老师','小明')
'老师说:小明滚出去!'
print("%s说:我不想上课\n%s说:回家养猪!\n"%('小明','妈妈'))
小明说:我不想上课
妈妈说:回家养猪!

3. 列表

arr=[0,1,1,2,3,5,8,13,21]
print(arr[0:3])
print(arr[-3:])
print(len(arr))
print(arr+[34,55])
arr.append(89)
print(arr)
[0, 1, 1]
[8, 13, 21]
9
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
[0, 1, 1, 2, 3, 5, 8, 13, 21, 89]

4. 牛刀小试

斐波那契序列
a,b=0,1
c=[]
while a<100:
    print(a)
    c.append(a)
    a,b=b,a+b
print("Fibonacci series:%s"%str(c))
0
1
1
2
3
5
8
13
21
34
55
89
Fibonacci series:[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

5. 控制语句

for n in range(2,100):
    for x in range(2,n):
        if n%x==0:
            print("%d=%d*%d"%(n,x,n//x))
            break
    else:
        print("%d是个素数"%n)
2是个素数
3是个素数
4=2*2
5是个素数
6=2*3
7是个素数
8=2*4
9=3*3
10=2*5
11是个素数
12=2*6
13是个素数
14=2*7
15=3*5
16=2*8
17是个素数
18=2*9
19是个素数
20=2*10
21=3*7
22=2*11
23是个素数
24=2*12
25=5*5
26=2*13
27=3*9
28=2*14
29是个素数
30=2*15
31是个素数
32=2*16
33=3*11
34=2*17
35=5*7
36=2*18
37是个素数
38=2*19
39=3*13
40=2*20
41是个素数
42=2*21
43是个素数
44=2*22
45=3*15
46=2*23
47是个素数
48=2*24
49=7*7
50=2*25
51=3*17
52=2*26
53是个素数
54=2*27
55=5*11
56=2*28
57=3*19
58=2*29
59是个素数
60=2*30
61是个素数
62=2*31
63=3*21
64=2*32
65=5*13
66=2*33
67是个素数
68=2*34
69=3*23
70=2*35
71是个素数
72=2*36
73是个素数
74=2*37
75=3*25
76=2*38
77=7*11
78=2*39
79是个素数
80=2*40
81=3*27
82=2*41
83是个素数
84=2*42
85=5*17
86=2*43
87=3*29
88=2*44
89是个素数
90=2*45
91=7*13
92=2*46
93=3*31
94=2*47
95=5*19
96=2*48
97是个素数
98=2*49
99=3*33

6. 函数

def shopping(items,*arguments,**keywords):
    print('-- 我想要买%s,多少钱?'%items)
    for arg in arguments:
        print(arg)
    print("-"*40)
    for kw in keywords:
        print(kw,":",keywords[kw])
shopping('米','-- 1斤3块钱','-- 冬季新米,煮饭很香',supermarket='沃尔玛',placeofproduction='福建厦门',price='3¥')
-- 我想要买米,多少钱?
-- 1斤3块钱
-- 冬季新米,煮饭很香
----------------------------------------
supermarket : 沃尔玛
placeofproduction : 福建厦门
price : 3¥
def f(pos1, pos2, /, pos_or_kwd, *, kwd1, kwd2):
      -----------    ----------     ----------
        |             |                  |
        |        Positional or keyword   |
        |                                - Keyword only
         -- Positional only
/之前的位置参数,*之后的关键字参数
在这里/好像不生效
#def pos_f(pos1,pos2,/):
#    print('位置参数pos1:%s,pos2:%s'%(pos1,pos2))
def pos_or_kwd_f(pos_or_kwd):
    print('可用位置参数或关键字参数标识pos_or_kwd:%s'%pos_or_kwd)
def kwd_f(*,kwd1,kwd2):   
    print('只能用关键字参数的kwd1:%s,kws2:%s'%(kwd1,kwd2))

#pos_f('pos1','pos2')
pos_or_kwd_f('pos')
pos_or_kwd_f(pos_or_kwd='kwd')
kwd_f(kwd1='kwd1',kwd2='kwd2')
可用位置参数或关键字参数标识pos_or_kwd:pos
可用位置参数或关键字参数标识pos_or_kwd:kwd
只能用关键字参数的kwd1:kwd1,kws2:kwd2
def foo(*arr,sep=','):
    return sep.join(arr)
arr=['h','e','l','l','o']
print(foo(*arr,sep='*'))
h*e*l*l*o
def dicfun(**d):
    for k in d:
        print('%s=>%s'%(k,d[k]))
d={"name":"test","age":30,"height":173}
dicfun(**d)
name=>test
age=>30
height=>173
test=lambda x,y:x**y
test(2,3)
8



与map,filter等函数配合使用
foo = [2, 18, 9, 22, 17, 24, 8, 12, 27]
print(list(filter(lambda x:x%4==0,foo)))
print(list(map(lambda x:x**2,foo)))
[24, 8, 12]
[4, 324, 81, 484, 289, 576, 64, 144, 729]


与sort结合进行排序
pairs = [(1, 'one',9), (2, 'two',8), (3, 'three',7), (4, 'four',6)]
pairs.sort(key=lambda p:p[0],reverse=True)
print(pairs)
[(4, 'four', 6), (3, 'three', 7), (2, 'two', 8), (1, 'one', 9)]
def f(k:str)->str:
    """@des 打印显示函数注释文档和标注
       @autor jireh
    """
    print(f.__doc__)
    print(f.__annotations__)
    return k
print(f('函数标注'))
@des 打印显示函数注释文档和标注
       @autor jireh
    
{'k': <class 'str'>, 'return': <class 'str'>}
函数标注

7. 数据结构

ls=[1,2,3,4,5]
print('list.append表示在列表结尾插入增加元素')
ls.append(6)
print(ls)
print('list.extend表示在列表结尾扩展列表')
ls.extend([7,8,9])
print(ls)
print('list.insert表示在列表给定位置插入元素0')
ls.insert(4,0)
print(ls)
print('list.remove表示移除列表中元素0,如果没有就报错')
ls.remove(0)
print(ls)
#ls.remove(10)
#print(ls)
print('list.pop删除列表中给定位置的元素并返回它,如果没有给定位置,默认删除并返回列表最后一个元素')
ls.pop()
print(ls)
ls.pop(3)
print(ls)
print('list.index返回列表中值为5的索引,如果没有就报错')
print(ls.index(5))
#print(ls.index(9))
print('list.count计算某个元素出现的次数')
print(ls.count(5))
print('list.sort排序')
ls.sort(reverse=True)
print(ls)
print('list.reverse反转')
ls.reverse()
print(ls)
print('list.copy浅拷贝')
newls=ls.copy()
newls.reverse()
print('新的:',newls)
print('原来的:',ls)
list.append表示在列表结尾插入增加元素
[1, 2, 3, 4, 5, 6]
list.extend表示在列表结尾扩展列表
[1, 2, 3, 4, 5, 6, 7, 8, 9]
list.insert表示在列表给定位置插入元素0
[1, 2, 3, 4, 0, 5, 6, 7, 8, 9]
list.remove表示移除列表中元素0,如果没有就报错
[1, 2, 3, 4, 5, 6, 7, 8, 9]
list.pop删除列表中给定位置的元素并返回它,如果没有给定位置,默认删除并返回列表最后一个元素
[1, 2, 3, 4, 5, 6, 7, 8]
[1, 2, 3, 5, 6, 7, 8]
list.index返回列表中值为5的索引,如果没有就报错
3
list.count计算某个元素出现的次数
1
list.sort排序
[8, 7, 6, 5, 3, 2, 1]
list.reverse反转
[1, 2, 3, 5, 6, 7, 8]
list.copy浅拷贝
新的: [8, 7, 6, 5, 3, 2, 1]
原来的: [1, 2, 3, 5, 6, 7, 8]
from collections import deque
qu=deque(['alon','tes','eglon'])
qu.append('ddk')
qu.append('kkjk')
print(qu)
print(qu.popleft())
print(qu)
print(qu.popleft())
print(list(qu))
deque(['alon', 'tes', 'eglon', 'ddk', 'kkjk'])
alon
deque(['tes', 'eglon', 'ddk', 'kkjk'])
tes
['eglon', 'ddk', 'kkjk']
list(map(lambda x:x**2,range(10)))
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
[x**2 for x in range(10)]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
[(x,y) for x in range(10) for y in range(2,10) if x%y==0 and x>=y]
[(2, 2),
 (3, 3),
 (4, 2),
 (4, 4),
 (5, 5),
 (6, 2),
 (6, 3),
 (6, 6),
 (7, 7),
 (8, 2),
 (8, 4),
 (8, 8),
 (9, 3),
 (9, 9)]
vec=[[1,2,3],[4,5,6],[7,8,9]]
[x for y in vec for x in y]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
from math import pi
[round(pi,x) for x in range(5,10)]
[3.14159, 3.141593, 3.1415927, 3.14159265, 3.141592654]
arr=[
    [1,2,3,4],
    [5,6,7,8],
    [9,10,11,12]
]
[[row[i] for row in arr] for i in range(4)]
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
help(zip)
Help on class zip in module builtins:

class zip(object)
 |  zip(iter1 [,iter2 [...]]) --> zip object
 |  
 |  Return a zip object whose .__next__() method returns a tuple where
 |  the i-th element comes from the i-th iterable argument.  The .__next__()
 |  method continues until the shortest iterable in the argument sequence
 |  is exhausted and then it raises StopIteration.
 |  
 |  Methods defined here:
 |  
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |  
 |  __iter__(self, /)
 |      Implement iter(self).
 |  
 |  __next__(self, /)
 |      Implement next(self).
 |  
 |  __reduce__(...)
 |      Return state information for pickling.
 |  
 |  ----------------------------------------------------------------------
 |  Static methods defined here:
 |  
 |  __new__(*args, **kwargs) from builtins.type
 |      Create and return a new object.  See help(type) for accurate signature.
list(zip(*arr))
[(1, 5, 9), (2, 6, 10), (3, 7, 11), (4, 8, 12)]
a=list(range(10))
del a[5]
print(a)
del a[3:7]
print(a)
del a[:]
print(a)
[0, 1, 2, 3, 4, 6, 7, 8, 9]
[0, 1, 2, 8, 9]
[]

8. 元组与集合

t=12,345,'hello'
print(t[0])
print(t)
u=t,(1,2,3,4,5)
print(u)
print(list(zip(*u)))
12
(12, 345, 'hello')
((12, 345, 'hello'), (1, 2, 3, 4, 5))
[(12, 1), (345, 2), ('hello', 3)]
x,y,z=t
print(x,y,z)
12 345 hello

Python也包含有 集合 类型。集合是由不重复元素组成的无序的集。它的基本用法包括成员检测和消除重复元素。集合对象也支持像 联合,交集,差集,对称差分等数学运算。

a=set('abcdddskjksiyk')
b={'k','s','l','k','j','s'}
print(a,b)
{'j', 'a', 'y', 'd', 'b', 'k', 'i', 's', 'c'} {'j', 's', 'k', 'l'}
#在a不在b的字母
a-b
{'a', 'b', 'c', 'd', 'i', 'y'}
#在a或在b或两个都在的字母
a|b
{'a', 'b', 'c', 'd', 'i', 'j', 'k', 'l', 's', 'y'}
#既在a又在b的字母,即两者交集
a&b
{'j', 'k', 's'}
#或者在a或者在b,但不能同时在两者里面
a^b
{'a', 'b', 'c', 'd', 'i', 'l', 'y'}
#在a不在b
c={x for x in a if x not in b}
print(c)
{'a', 'y', 'd', 'b', 'i', 'c'}

9. 字典

tel={'ccb':'95533','icbc':'95588'}
print(tel['ccb'])
95533
tel['psbc']='95580'
print(tel)
{'ccb': '95533', 'icbc': '95588', 'psbc': '95580'}
print(list(tel))
print('ccb' in tel)
print('cbc' in tel)
print(sorted(tel))
a=dict([('abc',123),('def',456)])
print(a)
['ccb', 'icbc', 'psbc']
True
False
['ccb', 'icbc', 'psbc']
{'abc': 123, 'def': 456}
d={x:x**2 for x in range(10)}
print(d)
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}

10. 循环的技巧

d={'a':1,'b':2,'c':3}
print(d.items())
for k,v in d.items():
    print(k,v)

dict_items([('a', 1), ('b', 2), ('c', 3)])
a 1
b 2
c 3
l=[3,5,6,7,9,10]
for k,v in enumerate(l):
    print(k,v)
0 3
1 5
2 6
3 7
4 9
5 10
questions = ['name', 'quest', 'favorite color']
answers = ['lancelot', 'the holy grail', 'blue']
for q,a in zip(questions,answers):
    print('What is your {0}?  It is {1}.'.format(q, a))
What is your name?  It is lancelot.
What is your quest?  It is the holy grail.
What is your favorite color?  It is blue.

11. 模块与包

模块是一个包含Python定义和语句的文件。文件名就是模块名后跟文件后缀 .py 。

%%writefile fibo.py
# Fibonacci numbers module

def fib(n):    # write Fibonacci series up to n
    a, b = 0, 1
    while a < n:
        print(a, end=' ')
        a, b = b, a+b
    print()

def fib2(n):   # return Fibonacci series up to n
    result = []
    a, b = 0, 1
    while a < n:
        result.append(a)
        a, b = b, a+b
    return result

if __name__=="__main__":
    import sys
    fib(int(sys.argv[1]))
Overwriting fibo.py
import fibo
print(fibo.__name__)
fibo.fib(1000)
fibo.fib2(500)
fibo
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 





[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]

在程序结尾添加以下代码:

if __name__ == "__main__":
    import sys
    fib(int(sys.argv[1]))
!python fibo.py 2000
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 
print(sys.path)
['/home/jovyan/AI-Learning-Note/program', '/usr/local/spark/python', '/usr/local/spark/python/lib/py4j-0.10.7-src.zip', '/opt/conda/lib/python37.zip', '/opt/conda/lib/python3.7', '/opt/conda/lib/python3.7/lib-dynload', '', '/opt/conda/lib/python3.7/site-packages', '/opt/conda/lib/python3.7/site-packages/IPython/extensions', '/home/jovyan/.ipython']
import sys
print(sys.ps1,sys.ps2)
sys.ps1='>>>'
print(sys.ps1)
>>> ...: 
>>>
dir(fibo)
['__builtins__',
 '__cached__',
 '__doc__',
 '__file__',
 '__loader__',
 '__name__',
 '__package__',
 '__spec__',
 'fib',
 'fib2']
import builtins
dir(builtins)
['ArithmeticError',
 'AssertionError',
 'AttributeError',
 'BaseException',
 'BlockingIOError',
 'BrokenPipeError',
 'BufferError',
 'BytesWarning',
 'ChildProcessError',
 'ConnectionAbortedError',
 'ConnectionError',
 'ConnectionRefusedError',
 'ConnectionResetError',
 'DeprecationWarning',
 'EOFError',
 'Ellipsis',
 'EnvironmentError',
 'Exception',
 'False',
 'FileExistsError',
 'FileNotFoundError',
 'FloatingPointError',
 'FutureWarning',
 'GeneratorExit',
 'IOError',
 'ImportError',
 'ImportWarning',
 'IndentationError',
 'IndexError',
 'InterruptedError',
 'IsADirectoryError',
 'KeyError',
 'KeyboardInterrupt',
 'LookupError',
 'MemoryError',
 'ModuleNotFoundError',
 'NameError',
 'None',
 'NotADirectoryError',
 'NotImplemented',
 'NotImplementedError',
 'OSError',
 'OverflowError',
 'PendingDeprecationWarning',
 'PermissionError',
 'ProcessLookupError',
 'RecursionError',
 'ReferenceError',
 'ResourceWarning',
 'RuntimeError',
 'RuntimeWarning',
 'StopAsyncIteration',
 'StopIteration',
 'SyntaxError',
 'SyntaxWarning',
 'SystemError',
 'SystemExit',
 'TabError',
 'TimeoutError',
 'True',
 'TypeError',
 'UnboundLocalError',
 'UnicodeDecodeError',
 'UnicodeEncodeError',
 'UnicodeError',
 'UnicodeTranslateError',
 'UnicodeWarning',
 'UserWarning',
 'ValueError',
 'Warning',
 'ZeroDivisionError',
 '__IPYTHON__',
 '__build_class__',
 '__debug__',
 '__doc__',
 '__import__',
 '__loader__',
 '__name__',
 '__package__',
 '__spec__',
 'abs',
 'all',
 'any',
 'ascii',
 'bin',
 'bool',
 'breakpoint',
 'bytearray',
 'bytes',
 'callable',
 'chr',
 'classmethod',
 'compile',
 'complex',
 'copyright',
 'credits',
 'delattr',
 'dict',
 'dir',
 'display',
 'divmod',
 'enumerate',
 'eval',
 'exec',
 'filter',
 'float',
 'format',
 'frozenset',
 'get_ipython',
 'getattr',
 'globals',
 'hasattr',
 'hash',
 'help',
 'hex',
 'id',
 'input',
 'int',
 'isinstance',
 'issubclass',
 'iter',
 'len',
 'license',
 'list',
 'locals',
 'map',
 'max',
 'memoryview',
 'min',
 'next',
 'object',
 'oct',
 'open',
 'ord',
 'pow',
 'print',
 'property',
 'range',
 'repr',
 'reversed',
 'round',
 'set',
 'setattr',
 'slice',
 'sorted',
 'staticmethod',
 'str',
 'sum',
 'super',
 'tuple',
 'type',
 'vars',
 'zip']

必须要有 __init__.py 文件才能让 Python 将包含该文件的目录当作包。__init__.py可以是空文件

在__init__.py中定义__all__=[“模块名”],可以限制import *导入的模块

12. 输入与输出

import math
print(f"圆周率精确到小数点后6位:{math.pi:.6f}")
圆周率精确到小数点后6位:3.141593
table={'name':'斗破穹苍','type':'网络小说','content':'主角叫王林'}
print('有一本{type}叫{name},里面{content}'.format(**table))
有一本网络小说叫斗破穹苍,里面主角叫王林
with open('testfile.txt','w') as f:
    f.write(' '.join([str(x) for x in range(10)]))
f.closed
True
with open('testfile.txt','r+') as f:
    #.join([str(x) for x in range(50)]))
    for line in f:
        print(line)
        
0 1 2 3 4 5 6 7 8 90

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49
import json
json.dumps({'a':11,'b':22})
json.dumps(list(range(10)))
with open('test.json','w') as f:
    json.dump(list(range(10)),f)
with open("test.json",'r+') as f:
    x=json.load(f)
    print(type(x))
    print(x)
<class 'list'>
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

13. 错误和异常

语法错误又称解析错误,SyntaxError

异常的定义

try:
    ……
except ValueError:
    ……
else:
    ……
finally:
    ……

14. 类

class MyClass:
    '''一个简单的类示例'''
    i=12344
    def f(self):
        print('Hello world!')

实例化一个类x=MyClass()

x=MyClass()
print(x.i)
x.f()
MyClass.f(x)
MyClass.i
12344
Hello world!
Hello world!





12344
class Dog:
    def __init__(self,name):
        self.name=name
        self.tricks=[]
    def add_trick(self,trick):
        self.tricks.append(trick)

d=Dog('Fido')
b=Dog('Buddy')
d.add_trick('roll over')
d.add_trick('play dead')
b.add_trick('sit down')
b.add_trick('stand up')
print(d.name,':',d.tricks)
print(b.name,':',b.tricks)
Fido : ['roll over', 'play dead']
Buddy : ['sit down', 'stand up']

继承BasicClassName如下:

class DerivedClassName(BaseClassName):
    <statement-1>
    .
    .
    .
    <statement-N>

多重继承,搜索从父类所继承属性的操作是深度优先、从左至右

class DerivedClassName(Base1, Base2, Base3):
    <statement-1>
    .
    .
    .
    <statement-N>
sum(i*i for i in range(10))
285
xvec=[12,33,44]
yvec=[22,45,66]
sum(x*y for x,y in zip(xvec,yvec))
4653

15. 标准库简介

import os  #引入操作系统标准库
print(os.getcwd()) #获得当前目录 get current work directory
os.chdir('/tmp') #改变目录,change directory\
print(os.getcwd())
os.system('mkdir today') #执行bash命令,创建目录today
/tmp
/tmp





0
import shutil
help(shutil.disk_usage)
shutil.disk_usage('./').__str__()

Help on function disk_usage in module shutil:

disk_usage(path)
    Return disk usage statistics about the given path.
    
    Returned value is a named tuple with attributes 'total', 'used' and
    'free', which are the amount of total, used and free space, in bytes.






'usage(total=52710469632, used=17886564352, free=32122773504)'
import glob
glob.glob('*')
['yarn--1576810275174-0.8354554860172894',
 'npm-110-c2d5d7be',
 'yarn--1578639820449-0.5813139807701349',
 'yarn--1576810267365-0.2478749365966999',
 'npm-99-51ee8f19',
 'npm-147-cf3e6ac3',
 'yarn--1578639825186-0.8494421884788006',
 'yarn--1576810273783-0.25396909895879394',
 'npm-898-bf8e11f7',
 'npm-113-22f13a49',
 'today',
 'core-js-banners',
 'yarn--1578639823774-0.4404055612834037',
 'npm-920-ff00b02e',
 'npm-70-b95c8bda',
 'yarn--1576810270517-0.8036830145487115',
 'v8-compile-cache-1000',
 'hsperfdata_jovyan',
 'mesos.key',
 'hsperfdata_root',
 'npm-3586-a7e79998',
 'yarn--1575292958869-0.4408930172539627',
 'yarn--1575292968863-0.7928554015356517',
 'npm-3543-f0fd0242',
 'yarn--1575292949132-0.5060725018560768']
import sys
print(sys.argv)
os.chdir('/home/jovyan/AI-Learning-Note/program')
['/opt/conda/lib/python3.7/site-packages/ipykernel_launcher.py', '-f', '/home/jovyan/.local/share/jupyter/runtime/kernel-9cc55834-07e8-4206-b1c3-8b76c5ef823b.json']
import re
re.findall(r'\bf[a-z]*', 'which foot or hand fell fastest')
['foot', 'fell', 'fastest']
import math
math.cos(math.pi/3)
math.log(1024,2)
10.0
import random
random.choice(['lily','bob','kite'])
'kite'
from urllib.request import urlopen
with urlopen('http://www.time.ac.cn/stime.asp') as response:
    for line in response:
        line = line.decode('gbk')  # Decoding the binary data to text.
        #print(line)
        if '北京' in line:  # look for Eastern Time
            print(line)
        <td><font size="2">北京,重庆,广州,上海,香港,乌鲁木齐,台北,新加坡,佩思</font></td>
from datetime import date
now=date.today()
now.strftime("%Y-%m-%d")
birthday=date(1983,12,20)
age=now-birthday
print(age.days)
13175
import zlib
s=b'witch which has which witches wrist watch'
print(len(s))
t=zlib.compress(s)
print(len(t),':',t)
#zlib.decompress(t)
zlib.crc32(s)
41
37 : b'x\x9c+\xcf,I\xceP(\xcf\xc8\x04\x92\x19\x89\xc5PV9H4\x15\xc8+\xca,.Q(O\x04\xf2\x00D?\x0f\x89'





226805979
def average(values):
    """Computes the arithmetic mean of a list of numbers.

    >>> print(average([20, 30, 70]))
    40.0
    """
    return sum(values) / len(values)

import doctest
doctest.testmod()   # automatically validate the embedded tests
TestResults(failed=0, attempted=1)
from string import Template
t = Template('${village}folk send $$10 to $cause.')
t.substitute(village='Nottingham', cause='the ditch fund')
'Nottinghamfolk send $10 to the ditch fund.'
import time, os.path
photofiles = ['img_1074.jpg', 'img_1076.jpg', 'img_1077.jpg']
class BatchRename(Template):
    delimiter = '%'
fmt = input('Enter rename style (%d-date %n-seqnum %f-format):  ')


t = BatchRename(fmt)
date = time.strftime('%d%b%y')
for i, filename in enumerate(photofiles):
    base, ext = os.path.splitext(filename)
    newname = t.substitute(d=date, n=i, f=ext)
    print('{0} --> {1}'.format(filename, newname))

Enter rename style (%d-date %n-seqnum %f-format):   heyl_%n%f


img_1074.jpg --> heyl_0.jpg
img_1076.jpg --> heyl_1.jpg
img_1077.jpg --> heyl_2.jpg
import logging
logging.debug('Debugging information')
logging.info('Informational message')
logging.warning('Warning:config file %s not found', 'server.conf')
logging.error('Error occurred')
logging.critical('Critical error -- shutting down')
WARNING:root:Warning:config file server.conf not found
ERROR:root:Error occurred
CRITICAL:root:Critical error -- shutting down