-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path28396.html
827 lines (628 loc) · 47.1 KB
/
28396.html
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
<!DOCTYPE html>
<html lang=zh>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui">
<meta name="renderer" content="webkit">
<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="format-detection" content="telephone=no,email=no,adress=no">
<!-- Color theme for statusbar -->
<meta name="theme-color" content="#000000" />
<!-- 强制页面在当前窗口以独立页面显示,防止别人在框架里调用页面 -->
<meta http-equiv="window-target" content="_top" />
<title>Python入门笔记(二) | Paneer's blog</title>
<meta name="description" content="人生苦短 我用Python">
<meta property="og:type" content="article">
<meta property="og:title" content="Python入门笔记(二)">
<meta property="og:url" content="http://paneersblog.github.io/28396.html">
<meta property="og:site_name" content="Panner&Emily’s paradise!">
<meta property="og:description" content="人生苦短 我用Python">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-02-07T15:31:37.000Z">
<meta property="article:modified_time" content="2022-07-16T09:28:48.139Z">
<meta property="article:author" content="Paneer & Emily">
<meta property="article:tag" content="Python">
<meta name="twitter:card" content="summary">
<!-- Canonical links -->
<link rel="canonical" href="http://paneersblog.github.io/28396.html">
<link rel="icon" href="/favicon.png" type="image/x-icon">
<link rel="stylesheet" href="/css/style.css">
<link href="//cdn.jsdelivr.net/npm/@fancyapps/[email protected]/dist/jquery.fancybox.min.css" rel="stylesheet">
<meta name="generator" content="Hexo 6.2.0"><link rel="alternate" href="/atom.xml" title="Panner&Emily’s paradise!" type="application/atom+xml">
</head>
<body class="main-center" itemscope itemtype="http://schema.org/WebPage">
<header class="header" itemscope itemtype="http://schema.org/WPHeader">
<div class="slimContent">
<div class="navbar-header">
<div class="profile-block text-center">
<a id="avatar" href="https://github.com/paneersblog" target="_blank">
<img class="img-circle img-rotate" src="/images/IMG_4556.JPG" width="200" height="200">
</a>
<h2 id="name" class="hidden-xs hidden-sm">Paneer</h2>
<h3 id="title" class="hidden-xs hidden-sm hidden-md">一个成熟稳重的沙雕网友</h3>
<small id="location" class="text-muted hidden-xs hidden-sm"><i class="icon icon-map-marker"></i> Shandong, China</small>
</div>
<div class="search" id="search-form-wrap">
<form class="search-form sidebar-form">
<div class="input-group">
<input type="text" class="search-form-input form-control" placeholder="搜索" />
<span class="input-group-btn">
<button type="submit" class="search-form-submit btn btn-flat" onclick="return false;"><i class="icon icon-search"></i></button>
</span>
</div>
</form>
<div class="ins-search">
<div class="ins-search-mask"></div>
<div class="ins-search-container">
<div class="ins-input-wrapper">
<input type="text" class="ins-search-input" placeholder="想要查找什么..." x-webkit-speech />
<button type="button" class="close ins-close ins-selectable" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
</div>
<div class="ins-section-wrapper">
<div class="ins-section-container"></div>
</div>
</div>
</div>
</div>
<button class="navbar-toggle collapsed" type="button" data-toggle="collapse" data-target="#main-navbar" aria-controls="main-navbar" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<nav id="main-navbar" class="collapse navbar-collapse" itemscope itemtype="http://schema.org/SiteNavigationElement" role="navigation">
<ul class="nav navbar-nav main-nav ">
<li class="menu-item menu-item-home">
<a href="/.">
<i class="icon icon-home-fill"></i>
<span class="menu-title">首页</span>
</a>
</li>
<li class="menu-item menu-item-archives">
<a href="/archives">
<i class="icon icon-archives-fill"></i>
<span class="menu-title">归档</span>
</a>
</li>
<li class="menu-item menu-item-categories">
<a href="/categories">
<i class="icon icon-folder"></i>
<span class="menu-title">分类</span>
</a>
</li>
<li class="menu-item menu-item-tags">
<a href="/tags">
<i class="icon icon-tags"></i>
<span class="menu-title">标签</span>
</a>
</li>
<li class="menu-item menu-item-repository">
<a href="/repository">
<i class="icon icon-project"></i>
<span class="menu-title">项目</span>
</a>
</li>
<li class="menu-item menu-item-links">
<a href="/links">
<i class="icon icon-friendship"></i>
<span class="menu-title">友链</span>
</a>
</li>
<li class="menu-item menu-item-about">
<a href="/about">
<i class="icon icon-cup-fill"></i>
<span class="menu-title">关于</span>
</a>
</li>
<li class="menu-item menu-item-播放列表">
<a href="/playlist">
<i class="icon icon-book-fill"></i>
<span class="menu-title">menu.播放列表</span>
</a>
</li>
</ul>
<ul class="social-links">
<li><a href="https://github.com/paneersblog" target="_blank" title="Github" data-toggle=tooltip data-placement=top><i class="icon icon-github"></i></a></li>
<li><a href="https://weibo.com/u/6203293798" target="_blank" title="Weibo" data-toggle=tooltip data-placement=top><i class="icon icon-weibo"></i></a></li>
<li><a href="https://twitter.com/Paneer_007" target="_blank" title="Twitter" data-toggle=tooltip data-placement=top><i class="icon icon-twitter"></i></a></li>
</ul>
</nav>
</div>
</header>
<aside class="sidebar" itemscope itemtype="http://schema.org/WPSideBar">
<div class="slimContent">
<div class="widget">
<h3 class="widget-title">公告</h3>
<div class="widget-body">
<div id="board">
<div class="content">
<p>欢迎交流与分享经验!</p>
</div>
</div>
</div>
</div>
<div class="widget">
<h3 class="widget-title">分类</h3>
<div class="widget-body">
<ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/%E6%94%BB%E7%95%A5/">攻略</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E6%B5%8B%E8%AF%95%E9%A1%B5/">测试页</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/">编程语言</a><span class="category-list-count">16</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E8%8B%B1%E8%AF%AD%E5%AD%A6%E4%B9%A0/">英语学习</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E8%BD%AF%E4%BB%B6%E5%AD%A6%E4%B9%A0/">软件学习</a><span class="category-list-count">5</span></li></ul>
</div>
</div>
<div class="widget">
<h3 class="widget-title">标签</h3>
<div class="widget-body">
<ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/tags/DOS/" rel="tag">DOS</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Linux/" rel="tag">Linux</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/MCNP5/" rel="tag">MCNP5</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Markdown/" rel="tag">Markdown</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Matlab/" rel="tag">Matlab</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Photo/" rel="tag">Photo</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Python/" rel="tag">Python</a><span class="tag-list-count">12</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/SRIM/" rel="tag">SRIM</a><span class="tag-list-count">4</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Trip/" rel="tag">Trip</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/WordList-E/" rel="tag">WordList_E</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/WordList-P/" rel="tag">WordList_P</a><span class="tag-list-count">1</span></li></ul>
</div>
</div>
<div class="widget">
<h3 class="widget-title">标签云</h3>
<div class="widget-body tagcloud">
<a href="/tags/DOS/" style="font-size: 13.33px;">DOS</a> <a href="/tags/Linux/" style="font-size: 13px;">Linux</a> <a href="/tags/MCNP5/" style="font-size: 13px;">MCNP5</a> <a href="/tags/Markdown/" style="font-size: 13px;">Markdown</a> <a href="/tags/Matlab/" style="font-size: 13px;">Matlab</a> <a href="/tags/Photo/" style="font-size: 13px;">Photo</a> <a href="/tags/Python/" style="font-size: 14px;">Python</a> <a href="/tags/SRIM/" style="font-size: 13.67px;">SRIM</a> <a href="/tags/Trip/" style="font-size: 13.33px;">Trip</a> <a href="/tags/WordList-E/" style="font-size: 13px;">WordList_E</a> <a href="/tags/WordList-P/" style="font-size: 13px;">WordList_P</a>
</div>
</div>
<div class="widget">
<h3 class="widget-title">归档</h3>
<div class="widget-body">
<ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/08/">八月 2022</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/07/">七月 2022</a><span class="archive-list-count">5</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/04/">四月 2022</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/02/">二月 2022</a><span class="archive-list-count">18</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/01/">一月 2022</a><span class="archive-list-count">4</span></li></ul>
</div>
</div>
<div class="widget">
<h3 class="widget-title">最新文章</h3>
<div class="widget-body">
<ul class="recent-post-list list-unstyled no-thumbnail">
<li>
<div class="item-inner">
<p class="item-category">
<a class="category-link" href="/categories/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/">编程语言</a>
</p>
<p class="item-title">
<a href="/41781.html" class="title">Python入门笔记(11)</a>
</p>
<p class="item-date">
<time datetime="2022-08-04T12:31:21.000Z" itemprop="datePublished">2022-08-04</time>
</p>
</div>
</li>
<li>
<div class="item-inner">
<p class="item-category">
<a class="category-link" href="/categories/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/">编程语言</a>
</p>
<p class="item-title">
<a href="/34793.html" class="title">Pyhton入门笔记(十)</a>
</p>
<p class="item-date">
<time datetime="2022-08-03T07:08:20.000Z" itemprop="datePublished">2022-08-03</time>
</p>
</div>
</li>
<li>
<div class="item-inner">
<p class="item-category">
<a class="category-link" href="/categories/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/">编程语言</a>
</p>
<p class="item-title">
<a href="/21165.html" class="title">Python入门笔记(九)</a>
</p>
<p class="item-date">
<time datetime="2022-07-23T13:30:18.000Z" itemprop="datePublished">2022-07-23</time>
</p>
</div>
</li>
<li>
<div class="item-inner">
<p class="item-category">
<a class="category-link" href="/categories/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/">编程语言</a>
</p>
<p class="item-title">
<a href="/3827.html" class="title">Python入门笔记(八)</a>
</p>
<p class="item-date">
<time datetime="2022-07-23T08:43:13.000Z" itemprop="datePublished">2022-07-23</time>
</p>
</div>
</li>
<li>
<div class="item-inner">
<p class="item-category">
<a class="category-link" href="/categories/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/">编程语言</a>
</p>
<p class="item-title">
<a href="/47766.html" class="title">Python入门笔记(七)</a>
</p>
<p class="item-date">
<time datetime="2022-07-22T06:20:09.000Z" itemprop="datePublished">2022-07-22</time>
</p>
</div>
</li>
</ul>
</div>
</div>
</div>
</aside>
<main class="main" role="main">
<div class="content">
<article id="post-Python入门笔记(二)" class="article article-type-post" itemscope itemtype="http://schema.org/BlogPosting">
<div class="article-header">
<h1 class="article-title" itemprop="name">
Python入门笔记(二)
</h1>
<div class="article-meta">
<span class="article-date">
<i class="icon icon-calendar-check"></i>
<a href="/28396.html" class="article-date">
<time datetime="2022-02-07T15:31:37.000Z" itemprop="datePublished">2022-02-07</time>
</a>
</span>
<span class="article-category">
<i class="icon icon-folder"></i>
<a class="article-category-link" href="/categories/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/">编程语言</a>
</span>
<span class="article-tag">
<i class="icon icon-tags"></i>
<a class="article-tag-link-link" href="/tags/Python/" rel="tag">Python</a>
</span>
<span class="post-comment"><i class="icon icon-comment"></i> <a href="/28396.html#comments" class="article-comment-link">评论</a></span>
<span class="post-wordcount hidden-xs" itemprop="wordCount">字数统计: 2.5k(字)</span>
<span class="post-readcount hidden-xs" itemprop="timeRequired">阅读时长: 9(分)</span>
</div>
</div>
<div class="article-entry marked-body" itemprop="articleBody">
<p>人生苦短 我用Python</p>
<span id="more"></span>
<hr>
<h2 id="Python中的注释"><a href="#Python中的注释" class="headerlink" title="Python中的注释"></a>Python中的注释</h2><p>什么是注释?</p>
<ul>
<li>是指程序员在代码中对代码功能解释说明的文字,可以提高代码的可读性</li>
<li>注释的内容将被Python解释器忽略,不被计算机执行</li>
</ul>
<p>注释的分类?</p>
<ul>
<li>单行注释<ul>
<li>以“#”作为单行注释的符号,作用范围从“#”开始直到换行为止</li>
</ul>
</li>
<li>多行注释<ul>
<li>Python中并没有单独的多行注释标记,将包含在一对三引号[‘’’…’’’]OR[“””…”””]之间的代码称为多行注释</li>
</ul>
</li>
<li>中文注释声明<ul>
<li># coding:utf-8 或 #coding=utf-8 或 #-*-coding:utf-8-*-</li>
</ul>
</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 10.单行注释</span></span><br><span class="line"><span class="comment"># 要求从键盘去输入出生年份,举例:1998</span></span><br><span class="line">year = <span class="built_in">input</span>(<span class="string">'请输入您的出生年份:'</span>)</span><br><span class="line"></span><br><span class="line">year = <span class="built_in">input</span>(<span class="string">'请输入您的出生年份:'</span>) <span class="comment"># 要求从键盘去输入出生年份,举例:1998</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 11.多行注释</span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="string">参考视频:马士兵教育</span></span><br><span class="line"><span class="string">分节名:11.多行注释</span></span><br><span class="line"><span class="string">创建人:Paneer</span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="comment"># -*- OR -*- #</span></span><br><span class="line"><span class="string">"""</span></span><br><span class="line"><span class="string">参考视频:马士兵教育</span></span><br><span class="line"><span class="string">分节名:11.多行注释</span></span><br><span class="line"><span class="string">创建人:Paneer</span></span><br><span class="line"><span class="string">"""</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 12.中文声明注释</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 在Python2.x中,使用中文注释时必须要加上中文声明注释。在Python3.x中可加可不加。</span></span><br><span class="line"><span class="comment"># coding:utf-8</span></span><br><span class="line"><span class="comment"># 此声明若加的话务必加在代码首行。</span></span><br><span class="line"><span class="comment"># 写好注释至关重要哦~</span></span><br></pre></td></tr></table></figure>
<h2 id="代码缩进"><a href="#代码缩进" class="headerlink" title="代码缩进"></a>代码缩进</h2><p>Python语言采用严格的“缩进”来表示程序逻辑</p>
<ul>
<li>缩进是指每行语句开始前的空白区域,用来表示Python程序间的包含和层次关系</li>
<li>一般代码不需要缩进,顶行编写且不留空白</li>
<li>类定义、函数定义、流程控制语句,以及异常处理语句等,行尾的冒号和下一行的缩进表示一个代码块的开始,而缩进结束则表示一个代码块的结束</li>
<li>缩进可以使用空格或Tab键来实现,通常情况下采用四个空格作为一个缩进量</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 13.代码缩进</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">'hello'</span>)</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'hello'</span>) <span class="comment">#此情况下会报错:IndentationError: unexpected indent,解决办法是删掉第二个print前的空格</span></span><br><span class="line"> </span><br><span class="line"><span class="comment"># 类的定义</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Student</span>:</span><br><span class="line"> <span class="keyword">pass</span></span><br><span class="line"><span class="comment"># 函数定义</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">fun</span>():</span><br><span class="line"> <span class="keyword">pass</span></span><br></pre></td></tr></table></figure>
<h2 id="在Python交互式模式中运行-py文件"><a href="#在Python交互式模式中运行-py文件" class="headerlink" title="在Python交互式模式中运行*.py文件"></a>在Python交互式模式中运行*.py文件</h2><p>首先确保电脑已经安装好python且配置好了环境变量,其语法格式为:</p>
<figure class="highlight dos"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">python 完整文件名(包括路径)</span><br></pre></td></tr></table></figure>
<h2 id="数据类型和基本运算符"><a href="#数据类型和基本运算符" class="headerlink" title="数据类型和基本运算符"></a>数据类型和基本运算符</h2><h3 id="保留字与标识符"><a href="#保留字与标识符" class="headerlink" title="保留字与标识符"></a>保留字与标识符</h3><p>保留字:<br>保留字是Python已经被赋予特定意义的一些单词,在开发程序时,不可以把这些保留字作为变量、函数、类、模块和其他对象的名称来使用<br>保留字表如下所示:</p>
<table>
<thead>
<tr>
<th align="center"></th>
<th align="center"></th>
<th align="center"></th>
<th align="center"></th>
<th align="center"></th>
<th align="center"></th>
<th align="center"></th>
<th align="center"></th>
<th align="center"></th>
<th align="center"></th>
<th align="center"></th>
</tr>
</thead>
<tbody><tr>
<td align="center">and</td>
<td align="center">as</td>
<td align="center">assert</td>
<td align="center">break</td>
<td align="center">class</td>
<td align="center">continue</td>
<td align="center">def</td>
<td align="center">del</td>
<td align="center">elif</td>
<td align="center">else</td>
<td align="center">except</td>
</tr>
<tr>
<td align="center">finally</td>
<td align="center">for</td>
<td align="center">from</td>
<td align="center">False</td>
<td align="center">global</td>
<td align="center">if</td>
<td align="center">import</td>
<td align="center">in</td>
<td align="center">is</td>
<td align="center">lambda</td>
<td align="center">nonlocal</td>
</tr>
<tr>
<td align="center">not</td>
<td align="center">None</td>
<td align="center">or</td>
<td align="center">pass</td>
<td align="center">raise</td>
<td align="center">return</td>
<td align="center">try</td>
<td align="center">True</td>
<td align="center">while</td>
<td align="center">with</td>
<td align="center">yield</td>
</tr>
</tbody></table>
<blockquote>
<p>注:保留字严格区分大小写,只有<strong>True</strong>、<strong>False</strong>、<strong>None</strong>为首字母大写。</p>
</blockquote>
<p>保留字查看</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 保留字的查看</span></span><br><span class="line"><span class="keyword">import</span> keyword</span><br><span class="line"><span class="built_in">print</span>(keyword.kwlist)</span><br></pre></td></tr></table></figure>
<p>标识符:<br>标识符可以简单的理解为是一个名字,它主要用来标识变量、函数、类、模块和其他对象的名称。 </p>
<blockquote>
<p>Python标识符的命名规则:</p>
<ul>
<li>可以是字母、下划线“_”和数字,并且第一个字符不能是数字</li>
<li>不能使用Python中的保留字</li>
<li>标识符严格区分大小写</li>
<li>以下划线开头的标识符有特殊意义,一般应避免使用相似的标识符</li>
<li>允许使用中文作为标识符,但不建议</li>
</ul>
</blockquote>
<p>标识符的命名规范:</p>
<ul>
<li>模块名尽量短小,并且尽量全部使用小写字母,可以使用下划线分隔多个字母。如 grame_main</li>
<li>包名尽量短小,并且尽量全部使用小写字母,不推荐使用下划线。如 com.paneer,不推荐com_paneer</li>
<li>类名采用单词首字母大写形式(Pascal风格)。如 MyClass</li>
<li>模块内部的类采用“_”+Pascal风格的类名组成。如在MyClass中的内部类 _InnerMyClass</li>
<li>函数、类的属性和方法的命名,全部使用小写字母,多个字母之间使用下划线分隔</li>
<li>常量命名时采用全部大写字母,可以使用下划线</li>
<li>使用单下划线“_”开头的模块变量或函数是受保护的,在使用<code>from xxx import *</code>语句从模块中导入时,这些模块变量或函数是不能被导入的</li>
<li>使用双下划线“__“开头的实例变量或方法是类私有的</li>
<li>以双下划线开头和结尾的是Python的专用标识。如 <code>__init__()</code> 表示初始化函数</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 2.标识符的规范</span></span><br><span class="line"></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="string">每一个*.py文件都是一个模块,模块命名应尽量使用小写字母并使用下划线分隔</span></span><br><span class="line"><span class="string">这些*.py文件所在的文件夹称作包,包命名应尽量使用小写字母不推荐使用下划线</span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">MyClass</span>: <span class="comment"># 新建一个名为MyClass的类,采用Pascal风格</span></span><br><span class="line"> <span class="keyword">class</span> <span class="title class_">_InnerMyClass</span>: <span class="comment"># 新建一个名为 _InnerMyClass 的内部类</span></span><br><span class="line"> <span class="keyword">pass</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 函数 fun & fun_name 的定义</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">fun</span>():</span><br><span class="line"> <span class="keyword">pass</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">fun_name</span>():</span><br><span class="line"> <span class="keyword">pass</span></span><br></pre></td></tr></table></figure>
<h2 id="Python中的变量"><a href="#Python中的变量" class="headerlink" title="Python中的变量"></a>Python中的变量</h2><p>变量是指保存和表示数值的一种语法元素,可以简单的理解为“名字”。<br>变量的值是可以改变的,可以通过赋值方式被修改。</p>
<blockquote>
<p>例如,<code>word='Paneer真帅'</code>即是将“Paneer真帅”赋值给了变量“word”</p>
<p>可以理解为去菜鸟驿站取快递,我只需要取件码就能找到自己的快递,此时我的取件码就是一个变量,它代表的那个位置上的快递是我的,因为是菜鸟驿站的工作人员给它赋值了。</p>
</blockquote>
<h3 id="变量的定义"><a href="#变量的定义" class="headerlink" title="变量的定义"></a>变量的定义</h3><p>其语法结构为:</p>
<pre><code>变量名=value
</code></pre>
<h3 id="变量的使用"><a href="#变量的使用" class="headerlink" title="变量的使用"></a>变量的使用</h3><p>Python是一种动态类型的语言,变量的类型可以随时变化</p>
<ul>
<li>使用内置函数<code>type()</code>可以查看变量的数据类型</li>
</ul>
<p>Python中允许多个变量指向同一个值</p>
<ul>
<li>使用内置函数<code>id()</code>可以返回变量所指的内存地址</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 3.变量的定义和使用</span></span><br><span class="line">luck_number=<span class="number">8</span> <span class="comment"># 创建一个整型变量luck_number,赋值为8</span></span><br><span class="line">my_name=<span class="string">'Paneer'</span> <span class="comment"># 创建一个字符串类型变量</span></span><br><span class="line"><span class="built_in">print</span>(my_name,<span class="string">'的幸运数字是:'</span>,luck_number)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">'luck_number的数据类型是:'</span>,<span class="built_in">type</span>(luck_number))</span><br><span class="line"></span><br><span class="line"><span class="comment"># 变量的值可以更改</span></span><br><span class="line">luck_number=<span class="string">'Emily真圆'</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">'luck_number的数据类型是:'</span>,<span class="built_in">type</span>(luck_number)) <span class="comment"># 此时其数据类型就是 str型(字符串型)了</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Python允许多个变量指向同一个值</span></span><br><span class="line">no=number=<span class="number">502</span> <span class="comment"># 给no 和number 都赋值为502</span></span><br><span class="line"><span class="built_in">print</span>(no,number)</span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">id</span>(no)) <span class="comment"># 输出no 所指向的内存地址</span></span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">id</span>(number)) <span class="comment">#输出number 所指向的内存地址,会发现与no 的相同</span></span><br></pre></td></tr></table></figure>
<p>变量命名应遵循以下几条规则</p>
<blockquote>
<ul>
<li>变量名必须是一个有效的标识符</li>
<li>变量名不能使用Python中的保留字</li>
<li>慎用小写字母l和大写字母O</li>
<li>应选择有意义的单词作为变量名</li>
</ul>
</blockquote>
<p>同样的,常量也应遵循以下规则</p>
<blockquote>
<ul>
<li>常量就是在程序运行过程中值不能改变的量。如PI</li>
<li>Python中没有定义常量的保留字</li>
<li>常量规定使用大写字母和下划线组成</li>
<li>常量首次赋值之后,还是可以被其他代码修改的</li>
</ul>
</blockquote>
<h2 id="基本数据类型"><a href="#基本数据类型" class="headerlink" title="基本数据类型"></a>基本数据类型</h2><h3 id="数值类型"><a href="#数值类型" class="headerlink" title="数值类型"></a>数值类型</h3><h4 id="整数类型特点"><a href="#整数类型特点" class="headerlink" title="整数类型特点"></a>整数类型特点</h4><ul>
<li>表示整数数值,即没有小数部分的数值</li>
<li>正整数、负数和0</li>
<li>理论取值范围为(负无穷,正无穷)</li>
<li>整数的表示类型</li>
<li>不可变的数据类型</li>
</ul>
<table>
<thead>
<tr>
<th align="center">进制种类</th>
<th align="center">引导符号</th>
<th align="center">概述</th>
</tr>
</thead>
<tbody><tr>
<td align="center">十进制</td>
<td align="center">无</td>
<td align="center">默认情况,如 365,257</td>
</tr>
<tr>
<td align="center">二进制</td>
<td align="center">0b或0B</td>
<td align="center">由0,1组成,如 0b101,0B1010</td>
</tr>
<tr>
<td align="center">八进制</td>
<td align="center">0o或0O</td>
<td align="center">由0-7组成,如 0o367,0O666</td>
</tr>
<tr>
<td align="center">十六进制</td>
<td align="center">0x或0X</td>
<td align="center">由0-9,a(A)-f(F)组成,如 0x98A,0X89F</td>
</tr>
</tbody></table>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 4.整数类型</span></span><br><span class="line">num=<span class="number">987</span> <span class="comment"># 默认十进制</span></span><br><span class="line">num1=<span class="number">0b10100101</span> <span class="comment"># 使用二进制表示整数</span></span><br><span class="line">num2=<span class="number">0o765</span> <span class="comment"># 使用八进制表示整数</span></span><br><span class="line">num3=<span class="number">0x98AF</span> <span class="comment"># 使用十六进制表示整数</span></span><br><span class="line"><span class="built_in">print</span>(num) <span class="comment"># 输出全部为十进制</span></span><br><span class="line"><span class="built_in">print</span>(num1) <span class="comment"># 输出全部为十进制</span></span><br><span class="line"><span class="built_in">print</span>(num2) <span class="comment"># 输出全部为十进制</span></span><br><span class="line"><span class="built_in">print</span>(num3) <span class="comment"># 输出全部为十进制</span></span><br></pre></td></tr></table></figure>
<h4 id="浮点数类型"><a href="#浮点数类型" class="headerlink" title="浮点数类型"></a>浮点数类型</h4><ul>
<li>表示带有小数点的数</li>
<li>浮点数由整数部分和小数部分组成</li>
<li>Python中浮点数类型必须要有小数部分,小数部分可以是0</li>
<li>浮点数可以使用科学计数法表示</li>
<li>两个浮点数相加,有一定概率运算结果后增加一些“不确定的”尾数</li>
<li>使用内置函数<code>round()</code>可以限定运算结果需要保留的小数位数</li>
<li>不可变数据类型</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 5.浮点数类型</span></span><br><span class="line">height=<span class="number">180.1</span></span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">type</span>(height)) <span class="comment"># 可以得出height 的数据类型为float 型</span></span><br><span class="line">x=<span class="number">10</span></span><br><span class="line">y=<span class="number">10.0</span></span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">type</span>(x)) <span class="comment"># x为int 型</span></span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">type</span>(y)) <span class="comment"># y为float 型</span></span><br><span class="line"><span class="comment"># 浮点数相加尾数不确定问题</span></span><br><span class="line"><span class="built_in">print</span>(<span class="number">0.1</span>+<span class="number">0.2</span>) <span class="comment"># 得结果为 0.30000000000000004,有一不确定尾数</span></span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">round</span>(<span class="number">0.1</span>+<span class="number">0.2</span>,<span class="number">1</span>)) <span class="comment"># 保留一位小数</span></span><br></pre></td></tr></table></figure>
<h4 id="复数类型"><a href="#复数类型" class="headerlink" title="复数类型"></a>复数类型</h4><ul>
<li>Python中复数与数学中的复数形式完全一致</li>
<li>复数由实部和虚部组成</li>
<li>j是复数的一个基本单位,被定义为 j=$\sqrt{-1}$ ,又称虚数单位</li>
<li><code>.real</code>获取实数部分,<code>.imag</code>获取虚数部分</li>
<li>不可变数据类型</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 6.复数类型</span></span><br><span class="line">z=<span class="number">123</span>+<span class="number">456j</span> <span class="comment"># 定义复数类型 complex型</span></span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">type</span>(z))</span><br><span class="line"><span class="built_in">print</span>(<span class="string">'实数部分:'</span>,z.real) <span class="comment"># 输出实数部分</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">'虚数部分:'</span>,z.imag) <span class="comment"># 输出虚数部分</span></span><br></pre></td></tr></table></figure>
<h4 id="字符串类型"><a href="#字符串类型" class="headerlink" title="字符串类型"></a>字符串类型</h4><ul>
<li>字符串就是连续的字符序列,,可以表示计算机所能识别的一切字符</li>
<li>不可变数据类型,,即不可变字符序列</li>
<li>单行字符串使用’…’或”…”</li>
<li>多行字符串使用’’’…’’’或”””…”””</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 7.字符串类型</span></span><br><span class="line"><span class="comment"># 单行字符串</span></span><br><span class="line">city=<span class="string">'菏泽'</span></span><br><span class="line">address=<span class="string">"山东省菏泽市"</span></span><br><span class="line"><span class="built_in">print</span>(city)</span><br><span class="line"><span class="built_in">print</span>(address)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 多行字符串</span></span><br><span class="line">info=<span class="string">'''</span></span><br><span class="line"><span class="string">地址:山东省菏泽市</span></span><br><span class="line"><span class="string">收件人:Paneer</span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="built_in">print</span>(info)</span><br></pre></td></tr></table></figure>
</div>
<div class="article-footer">
<blockquote class="mt-2x">
<ul class="post-copyright list-unstyled">
<li class="post-copyright-link hidden-xs">
<strong>本文链接:</strong>
<a href="http://paneersblog.github.io/28396.html" title="Python入门笔记(二)" target="_blank" rel="external">http://paneersblog.github.io/28396.html</a>
</li>
<li class="post-copyright-license">
<strong>版权声明: </strong> 本博客所有文章除特别声明外,均采用 <a href="http://creativecommons.org/licenses/by/4.0/deed.zh" target="_blank" rel="external">CC BY 4.0 CN协议</a> 许可协议。转载请注明出处!
</li>
</ul>
</blockquote>
<div class="panel panel-default panel-badger">
<div class="panel-body">
<figure class="media">
<div class="media-left">
<a href="https://github.com/paneersblog" target="_blank" class="img-burn thumb-sm visible-lg">
<img src="/images/IMG_4556.JPG" class="img-rounded w-full" alt="">
</a>
</div>
<div class="media-body">
<h3 class="media-heading"><a href="https://github.com/paneersblog" target="_blank"><span class="text-dark">Paneer</span><small class="ml-1x">一个成熟稳重的沙雕网友</small></a></h3>
<div>搬砖永无止境</div>
</div>
</figure>
</div>
</div>
</div>
</article>
<section id="comments">
<div id="vcomments"></div>
</section>
</div>
<nav class="bar bar-footer clearfix" data-stick-bottom>
<div class="bar-inner">
<ul class="pager pull-left">
<li class="prev">
<a href="/1.html" title="test page (music)"><i class="icon icon-angle-left" aria-hidden="true"></i><span> 上一篇</span></a>
</li>
<li class="next">
<a href="/25723.html" title="专业词汇表(一)"><span>下一篇 </span><i class="icon icon-angle-right" aria-hidden="true"></i></a>
</li>
</ul>
<div class="bar-right">
<div class="share-component" data-sites="weibo,qq,wechat,facebook,twitter" data-mobile-sites="weibo,qq,qzone"></div>
</div>
</div>
</nav>
</main>
<footer class="footer" itemscope itemtype="http://schema.org/WPFooter">
<ul class="social-links">
<li><a href="https://github.com/paneersblog" target="_blank" title="Github" data-toggle=tooltip data-placement=top><i class="icon icon-github"></i></a></li>
<li><a href="https://weibo.com/u/6203293798" target="_blank" title="Weibo" data-toggle=tooltip data-placement=top><i class="icon icon-weibo"></i></a></li>
<li><a href="https://twitter.com/Paneer_007" target="_blank" title="Twitter" data-toggle=tooltip data-placement=top><i class="icon icon-twitter"></i></a></li>
</ul>
<div class="copyright">
© 2022 Paneer & Emily
<!-- <div class="publishby">
Theme by <a href="https://github.com/cofess" target="_blank"> cofess </a>base on <a href="https://github.com/cofess/hexo-theme-pure" target="_blank">pure</a>.
</div>-->
</div>
</footer>
<script src="//cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"></script>
<script>
window.jQuery || document.write('<script src="js/jquery.min.js"><\/script>')
</script>
<script src="/js/plugin.min.js"></script>
<script src="/js/application.js"></script>
<script>
(function (window) {
var INSIGHT_CONFIG = {
TRANSLATION: {
POSTS: '文章',
PAGES: '页面',
CATEGORIES: '分类',
TAGS: '标签',
UNTITLED: '(未命名)',
},
ROOT_URL: '/',
CONTENT_URL: '/content.json',
};
window.INSIGHT_CONFIG = INSIGHT_CONFIG;
})(window);
</script>
<script src="/js/insight.js"></script>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src="//cdn.jsdelivr.net/npm/valine"></script>
<script type="text/javascript">
var GUEST = ['nick', 'mail', 'link'];
var meta = 'nick,mail,link';
meta = meta.split(',').filter(function(item) {
return GUEST.indexOf(item) > -1;
});
new Valine({
el: '#vcomments',
verify: false,
notify: false,
appId: 'IhVNOCA6KMiSCGQQEJHuNj18-gzGzoHsz',
appKey: 'M7KF1RwFGcFcF2RThCWkSCOj',
placeholder: 'Just go go',
avatar: 'mm',
meta: meta,
pageSize: '10' || 10,
visitor: false
});
</script>
<script src="//cdn.jsdelivr.net/npm/@fancyapps/[email protected]/dist/jquery.fancybox.min.js"></script>
<script>
//利用 FancyBox 实现点击图片放大
$(document).ready(function() {
$('article img').not('[hidden]').not('.panel-body img').each(function() {
var $image = $(this);
var imageCaption = $image.attr('alt');
var $imageWrapLink = $image.parent('a');
if ($imageWrapLink.length < 1) {
var src = this.getAttribute('src');
var idx = src.lastIndexOf('?');
if (idx != -1) {
src = src.substring(0, idx);
}
$imageWrapLink = $image.wrap('<a href="' + src + '"></a>').parent('a');
}
$imageWrapLink.attr('data-fancybox', 'images');
if (imageCaption) {
$imageWrapLink.attr('data-caption', imageCaption);
}
});
$().fancybox({
selector: '[data-fancybox="images"]',
hash: false,
loop: false,
});
});
</script>
</body>
</html>