From c77832c59eac1642b7ade95e17e07826f6027d88 Mon Sep 17 00:00:00 2001 From: mikelyou Date: Thu, 2 Jan 2020 15:37:22 -0500 Subject: [PATCH] new posts python-#02~#06 --- ...ae1b45bfddb0ec8884f3a2b764465d6514c096d556 | 52 ++ ...8031ec64d626da27883abdf64420ba2f04f585e396 | 19 + ...576e593e162d41a8d924f4a61426d4befea6b1ee2f | 19 + ...41e9fc3a15409ade35655a3867bcbb91f9536519c2 | 47 ++ ...2ed2ee4a6a32c7a957147d4d90491c4daa42908e84 | 453 ++++++++++++++++++ ...46671ac88b7f42390ec7b078b0016f1f2ec17269d1 | 19 + ...93cd881decb6a63816be594773d40f1b41392dd206 | 11 + ...49aa8ce5c3f820577564a5ee883933776d799c2ddf | 255 ++++++++++ ...27fe043619414c0f9da26dfac1a7892ccbc192be9f | 2 + ...2eeb3ca9519aa41f80941c0fd39d98ff1541fa4b5d | 7 + ...2fd509797eb7c9510c5adccc4783675f49985a93d3 | 453 ++++++++++++++++++ ...b5404a41edd94d1e5b7b2ee107fd65dee39eae85fc | 52 ++ ...b35eea7167c605f38fd7a97afdfd96efaeaf7003d7 | 46 ++ ...fb3135498e5c45b7011aedf57b8c99adf9c3f6e619 | 47 ++ ...4a9c50a544f0db466dea22c678252b8c290857b101 | 11 + ...650e7a6d47c2a7b6a61fe733eeb7378e82f22b205f | 2 + ...706a2683ad834a850c1d20ae89782a930b95ca20a1 | 52 ++ ...19fc774fccceb0c55f3920c7c6c511f199898d5a52 | 2 + ...34d0b3bcc8c34b8afb372fdafdf7ee8d60881efd0d | 47 ++ ...5296537c9c5977d6cf63a584c7c9b2f4ed70bd08ca | 255 ++++++++++ ...1b24b2c25cc0df3f1f583574f8d1ff385f874f4a91 | 2 + ...332c384bc8aec15354278c2523de91bbb25433489d | 255 ++++++++++ ...80f797a164b194f1ea6d5d0b728eef4e159b8f0ff6 | 46 ++ ...135b02a4a72beada863aee13b6a5ba863753e43413 | 19 + ...7690a3605924fde8445269f610cff54fdae532dabf | 453 ++++++++++++++++++ ...1eed13ad789f365fa35a9e8d514c74e2c20770e72f | 255 ++++++++++ ...02b3ed3fe2f891cee97a750218df4a0a097009c552 | 39 ++ ...bd68ccce817763fd4324f08ca600bd434f53653379 | 2 + ...f9910ec19fcef73b718e6398cacfe1fae48c012485 | 150 ++++++ ...f846be8ab063cb4cb250e292ad4ed862dce6812792 | 148 ++++++ ...039d2f5ccaf176d67b96a80968acaf2e072928f96e | 82 ++++ ...b7db62afa0b2d95159590ce066db7e4bf311c7d7ef | 453 ++++++++++++++++++ ...fdc9f1999b53390fe9439960e76f15ac671e41557e | 2 + ...88b30abc83c634e7c0b02117460c32af3b94bc0a9d | 2 + ...2b32bf107ce292bf219e963a88f3e05bf270b1ea4d | 31 ++ ...9f8cb2a46505c910c97b0b874d75aff04d64b6e1ce | 52 ++ ...3959b232c59e98452b42953419695d9f79dccbdb8e | 2 + ...afc8e08505f1e281553b6545c26884e9d4c60f2ce0 | 148 ++++++ ...f140aef2ce103feeee5bb63ec202fa886814c0e2af | 148 ++++++ ...ca992ab938a3857380123ddeba79385ee4cd05808e | 48 ++ .../2020-01-02-python-learning-00-readme.md | 6 +- ...n-learning-01-notes-variables-operators.md | 43 ++ ...on-learning-02-input-output-assignment.md} | 6 +- ...-02-python-learning-03-branch-structure.md | 45 ++ ...01-02-python-learning-04-loop-structure.md | 65 +++ ...1-02-python-learning-05-function-module.md | 52 ++ ...-learning-05-string-list-tuple-dict-set.md | 294 ++++++++++++ 47 files changed, 4694 insertions(+), 5 deletions(-) create mode 100644 .jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/01/96e631519e0009945c6eae1b45bfddb0ec8884f3a2b764465d6514c096d556 create mode 100644 .jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/12/5e67e444a19f2bc590728031ec64d626da27883abdf64420ba2f04f585e396 create mode 100644 .jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/13/2ca5aa787e9848c6f73d576e593e162d41a8d924f4a61426d4befea6b1ee2f create mode 100644 .jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/15/a2e34fd0c6d1c6bb862141e9fc3a15409ade35655a3867bcbb91f9536519c2 create mode 100644 .jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/1e/39c53e2c451469bcb6e52ed2ee4a6a32c7a957147d4d90491c4daa42908e84 create mode 100644 .jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/1f/3f6a391195fdde33c16f46671ac88b7f42390ec7b078b0016f1f2ec17269d1 create mode 100644 .jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/1f/baddb31a101a0959aff793cd881decb6a63816be594773d40f1b41392dd206 create mode 100644 .jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/28/67445b10ea9cd3b1a7c049aa8ce5c3f820577564a5ee883933776d799c2ddf create mode 100644 .jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/28/6bbe62b3352b5eea346227fe043619414c0f9da26dfac1a7892ccbc192be9f create mode 100644 .jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/2f/b7bd2c8e678f471ff6e32eeb3ca9519aa41f80941c0fd39d98ff1541fa4b5d create mode 100644 .jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/2f/be2b0408f82230d564bf2fd509797eb7c9510c5adccc4783675f49985a93d3 create mode 100644 .jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/31/0f2c9b8a33b0fe99313bb5404a41edd94d1e5b7b2ee107fd65dee39eae85fc create mode 100644 .jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/31/2e22a7c8248a79ef7f54b35eea7167c605f38fd7a97afdfd96efaeaf7003d7 create mode 100644 .jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/31/42f2ad67863295ba400bfb3135498e5c45b7011aedf57b8c99adf9c3f6e619 create mode 100644 .jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/38/0db33138f501cec3668d4a9c50a544f0db466dea22c678252b8c290857b101 create mode 100644 .jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/3c/4441eb6279c7b15496a6650e7a6d47c2a7b6a61fe733eeb7378e82f22b205f create mode 100644 .jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/56/315fd37e85c4876d21b4706a2683ad834a850c1d20ae89782a930b95ca20a1 create mode 100644 .jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/64/0dcfa1f823cdc7acadb519fc774fccceb0c55f3920c7c6c511f199898d5a52 create mode 100644 .jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/67/c37d10448d2b1b6028cd34d0b3bcc8c34b8afb372fdafdf7ee8d60881efd0d create mode 100644 .jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/6a/706d45f4524d2f8998235296537c9c5977d6cf63a584c7c9b2f4ed70bd08ca create mode 100644 .jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/6b/47a97f6e4c0dc3d0e7551b24b2c25cc0df3f1f583574f8d1ff385f874f4a91 create mode 100644 .jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/6e/14749fe8b12983f6c345332c384bc8aec15354278c2523de91bbb25433489d create mode 100644 .jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/79/b86a10903fba21dce19980f797a164b194f1ea6d5d0b728eef4e159b8f0ff6 create mode 100644 .jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/88/1e186b464f24bf211e1e135b02a4a72beada863aee13b6a5ba863753e43413 create mode 100644 .jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/88/faa68b702f9de90c6c437690a3605924fde8445269f610cff54fdae532dabf create mode 100644 .jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/96/cd90a15c43e26ab0db961eed13ad789f365fa35a9e8d514c74e2c20770e72f create mode 100644 .jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/bc/cf53a441d255e403111202b3ed3fe2f891cee97a750218df4a0a097009c552 create mode 100644 .jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/bc/dd8a5deafe1ef81a841dbd68ccce817763fd4324f08ca600bd434f53653379 create mode 100644 .jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/c8/eca608adf23904bb1d18f9910ec19fcef73b718e6398cacfe1fae48c012485 create mode 100644 .jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/cb/50172a143ca00e637885f846be8ab063cb4cb250e292ad4ed862dce6812792 create mode 100644 .jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/d0/8e9a0c9f35298f8db1bd039d2f5ccaf176d67b96a80968acaf2e072928f96e create mode 100644 .jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/d9/b5ab6cec9fad149bd31eb7db62afa0b2d95159590ce066db7e4bf311c7d7ef create mode 100644 .jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/da/b09ed8a154161612e04afdc9f1999b53390fe9439960e76f15ac671e41557e create mode 100644 .jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/e6/3c57c8b4d19a837b286788b30abc83c634e7c0b02117460c32af3b94bc0a9d create mode 100644 .jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/e6/7202aa0b32a0f8ba48b92b32bf107ce292bf219e963a88f3e05bf270b1ea4d create mode 100644 .jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/e8/d065ba4dd0b3f274f3079f8cb2a46505c910c97b0b874d75aff04d64b6e1ce create mode 100644 .jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/e9/7d1b313f2cc81e6a0dc63959b232c59e98452b42953419695d9f79dccbdb8e create mode 100644 .jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/f2/6eff824ca3b30418abc8afc8e08505f1e281553b6545c26884e9d4c60f2ce0 create mode 100644 .jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/f7/e941076127616402732ef140aef2ce103feeee5bb63ec202fa886814c0e2af create mode 100644 .jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/fd/85b957800aa99cc09024ca992ab938a3857380123ddeba79385ee4cd05808e create mode 100644 _posts/2020-01-02-python-learning-01-notes-variables-operators.md rename _posts/{2020-01-02-python-learning-01-note-variable-operator.md => 2020-01-02-python-learning-02-input-output-assignment.md} (88%) create mode 100644 _posts/2020-01-02-python-learning-03-branch-structure.md create mode 100644 _posts/2020-01-02-python-learning-04-loop-structure.md create mode 100644 _posts/2020-01-02-python-learning-05-function-module.md create mode 100644 _posts/2020-01-02-python-learning-05-string-list-tuple-dict-set.md diff --git a/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/01/96e631519e0009945c6eae1b45bfddb0ec8884f3a2b764465d6514c096d556 b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/01/96e631519e0009945c6eae1b45bfddb0ec8884f3a2b764465d6514c096d556 new file mode 100644 index 00000000000..cac152e280f --- /dev/null +++ b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/01/96e631519e0009945c6eae1b45bfddb0ec8884f3a2b764465d6514c096d556 @@ -0,0 +1,52 @@ +I"

函数

+ +
定义函数
+ +

使用def关键字来定义函数,通过return关键字来返回一个值,举个栗子:

+ +
1
+2
+3
+4
+5
+
def fac(num):   # 阶乘,实际上Python的math模块中有factorial函数,这里仅做演示
+    result = 1
+    for n in range(1, num + 1):
+        result *= n
+    return result
+
+ + + +

模块

+ +

由于Python没有函数重载的概念,那么后面的定义会覆盖之前的定义,也就意味着两个函数同名函数实际上只有一个是存在的。

+ +
1
+2
+3
+4
+5
+6
+7
+8
+
def foo():
+    print('hello, world!')
+
+def foo():
+    print('goodbye, world!')
+
+# 下面的代码会输出什么呢?
+foo()   # goodbye, world!
+
+ +
+

整篇除了函数的定义之外都不太懂,看 课件

+
+ +
+

Friendly reminder: This series are my own study notes of Python-100-Days. I do not own copyright of some of the content. Nor is this a good tutorial of Python. I really appreciate it if you notice any mistakes or errors and tell me. Sorry that things copied from Python-100-Days will not be noted due to huge workload. The whole series are not allowed to be reproduced. View python-learning-readme.

+:ET \ No newline at end of file diff --git a/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/12/5e67e444a19f2bc590728031ec64d626da27883abdf64420ba2f04f585e396 b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/12/5e67e444a19f2bc590728031ec64d626da27883abdf64420ba2f04f585e396 new file mode 100644 index 00000000000..f534597b203 --- /dev/null +++ b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/12/5e67e444a19f2bc590728031ec64d626da27883abdf64420ba2f04f585e396 @@ -0,0 +1,19 @@ +I"R

Friendly reminder: This series are my own study notes of Python-100-Days. I do not own copyright of some of the content. Nor is this a good tutorial of Python. I really appreciate it if you notice any mistakes or errors and tell me. Sorry that things copied from Python-100-Days will not be noted due to huge workload. The whole series are not allowed to be reproduced. View python-learning-readme.

+ +

Tools used:

+ + + +

Catalog:

+ + +:ET \ No newline at end of file diff --git a/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/13/2ca5aa787e9848c6f73d576e593e162d41a8d924f4a61426d4befea6b1ee2f b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/13/2ca5aa787e9848c6f73d576e593e162d41a8d924f4a61426d4befea6b1ee2f new file mode 100644 index 00000000000..ae0ac5d349b --- /dev/null +++ b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/13/2ca5aa787e9848c6f73d576e593e162d41a8d924f4a61426d4befea6b1ee2f @@ -0,0 +1,19 @@ +I"L

Friendly reminder: This series are my own study notes of Python-100-Days. I do not own copyright of some of the content. Nor is this a good tutorial of Python. I really appreciate it if you notice any mistakes or errors and tell me. Sorry that things copied from Python-100-Days will not be noted due to huge workload. The whole series are not allowed to be reproduced. View python-learning-readme.

+ +

Tools used:

+ + + +

Catalog:

+ + +:ET \ No newline at end of file diff --git a/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/15/a2e34fd0c6d1c6bb862141e9fc3a15409ade35655a3867bcbb91f9536519c2 b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/15/a2e34fd0c6d1c6bb862141e9fc3a15409ade35655a3867bcbb91f9536519c2 new file mode 100644 index 00000000000..94f0cd33717 --- /dev/null +++ b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/15/a2e34fd0c6d1c6bb862141e9fc3a15409ade35655a3867bcbb91f9536519c2 @@ -0,0 +1,47 @@ +I"'

赋值

+
1
+2
+3
+4
+
a = 10
+b = 3
+a += b # 相当于:a = a + b
+a *= a + 2 # 相当于:a = a * (a + 2)
+
+ +

输入input

+
1
+2
+3
+
a = int(input('a = ')) #读取为int类型
+f = float(input('请输入华氏温度: ')) #显示输入提示
+a, b = map(int, input().split()) #读取在同一行的多个数字
+
+ +

输出print

+
1
+2
+3
+4
+
print(a)
+print(a + b)
+print(a,b) #输出结果会以空格隔开
+print('%.1f' % a) #输出float型变量a,保留一位小数
+
+
+

Python格式化输出

+
+ + + +
+

Friendly reminder: This series are my own study notes of Python-100-Days. I do not own copyright of some of the content. Nor is this a good tutorial of Python. I really appreciate it if you notice any mistakes or errors and tell me. Sorry that things copied from Python-100-Days will not be noted due to huge workload. The whole series are not allowed to be reproduced. View python-learning-readme.

+:ET \ No newline at end of file diff --git a/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/1e/39c53e2c451469bcb6e52ed2ee4a6a32c7a957147d4d90491c4daa42908e84 b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/1e/39c53e2c451469bcb6e52ed2ee4a6a32c7a957147d4d90491c4daa42908e84 new file mode 100644 index 00000000000..ef83e8a443c --- /dev/null +++ b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/1e/39c53e2c451469bcb6e52ed2ee4a6a32c7a957147d4d90491c4daa42908e84 @@ -0,0 +1,453 @@ +I"!u

本节讲义 →

+ +

目录

+ + +

字符串

+ +
表示方法
+ +

由单引号或双引号括起来的单个或多个字符称为字符串,以三个双引号或单引号开头的字符串可以折行。

+ +
1
+2
+3
+4
+5
+6
+7
+8
+
s1 = 'hello, world!'
+s2 = "hello, world!"
+s3 = """
+hello,
+world!
+"""
+
+print(s1, s2, s3, end='')
+
+ +
转义
+ +

例如: \n表示换行,\'表示字符'\\表示字符\

+ +

\后面还可以跟一个八进制或者十六进制数来表示字符,例如\141\x61分别为八进制和十六进制的小写字母a

+ +

\后面还可以跟Unicode字符编码来表示字符(大大提高了可玩性)

+ +
1
+
print('\u00A2\u132B\u26BD\u2F5B\u3093') #¢ጫ⚽⽛ん
+
+ +

若不希望转义,可以通过在字符串的最前面加上字母r来实现。

+ +
1
+
s1 = r'\'hello, world!\''   # \'hello, world!\'
+
+ +
字符串的运算
+ +

Python为字符串类型提供了非常丰富的运算符:

+ +
1
+2
+3
+4
+5
+6
+7
+8
+9
+10
+11
+12
+13
+14
+15
+16
+17
+18
+19
+20
+21
+22
+23
+24
+
s1 = 'hello ' * 3
+print(s1) # hello hello hello
+
+s2 = 'world'
+s1 += s2
+print(s1) # hello hello hello world
+
+print('ll' in s1) # True
+print('good' in s1) # False
+
+
+str2 = 'abc123456'
+
+# 从字符串中取出指定位置的字符(下标运算)
+print(str2[2]) # c
+
+# 字符串切片(从指定的开始索引到指定的结束索引)
+#index 从 0 至 n-1,str2[l:r] 包含 l 不包含 r
+print(str2[2:5]) # c12
+print(str2[2:]) # c123456
+print(str2[2::2]) # c246
+print(str2[::2]) # ac246
+print(str2[::-1]) # 654321cba
+print(str2[-3:-1]) # 45
+
+ +

在Python中,我们还可以通过一系列的方法来完成对字符串的处理:

+
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
+
str1 = 'hello, world!'
+
+# 通过内置函数len计算字符串的长度
+print(len(str1)) # 13
+
+# 获得字符串首字母大写的拷贝
+print(str1.capitalize()) # Hello, world!
+
+# 获得字符串每个单词首字母大写的拷贝
+print(str1.title()) # Hello, World!
+
+# 获得字符串变大写后的拷贝
+print(str1.upper()) # HELLO, WORLD!
+
+# 从字符串中查找子串所在位置
+print(str1.find('or')) # 8
+print(str1.find('shit')) # -1
+# 与find类似但找不到子串时会引发异常
+# print(str1.index('or'))
+# print(str1.index('shit'))
+
+# 检查字符串是否以指定的字符串开头
+print(str1.startswith('He')) # False
+print(str1.startswith('hel')) # True
+
+# 检查字符串是否以指定的字符串结尾
+print(str1.endswith('!')) # True
+
+# 将字符串以指定的宽度居中并在两侧填充指定的字符
+print(str1.center(50, '*'))
+# 将字符串以指定的宽度靠右放置左侧填充指定的字符
+print(str1.rjust(50, ' '))
+
+
+str2 = 'abc123456'
+# 检查字符串是否由数字构成
+print(str2.isdigit())  # False
+
+# 检查字符串是否以字母构成
+print(str2.isalpha())  # False
+
+# 检查字符串是否以数字和字母构成
+print(str2.isalnum())  # True
+
+
+str3 = '  jackfrued@126.com '
+print(str3)
+
+# 获得字符串修剪左右两侧空格之后的拷贝
+print(str3.strip())
+
+ +

我们之前讲过,可以用下面的方式来格式化输出字符串:

+ +
1
+2
+
a, b = 5, 10
+print('%d * %d = %d' % (a, b, a * b))
+
+ +

当然,我们也可以用字符串提供的方法来完成字符串的格式:

+ +
1
+2
+
a, b = 5, 10
+print('{0} * {1} = {2}'.format(a, b, a * b))
+
+ +

Python 3.6以后,格式化字符串还有更为简洁的书写方式,就是在字符串前加上字母f,我们可以使用下面的语法糖来简化上面的代码。

+ +
1
+2
+
a, b = 5, 10
+print(f'{a} * {b} = {a * b}')
+
+ +

列表

+ +

列表(list),与字符串类似,也是一种结构化的、非标量类型,它是值的有序序列,每个值都可以通过索引进行标识,定义列表可以将列表的元素放在[]中,多个元素用,进行分隔,可以使用for循环对列表元素进行遍历,也可以使用[][:]运算符取出列表中的一个或多个元素

+ +

下面的代码演示了如何定义列表、如何遍历列表以及列表的下标运算。

+ +
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
+
list1 = [1, 3, 5, 7, 100]
+print(list1) # [1, 3, 5, 7, 100]
+
+# 乘号表示列表元素的重复
+list2 = ['hello'] * 3
+print(list2) # ['hello', 'hello', 'hello']
+
+# 计算列表长度(元素个数)
+print(len(list1)) # 5
+
+# 下标(索引)运算
+print(list1[0]) # 1
+print(list1[4]) # 100
+
+# print(list1[5])  # IndexError: list index out of range
+print(list1[-1]) # 100
+print(list1[-3]) # 5
+
+list1[2] = 300
+print(list1) # [1, 3, 300, 7, 100]
+
+# 通过循环用下标遍历列表元素
+for index in range(len(list1)):
+    print(list1[index])
+
+# 通过for循环遍历列表元素
+for elem in list1:
+    print(elem)
+
+# 通过enumerate函数处理列表之后再遍历可以同时获得元素索引和值
+for index, elem in enumerate(list1):
+    print(index, elem)
+
+ +

下面的代码演示了如何向列表中添加元素以及如何从列表中移除元素。

+ +
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
+
list1 = [1, 3, 5, 7, 100]
+
+# 添加元素
+list1.append(200)
+list1.insert(1, 400)
+
+# 合并两个列表
+# list1.extend([1000, 2000])
+list1 += [1000, 2000]
+print(list1) # [1, 400, 3, 5, 7, 100, 200, 1000, 2000]
+print(len(list1)) # 9
+
+# 先通过成员运算判断元素是否在列表中,如果存在就删除该元素
+if 3 in list1:
+	list1.remove(3)
+if 1234 in list1:
+    list1.remove(1234)
+print(list1) # [1, 400, 5, 7, 100, 200, 1000, 2000]
+
+# 从指定的位置删除元素
+list1.pop(0)
+list1.pop(len(list1) - 1)
+print(list1) # [400, 5, 7, 100, 200, 1000]
+
+# 清空列表元素
+list1.clear()
+print(list1) # []
+
+ +

和字符串一样,列表也可以做切片操作,通过切片操作我们可以实现对列表的复制或者将列表中的一部分取出来创建出新的列表,代码如下所示。

+ +
1
+2
+3
+4
+5
+6
+7
+8
+9
+10
+11
+12
+13
+14
+15
+16
+
fruits = ['grape', 'apple', 'strawberry', 'waxberry']
+fruits += ['pitaya', 'pear', 'mango']
+
+# 列表切片
+fruits2 = fruits[1:4]
+print(fruits2) # apple strawberry waxberry
+
+# 可以通过完整切片操作来复制列表
+fruits3 = fruits[:]
+print(fruits3) # ['grape', 'apple', 'strawberry', 'waxberry', 'pitaya', 'pear', 'mango']
+fruits4 = fruits[-3:-1]
+print(fruits4) # ['pitaya', 'pear']
+
+# 可以通过反向切片操作来获得倒转后的列表的拷贝
+fruits5 = fruits[::-1]
+print(fruits5) # ['mango', 'pear', 'pitaya', 'waxberry', 'strawberry', 'apple', 'grape']
+
+ +

下面的代码实现了对列表的排序操作。

+ +
1
+2
+3
+4
+5
+6
+7
+8
+9
+10
+11
+12
+13
+14
+15
+16
+17
+18
+19
+
list1 = ['orange', 'apple', 'zoo', 'internationalization', 'blueberry']
+
+list2 = sorted(list1)
+# sorted函数返回列表排序后的拷贝不会修改传入的列表
+# 函数的设计就应该像sorted函数一样尽可能不产生副作用
+
+list3 = sorted(list1, reverse=True)
+# 通过key关键字参数指定根据字符串长度进行排序而不是默认的字母表顺序
+
+list4 = sorted(list1, key=len)
+
+print(list1)
+print(list2)
+print(list3)
+print(list4)
+
+# 给列表对象发出排序消息直接在列表对象上进行排序
+list1.sort(reverse=True)
+print(list1)
+
+ +
使用生成式和生成器创建列表
+ +

元组

+ +

字典

+ +

集合

+ +
+

Friendly reminder: This series are my own study notes of Python-100-Days. I do not own copyright of some of the content. Nor is this a good tutorial of Python. I really appreciate it if you notice any mistakes or errors and tell me. Sorry that things copied from Python-100-Days will not be noted due to huge workload. The whole series are not allowed to be reproduced. View python-learning-readme.

+:ET \ No newline at end of file diff --git a/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/1f/3f6a391195fdde33c16f46671ac88b7f42390ec7b078b0016f1f2ec17269d1 b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/1f/3f6a391195fdde33c16f46671ac88b7f42390ec7b078b0016f1f2ec17269d1 new file mode 100644 index 00000000000..7240d74081d --- /dev/null +++ b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/1f/3f6a391195fdde33c16f46671ac88b7f42390ec7b078b0016f1f2ec17269d1 @@ -0,0 +1,19 @@ +I" + +

Tools used:

+ + + +

Catalog:

+ + +:ET \ No newline at end of file diff --git a/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/1f/baddb31a101a0959aff793cd881decb6a63816be594773d40f1b41392dd206 b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/1f/baddb31a101a0959aff793cd881decb6a63816be594773d40f1b41392dd206 new file mode 100644 index 00000000000..0e6941fcc02 --- /dev/null +++ b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/1f/baddb31a101a0959aff793cd881decb6a63816be594773d40f1b41392dd206 @@ -0,0 +1,11 @@ +I"

两分支 If-Else 语句

+
1
+2
+3
+4
+
if username == 'admin' and password == '123456':
+    print('身份验证成功!')
+else:
+    print('身份验证失败!')
+
+:ET \ No newline at end of file diff --git a/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/28/67445b10ea9cd3b1a7c049aa8ce5c3f820577564a5ee883933776d799c2ddf b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/28/67445b10ea9cd3b1a7c049aa8ce5c3f820577564a5ee883933776d799c2ddf new file mode 100644 index 00000000000..3a3cfdee207 --- /dev/null +++ b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/28/67445b10ea9cd3b1a7c049aa8ce5c3f820577564a5ee883933776d799c2ddf @@ -0,0 +1,255 @@ +I"&

本节讲义 ->

+ +

字符串

+ +
表示方法
+ +

由单引号或双引号括起来的单个或多个字符称为字符串,以三个双引号或单引号开头的字符串可以折行。

+ +
s1 = 'hello, world!'
+s2 = "hello, world!"
+s3 = """
+hello,
+world!
+"""
+
+print(s1, s2, s3, end='')
+
+ +
转义
+ +

例如: \n表示换行,\'表示字符'\\表示字符\

+ +

\后面还可以跟一个八进制或者十六进制数来表示字符,例如\141\x61分别为八进制和十六进制的小写字母a

+ +

\后面还可以跟Unicode字符编码来表示字符(大大提高了可玩性)

+
print('\u00A2\u132B\u26BD\u2F5B\u3093') #¢ጫ⚽⽛ん
+
+ +

若不希望转义,可以通过在字符串的最前面加上字母r来实现。

+
s1 = r'\'hello, world!\''   # \'hello, world!\'
+
+ +
字符串的运算
+ +

Python为字符串类型提供了非常丰富的运算符:

+ +
s1 = 'hello ' * 3
+print(s1) # hello hello hello
+
+s2 = 'world'
+s1 += s2
+print(s1) # hello hello hello world
+
+print('ll' in s1) # True
+print('good' in s1) # False
+
+
+str2 = 'abc123456'
+
+# 从字符串中取出指定位置的字符(下标运算)
+print(str2[2]) # c
+
+# 字符串切片(从指定的开始索引到指定的结束索引)
+#index 从 0 至 n-1,str2[l:r] 包含 l 不包含 r
+print(str2[2:5]) # c12
+print(str2[2:]) # c123456
+print(str2[2::2]) # c246
+print(str2[::2]) # ac246
+print(str2[::-1]) # 654321cba
+print(str2[-3:-1]) # 45
+
+ +

在Python中,我们还可以通过一系列的方法来完成对字符串的处理:

+
str1 = 'hello, world!'
+
+# 通过内置函数len计算字符串的长度
+print(len(str1)) # 13
+
+# 获得字符串首字母大写的拷贝
+print(str1.capitalize()) # Hello, world!
+
+# 获得字符串每个单词首字母大写的拷贝
+print(str1.title()) # Hello, World!
+
+# 获得字符串变大写后的拷贝
+print(str1.upper()) # HELLO, WORLD!
+
+# 从字符串中查找子串所在位置
+print(str1.find('or')) # 8
+print(str1.find('shit')) # -1
+# 与find类似但找不到子串时会引发异常
+# print(str1.index('or'))
+# print(str1.index('shit'))
+
+# 检查字符串是否以指定的字符串开头
+print(str1.startswith('He')) # False
+print(str1.startswith('hel')) # True
+
+# 检查字符串是否以指定的字符串结尾
+print(str1.endswith('!')) # True
+
+# 将字符串以指定的宽度居中并在两侧填充指定的字符
+print(str1.center(50, '*'))
+# 将字符串以指定的宽度靠右放置左侧填充指定的字符
+print(str1.rjust(50, ' '))
+
+
+str2 = 'abc123456'
+# 检查字符串是否由数字构成
+print(str2.isdigit())  # False
+
+# 检查字符串是否以字母构成
+print(str2.isalpha())  # False
+
+# 检查字符串是否以数字和字母构成
+print(str2.isalnum())  # True
+
+
+str3 = '  jackfrued@126.com '
+print(str3)
+
+# 获得字符串修剪左右两侧空格之后的拷贝
+print(str3.strip())
+
+ +

我们之前讲过,可以用下面的方式来格式化输出字符串:

+
a, b = 5, 10
+print('%d * %d = %d' % (a, b, a * b))
+
+ +

当然,我们也可以用字符串提供的方法来完成字符串的格式:

+
a, b = 5, 10
+print('{0} * {1} = {2}'.format(a, b, a * b))
+
+ +

Python 3.6以后,格式化字符串还有更为简洁的书写方式,就是在字符串前加上字母f,我们可以使用下面的语法糖来简化上面的代码。

+
a, b = 5, 10
+print(f'{a} * {b} = {a * b}')
+
+ +

列表 List

+ +

列表(list),与字符串类似,也是一种结构化的、非标量类型,它是值的有序序列,每个值都可以通过索引进行标识,定义列表可以将列表的元素放在[]中,多个元素用,进行分隔,可以使用for循环对列表元素进行遍历,也可以使用[][:]运算符取出列表中的一个或多个元素

+ +

下面的代码演示了如何定义列表、如何遍历列表以及列表的下标运算。

+ +
list1 = [1, 3, 5, 7, 100]
+print(list1) # [1, 3, 5, 7, 100]
+
+# 乘号表示列表元素的重复
+list2 = ['hello'] * 3
+print(list2) # ['hello', 'hello', 'hello']
+
+# 计算列表长度(元素个数)
+print(len(list1)) # 5
+
+# 下标(索引)运算
+print(list1[0]) # 1
+print(list1[4]) # 100
+
+# print(list1[5])  # IndexError: list index out of range
+print(list1[-1]) # 100
+print(list1[-3]) # 5
+
+list1[2] = 300
+print(list1) # [1, 3, 300, 7, 100]
+
+# 通过循环用下标遍历列表元素
+for index in range(len(list1)):
+    print(list1[index])
+
+# 通过for循环遍历列表元素
+for elem in list1:
+    print(elem)
+
+# 通过enumerate函数处理列表之后再遍历可以同时获得元素索引和值
+for index, elem in enumerate(list1):
+    print(index, elem)
+
+ +

下面的代码演示了如何向列表中添加元素以及如何从列表中移除元素。

+ +
list1 = [1, 3, 5, 7, 100]
+
+# 添加元素
+list1.append(200)
+list1.insert(1, 400)
+
+# 合并两个列表
+# list1.extend([1000, 2000])
+list1 += [1000, 2000]
+print(list1) # [1, 400, 3, 5, 7, 100, 200, 1000, 2000]
+print(len(list1)) # 9
+
+# 先通过成员运算判断元素是否在列表中,如果存在就删除该元素
+if 3 in list1:
+	list1.remove(3)
+if 1234 in list1:
+    list1.remove(1234)
+print(list1) # [1, 400, 5, 7, 100, 200, 1000, 2000]
+
+# 从指定的位置删除元素
+list1.pop(0)
+list1.pop(len(list1) - 1)
+print(list1) # [400, 5, 7, 100, 200, 1000]
+
+# 清空列表元素
+list1.clear()
+print(list1) # []
+
+ +

和字符串一样,列表也可以做切片操作,通过切片操作我们可以实现对列表的复制或者将列表中的一部分取出来创建出新的列表,代码如下所示。

+ +
fruits = ['grape', 'apple', 'strawberry', 'waxberry']
+fruits += ['pitaya', 'pear', 'mango']
+
+# 列表切片
+fruits2 = fruits[1:4]
+print(fruits2) # apple strawberry waxberry
+
+# 可以通过完整切片操作来复制列表
+fruits3 = fruits[:]
+print(fruits3) # ['grape', 'apple', 'strawberry', 'waxberry', 'pitaya', 'pear', 'mango']
+fruits4 = fruits[-3:-1]
+print(fruits4) # ['pitaya', 'pear']
+
+# 可以通过反向切片操作来获得倒转后的列表的拷贝
+fruits5 = fruits[::-1]
+print(fruits5) # ['mango', 'pear', 'pitaya', 'waxberry', 'strawberry', 'apple', 'grape']
+
+ +

下面的代码实现了对列表的排序操作。

+ +
list1 = ['orange', 'apple', 'zoo', 'internationalization', 'blueberry']
+
+list2 = sorted(list1)
+# sorted函数返回列表排序后的拷贝不会修改传入的列表
+# 函数的设计就应该像sorted函数一样尽可能不产生副作用
+
+list3 = sorted(list1, reverse=True)
+# 通过key关键字参数指定根据字符串长度进行排序而不是默认的字母表顺序
+
+list4 = sorted(list1, key=len)
+
+print(list1)
+print(list2)
+print(list3)
+print(list4)
+
+# 给列表对象发出排序消息直接在列表对象上进行排序
+list1.sort(reverse=True)
+print(list1)
+
+ +

生成式和生成器

+ +

元组

+ +

集合

+ +

字典

+ +
+

Friendly reminder: This series are my own study notes of Python-100-Days. I do not own copyright of some of the content. Nor is this a good tutorial of Python. I really appreciate it if you notice any mistakes or errors and tell me. Sorry that things copied from Python-100-Days will not be noted due to huge workload. The whole series are not allowed to be reproduced. View python-learning-readme.

+:ET \ No newline at end of file diff --git a/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/28/6bbe62b3352b5eea346227fe043619414c0f9da26dfac1a7892ccbc192be9f b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/28/6bbe62b3352b5eea346227fe043619414c0f9da26dfac1a7892ccbc192be9f new file mode 100644 index 00000000000..7c2655e6489 --- /dev/null +++ b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/28/6bbe62b3352b5eea346227fe043619414c0f9da26dfac1a7892ccbc192be9f @@ -0,0 +1,2 @@ +I"!

函数

+:ET \ No newline at end of file diff --git a/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/2f/b7bd2c8e678f471ff6e32eeb3ca9519aa41f80941c0fd39d98ff1541fa4b5d b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/2f/b7bd2c8e678f471ff6e32eeb3ca9519aa41f80941c0fd39d98ff1541fa4b5d new file mode 100644 index 00000000000..b3116e734ff --- /dev/null +++ b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/2f/b7bd2c8e678f471ff6e32eeb3ca9519aa41f80941c0fd39d98ff1541fa4b5d @@ -0,0 +1,7 @@ +I"

两分支 If-Else 语句

+
if username == 'admin' and password == '123456':
+    print('身份验证成功!')
+else:
+    print('身份验证失败!')
+
+:ET \ No newline at end of file diff --git a/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/2f/be2b0408f82230d564bf2fd509797eb7c9510c5adccc4783675f49985a93d3 b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/2f/be2b0408f82230d564bf2fd509797eb7c9510c5adccc4783675f49985a93d3 new file mode 100644 index 00000000000..424b9d63815 --- /dev/null +++ b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/2f/be2b0408f82230d564bf2fd509797eb7c9510c5adccc4783675f49985a93d3 @@ -0,0 +1,453 @@ +I"u

本节讲义 →

+ +

目录

+ + +

字符串

+ +
表示方法
+ +

由单引号或双引号括起来的单个或多个字符称为字符串,以三个双引号或单引号开头的字符串可以折行。

+ +
1
+2
+3
+4
+5
+6
+7
+8
+
s1 = 'hello, world!'
+s2 = "hello, world!"
+s3 = """
+hello,
+world!
+"""
+
+print(s1, s2, s3, end='')
+
+ +
转义
+ +

例如: \n表示换行,\'表示字符'\\表示字符\

+ +

\后面还可以跟一个八进制或者十六进制数来表示字符,例如\141\x61分别为八进制和十六进制的小写字母a

+ +

\后面还可以跟Unicode字符编码来表示字符(大大提高了可玩性)

+ +
1
+
print('\u00A2\u132B\u26BD\u2F5B\u3093') #¢ጫ⚽⽛ん
+
+ +

若不希望转义,可以通过在字符串的最前面加上字母r来实现。

+ +
1
+
s1 = r'\'hello, world!\''   # \'hello, world!\'
+
+ +
字符串的运算
+ +

Python为字符串类型提供了非常丰富的运算符:

+ +
1
+2
+3
+4
+5
+6
+7
+8
+9
+10
+11
+12
+13
+14
+15
+16
+17
+18
+19
+20
+21
+22
+23
+24
+
s1 = 'hello ' * 3
+print(s1) # hello hello hello
+
+s2 = 'world'
+s1 += s2
+print(s1) # hello hello hello world
+
+print('ll' in s1) # True
+print('good' in s1) # False
+
+
+str2 = 'abc123456'
+
+# 从字符串中取出指定位置的字符(下标运算)
+print(str2[2]) # c
+
+# 字符串切片(从指定的开始索引到指定的结束索引)
+#index 从 0 至 n-1,str2[l:r] 包含 l 不包含 r
+print(str2[2:5]) # c12
+print(str2[2:]) # c123456
+print(str2[2::2]) # c246
+print(str2[::2]) # ac246
+print(str2[::-1]) # 654321cba
+print(str2[-3:-1]) # 45
+
+ +

在Python中,我们还可以通过一系列的方法来完成对字符串的处理:

+
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
+
str1 = 'hello, world!'
+
+# 通过内置函数len计算字符串的长度
+print(len(str1)) # 13
+
+# 获得字符串首字母大写的拷贝
+print(str1.capitalize()) # Hello, world!
+
+# 获得字符串每个单词首字母大写的拷贝
+print(str1.title()) # Hello, World!
+
+# 获得字符串变大写后的拷贝
+print(str1.upper()) # HELLO, WORLD!
+
+# 从字符串中查找子串所在位置
+print(str1.find('or')) # 8
+print(str1.find('shit')) # -1
+# 与find类似但找不到子串时会引发异常
+# print(str1.index('or'))
+# print(str1.index('shit'))
+
+# 检查字符串是否以指定的字符串开头
+print(str1.startswith('He')) # False
+print(str1.startswith('hel')) # True
+
+# 检查字符串是否以指定的字符串结尾
+print(str1.endswith('!')) # True
+
+# 将字符串以指定的宽度居中并在两侧填充指定的字符
+print(str1.center(50, '*'))
+# 将字符串以指定的宽度靠右放置左侧填充指定的字符
+print(str1.rjust(50, ' '))
+
+
+str2 = 'abc123456'
+# 检查字符串是否由数字构成
+print(str2.isdigit())  # False
+
+# 检查字符串是否以字母构成
+print(str2.isalpha())  # False
+
+# 检查字符串是否以数字和字母构成
+print(str2.isalnum())  # True
+
+
+str3 = '  jackfrued@126.com '
+print(str3)
+
+# 获得字符串修剪左右两侧空格之后的拷贝
+print(str3.strip())
+
+ +

我们之前讲过,可以用下面的方式来格式化输出字符串:

+ +
1
+2
+
a, b = 5, 10
+print('%d * %d = %d' % (a, b, a * b))
+
+ +

当然,我们也可以用字符串提供的方法来完成字符串的格式:

+ +
1
+2
+
a, b = 5, 10
+print('{0} * {1} = {2}'.format(a, b, a * b))
+
+ +

Python 3.6以后,格式化字符串还有更为简洁的书写方式,就是在字符串前加上字母f,我们可以使用下面的语法糖来简化上面的代码。

+ +
1
+2
+
a, b = 5, 10
+print(f'{a} * {b} = {a * b}')
+
+ +

列表 List

+ +

列表(list),与字符串类似,也是一种结构化的、非标量类型,它是值的有序序列,每个值都可以通过索引进行标识,定义列表可以将列表的元素放在[]中,多个元素用,进行分隔,可以使用for循环对列表元素进行遍历,也可以使用[][:]运算符取出列表中的一个或多个元素

+ +

下面的代码演示了如何定义列表、如何遍历列表以及列表的下标运算。

+ +
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
+
list1 = [1, 3, 5, 7, 100]
+print(list1) # [1, 3, 5, 7, 100]
+
+# 乘号表示列表元素的重复
+list2 = ['hello'] * 3
+print(list2) # ['hello', 'hello', 'hello']
+
+# 计算列表长度(元素个数)
+print(len(list1)) # 5
+
+# 下标(索引)运算
+print(list1[0]) # 1
+print(list1[4]) # 100
+
+# print(list1[5])  # IndexError: list index out of range
+print(list1[-1]) # 100
+print(list1[-3]) # 5
+
+list1[2] = 300
+print(list1) # [1, 3, 300, 7, 100]
+
+# 通过循环用下标遍历列表元素
+for index in range(len(list1)):
+    print(list1[index])
+
+# 通过for循环遍历列表元素
+for elem in list1:
+    print(elem)
+
+# 通过enumerate函数处理列表之后再遍历可以同时获得元素索引和值
+for index, elem in enumerate(list1):
+    print(index, elem)
+
+ +

下面的代码演示了如何向列表中添加元素以及如何从列表中移除元素。

+ +
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
+
list1 = [1, 3, 5, 7, 100]
+
+# 添加元素
+list1.append(200)
+list1.insert(1, 400)
+
+# 合并两个列表
+# list1.extend([1000, 2000])
+list1 += [1000, 2000]
+print(list1) # [1, 400, 3, 5, 7, 100, 200, 1000, 2000]
+print(len(list1)) # 9
+
+# 先通过成员运算判断元素是否在列表中,如果存在就删除该元素
+if 3 in list1:
+	list1.remove(3)
+if 1234 in list1:
+    list1.remove(1234)
+print(list1) # [1, 400, 5, 7, 100, 200, 1000, 2000]
+
+# 从指定的位置删除元素
+list1.pop(0)
+list1.pop(len(list1) - 1)
+print(list1) # [400, 5, 7, 100, 200, 1000]
+
+# 清空列表元素
+list1.clear()
+print(list1) # []
+
+ +

和字符串一样,列表也可以做切片操作,通过切片操作我们可以实现对列表的复制或者将列表中的一部分取出来创建出新的列表,代码如下所示。

+ +
1
+2
+3
+4
+5
+6
+7
+8
+9
+10
+11
+12
+13
+14
+15
+16
+
fruits = ['grape', 'apple', 'strawberry', 'waxberry']
+fruits += ['pitaya', 'pear', 'mango']
+
+# 列表切片
+fruits2 = fruits[1:4]
+print(fruits2) # apple strawberry waxberry
+
+# 可以通过完整切片操作来复制列表
+fruits3 = fruits[:]
+print(fruits3) # ['grape', 'apple', 'strawberry', 'waxberry', 'pitaya', 'pear', 'mango']
+fruits4 = fruits[-3:-1]
+print(fruits4) # ['pitaya', 'pear']
+
+# 可以通过反向切片操作来获得倒转后的列表的拷贝
+fruits5 = fruits[::-1]
+print(fruits5) # ['mango', 'pear', 'pitaya', 'waxberry', 'strawberry', 'apple', 'grape']
+
+ +

下面的代码实现了对列表的排序操作。

+ +
1
+2
+3
+4
+5
+6
+7
+8
+9
+10
+11
+12
+13
+14
+15
+16
+17
+18
+19
+
list1 = ['orange', 'apple', 'zoo', 'internationalization', 'blueberry']
+
+list2 = sorted(list1)
+# sorted函数返回列表排序后的拷贝不会修改传入的列表
+# 函数的设计就应该像sorted函数一样尽可能不产生副作用
+
+list3 = sorted(list1, reverse=True)
+# 通过key关键字参数指定根据字符串长度进行排序而不是默认的字母表顺序
+
+list4 = sorted(list1, key=len)
+
+print(list1)
+print(list2)
+print(list3)
+print(list4)
+
+# 给列表对象发出排序消息直接在列表对象上进行排序
+list1.sort(reverse=True)
+print(list1)
+
+ +

生成式和生成器

+ +

元组

+ +

集合

+ +

字典

+ +
+

Friendly reminder: This series are my own study notes of Python-100-Days. I do not own copyright of some of the content. Nor is this a good tutorial of Python. I really appreciate it if you notice any mistakes or errors and tell me. Sorry that things copied from Python-100-Days will not be noted due to huge workload. The whole series are not allowed to be reproduced. View python-learning-readme.

+:ET \ No newline at end of file diff --git a/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/31/0f2c9b8a33b0fe99313bb5404a41edd94d1e5b7b2ee107fd65dee39eae85fc b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/31/0f2c9b8a33b0fe99313bb5404a41edd94d1e5b7b2ee107fd65dee39eae85fc new file mode 100644 index 00000000000..20b844bd555 --- /dev/null +++ b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/31/0f2c9b8a33b0fe99313bb5404a41edd94d1e5b7b2ee107fd65dee39eae85fc @@ -0,0 +1,52 @@ +I"

函数

+ +
定义函数
+ +

使用def关键字来定义函数,通过return关键字来返回一个值,举个栗子:

+ +
1
+2
+3
+4
+5
+
def fac(num):   # 阶乘,实际上Python的math模块中有factorial函数,这里仅做演示
+    result = 1
+    for n in range(1, num + 1):
+        result *= n
+    return result
+
+ + + +

模块

+ +

由于Python没有函数重载的概念,那么后面的定义会覆盖之前的定义,也就意味着两个函数同名函数实际上只有一个是存在的。

+ +
1
+2
+3
+4
+5
+6
+7
+8
+
def foo():
+    print('hello, world!')
+
+def foo():
+    print('goodbye, world!')
+
+# 下面的代码会输出什么呢?
+foo()   # goodbye, world!
+
+ +

使用模块管理函数

+ +

不是hin懂,以后再看。前往 课件.

+ +
+

Friendly reminder: This series are my own study notes of Python-100-Days. I do not own copyright of some of the content. Nor is this a good tutorial of Python. I really appreciate it if you notice any mistakes or errors and tell me. Sorry that things copied from Python-100-Days will not be noted due to huge workload. The whole series are not allowed to be reproduced. View python-learning-readme.

+:ET \ No newline at end of file diff --git a/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/31/2e22a7c8248a79ef7f54b35eea7167c605f38fd7a97afdfd96efaeaf7003d7 b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/31/2e22a7c8248a79ef7f54b35eea7167c605f38fd7a97afdfd96efaeaf7003d7 new file mode 100644 index 00000000000..0d67c9bf366 --- /dev/null +++ b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/31/2e22a7c8248a79ef7f54b35eea7167c605f38fd7a97afdfd96efaeaf7003d7 @@ -0,0 +1,46 @@ +I"!

赋值

+
1
+2
+3
+4
+
a = 10
+b = 3
+a += b # 相当于:a = a + b
+a *= a + 2 # 相当于:a = a * (a + 2)
+
+ +

输入input

+
1
+2
+3
+
a = int(input('a = ')) #读取为int类型
+f = float(input('请输入华氏温度: ')) #显示输入提示
+a, b = map(int, input().split()) #读取在同一行的多个数字
+
+ +

输出print`

+
1
+2
+3
+4
+
print(a)
+print(a + b)
+print(a,b) #输出结果会以空格隔开
+print('%.1f' % a) #输出float型变量a,保留一位小数
+
+
+

Python格式化输出

+
+ + + +

Friendly reminder: This series are my own study notes of Python-100-Days. I do not own copyright of some of the content. Nor is this a good tutorial of Python. I really appreciate it if you notice any mistakes or errors and tell me. Sorry that things copied from Python-100-Days will not be noted due to huge workload. The whole series are not allowed to be reproduced. View python-learning-readme.

+:ET \ No newline at end of file diff --git a/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/31/42f2ad67863295ba400bfb3135498e5c45b7011aedf57b8c99adf9c3f6e619 b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/31/42f2ad67863295ba400bfb3135498e5c45b7011aedf57b8c99adf9c3f6e619 new file mode 100644 index 00000000000..1f4466734b3 --- /dev/null +++ b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/31/42f2ad67863295ba400bfb3135498e5c45b7011aedf57b8c99adf9c3f6e619 @@ -0,0 +1,47 @@ +I"p

两分支 If-Else 语句

+
1
+2
+3
+4
+
if username == 'admin' and password == '123456':
+    print('身份验证成功!')
+else:
+    print('身份验证失败!')
+
+ +

多分支 If-Elseif-Else 语句

+
1
+2
+3
+4
+5
+6
+7
+
x = float(input('x = '))
+if x > 1:
+    y = 3 * x - 5
+elif x >= -1:
+    y = x + 2
+else:
+    y = 5 * x + 3
+
+ +

悬空Else分支

+

使用pass命令执行“不执行任何操作”

+
1
+2
+3
+4
+5
+
x = float(input('x = '))
+if x > 1:
+    print(x)
+else
+    pass
+
+

Attention:尽量避免没有必要的嵌套,嵌套既影响可读性也增加bug出现概率

+ +

Attention: 同时注意,与Pascal不同,if和else语句末尾有冒号”:”

+ +

Friendly reminder: This series are my own study notes of Python-100-Days. I do not own copyright of some of the content. Nor is this a good tutorial of Python. I really appreciate it if you notice any mistakes or errors and tell me. Sorry that things copied from Python-100-Days will not be noted due to huge workload. The whole series are not allowed to be reproduced. View python-learning-readme.

+:ET \ No newline at end of file diff --git a/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/38/0db33138f501cec3668d4a9c50a544f0db466dea22c678252b8c290857b101 b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/38/0db33138f501cec3668d4a9c50a544f0db466dea22c678252b8c290857b101 new file mode 100644 index 00000000000..ae0a478fbd6 --- /dev/null +++ b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/38/0db33138f501cec3668d4a9c50a544f0db466dea22c678252b8c290857b101 @@ -0,0 +1,11 @@ +I"

赋值

+
1
+2
+3
+4
+
a = 10
+b = 3
+a += b # 相当于:a = a + b
+a *= a + 2 # 相当于:a = a * (a + 2)
+
+:ET \ No newline at end of file diff --git a/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/3c/4441eb6279c7b15496a6650e7a6d47c2a7b6a61fe733eeb7378e82f22b205f b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/3c/4441eb6279c7b15496a6650e7a6d47c2a7b6a61fe733eeb7378e82f22b205f new file mode 100644 index 00000000000..6cf84743dc5 --- /dev/null +++ b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/3c/4441eb6279c7b15496a6650e7a6d47c2a7b6a61fe733eeb7378e82f22b205f @@ -0,0 +1,2 @@ +I"

本节讲义 $rarr

+:ET \ No newline at end of file diff --git a/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/56/315fd37e85c4876d21b4706a2683ad834a850c1d20ae89782a930b95ca20a1 b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/56/315fd37e85c4876d21b4706a2683ad834a850c1d20ae89782a930b95ca20a1 new file mode 100644 index 00000000000..6b9b8b153d2 --- /dev/null +++ b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/56/315fd37e85c4876d21b4706a2683ad834a850c1d20ae89782a930b95ca20a1 @@ -0,0 +1,52 @@ +I"

函数

+ +
定义函数
+ +

使用def关键字来定义函数,通过return关键字来返回一个值,举个栗子:

+ +
1
+2
+3
+4
+5
+
def fac(num):   # 阶乘,实际上Python的math模块中有factorial函数,这里仅做演示
+    result = 1
+    for n in range(1, num + 1):
+        result *= n
+    return result
+
+ + + +

模块

+ +

由于Python没有函数重载的概念,那么后面的定义会覆盖之前的定义,也就意味着两个函数同名函数实际上只有一个是存在的。

+ +
1
+2
+3
+4
+5
+6
+7
+8
+
def foo():
+    print('hello, world!')
+
+def foo():
+    print('goodbye, world!')
+
+# 下面的代码会输出什么呢?
+foo()   # goodbye, world!
+
+ +

使用模块管理函数

+ +

不是hin懂,前往课件.

+ +
+

Friendly reminder: This series are my own study notes of Python-100-Days. I do not own copyright of some of the content. Nor is this a good tutorial of Python. I really appreciate it if you notice any mistakes or errors and tell me. Sorry that things copied from Python-100-Days will not be noted due to huge workload. The whole series are not allowed to be reproduced. View python-learning-readme.

+:ET \ No newline at end of file diff --git a/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/64/0dcfa1f823cdc7acadb519fc774fccceb0c55f3920c7c6c511f199898d5a52 b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/64/0dcfa1f823cdc7acadb519fc774fccceb0c55f3920c7c6c511f199898d5a52 new file mode 100644 index 00000000000..e828e77c0d6 --- /dev/null +++ b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/64/0dcfa1f823cdc7acadb519fc774fccceb0c55f3920c7c6c511f199898d5a52 @@ -0,0 +1,2 @@ +I"' +:ET \ No newline at end of file diff --git a/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/67/c37d10448d2b1b6028cd34d0b3bcc8c34b8afb372fdafdf7ee8d60881efd0d b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/67/c37d10448d2b1b6028cd34d0b3bcc8c34b8afb372fdafdf7ee8d60881efd0d new file mode 100644 index 00000000000..dbd4074f38a --- /dev/null +++ b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/67/c37d10448d2b1b6028cd34d0b3bcc8c34b8afb372fdafdf7ee8d60881efd0d @@ -0,0 +1,47 @@ +I"

两分支 If-Else 语句

+
1
+2
+3
+4
+
if username == 'admin' and password == '123456':
+    print('身份验证成功!')
+else:
+    print('身份验证失败!')
+
+ +

多分支 If-Elseif-Else 语句

+
1
+2
+3
+4
+5
+6
+7
+
x = float(input('x = '))
+if x > 1:
+    y = 3 * x - 5
+elif x >= -1:
+    y = x + 2
+else:
+    y = 5 * x + 3
+
+ +

悬空Else分支

+

使用pass命令执行“不执行任何操作”

+
1
+2
+3
+4
+5
+
x = float(input('x = '))
+if x > 1:
+    print(x)
+else
+    pass
+
+

Attention:尽量避免没有必要的嵌套,嵌套既影响可读性也增加bug出现概率

+ +

Attention: 同时注意,与Pascal不同,if和else语句末尾有冒号”:”

+ +

Friendly reminder: This series are my own study notes of Python-100-Days. I do not own copyright of some of the content. Nor is this a good tutorial of Python. I really appreciate it if you notice any mistakes or errors and tell me. Sorry that things copied from Python-100-Days will not be noted due to huge workload. The whole series are not allowed to be reproduced. View python-learning-readme.

+:ET \ No newline at end of file diff --git a/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/6a/706d45f4524d2f8998235296537c9c5977d6cf63a584c7c9b2f4ed70bd08ca b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/6a/706d45f4524d2f8998235296537c9c5977d6cf63a584c7c9b2f4ed70bd08ca new file mode 100644 index 00000000000..ca297d72682 --- /dev/null +++ b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/6a/706d45f4524d2f8998235296537c9c5977d6cf63a584c7c9b2f4ed70bd08ca @@ -0,0 +1,255 @@ +I"&

本节讲义 &rarr

+ +

字符串

+ +
表示方法
+ +

由单引号或双引号括起来的单个或多个字符称为字符串,以三个双引号或单引号开头的字符串可以折行。

+ +
s1 = 'hello, world!'
+s2 = "hello, world!"
+s3 = """
+hello,
+world!
+"""
+
+print(s1, s2, s3, end='')
+
+ +
转义
+ +

例如: \n表示换行,\'表示字符'\\表示字符\

+ +

\后面还可以跟一个八进制或者十六进制数来表示字符,例如\141\x61分别为八进制和十六进制的小写字母a

+ +

\后面还可以跟Unicode字符编码来表示字符(大大提高了可玩性)

+
print('\u00A2\u132B\u26BD\u2F5B\u3093') #¢ጫ⚽⽛ん
+
+ +

若不希望转义,可以通过在字符串的最前面加上字母r来实现。

+
s1 = r'\'hello, world!\''   # \'hello, world!\'
+
+ +
字符串的运算
+ +

Python为字符串类型提供了非常丰富的运算符:

+ +
s1 = 'hello ' * 3
+print(s1) # hello hello hello
+
+s2 = 'world'
+s1 += s2
+print(s1) # hello hello hello world
+
+print('ll' in s1) # True
+print('good' in s1) # False
+
+
+str2 = 'abc123456'
+
+# 从字符串中取出指定位置的字符(下标运算)
+print(str2[2]) # c
+
+# 字符串切片(从指定的开始索引到指定的结束索引)
+#index 从 0 至 n-1,str2[l:r] 包含 l 不包含 r
+print(str2[2:5]) # c12
+print(str2[2:]) # c123456
+print(str2[2::2]) # c246
+print(str2[::2]) # ac246
+print(str2[::-1]) # 654321cba
+print(str2[-3:-1]) # 45
+
+ +

在Python中,我们还可以通过一系列的方法来完成对字符串的处理:

+
str1 = 'hello, world!'
+
+# 通过内置函数len计算字符串的长度
+print(len(str1)) # 13
+
+# 获得字符串首字母大写的拷贝
+print(str1.capitalize()) # Hello, world!
+
+# 获得字符串每个单词首字母大写的拷贝
+print(str1.title()) # Hello, World!
+
+# 获得字符串变大写后的拷贝
+print(str1.upper()) # HELLO, WORLD!
+
+# 从字符串中查找子串所在位置
+print(str1.find('or')) # 8
+print(str1.find('shit')) # -1
+# 与find类似但找不到子串时会引发异常
+# print(str1.index('or'))
+# print(str1.index('shit'))
+
+# 检查字符串是否以指定的字符串开头
+print(str1.startswith('He')) # False
+print(str1.startswith('hel')) # True
+
+# 检查字符串是否以指定的字符串结尾
+print(str1.endswith('!')) # True
+
+# 将字符串以指定的宽度居中并在两侧填充指定的字符
+print(str1.center(50, '*'))
+# 将字符串以指定的宽度靠右放置左侧填充指定的字符
+print(str1.rjust(50, ' '))
+
+
+str2 = 'abc123456'
+# 检查字符串是否由数字构成
+print(str2.isdigit())  # False
+
+# 检查字符串是否以字母构成
+print(str2.isalpha())  # False
+
+# 检查字符串是否以数字和字母构成
+print(str2.isalnum())  # True
+
+
+str3 = '  jackfrued@126.com '
+print(str3)
+
+# 获得字符串修剪左右两侧空格之后的拷贝
+print(str3.strip())
+
+ +

我们之前讲过,可以用下面的方式来格式化输出字符串:

+
a, b = 5, 10
+print('%d * %d = %d' % (a, b, a * b))
+
+ +

当然,我们也可以用字符串提供的方法来完成字符串的格式:

+
a, b = 5, 10
+print('{0} * {1} = {2}'.format(a, b, a * b))
+
+ +

Python 3.6以后,格式化字符串还有更为简洁的书写方式,就是在字符串前加上字母f,我们可以使用下面的语法糖来简化上面的代码。

+
a, b = 5, 10
+print(f'{a} * {b} = {a * b}')
+
+ +

列表 List

+ +

列表(list),与字符串类似,也是一种结构化的、非标量类型,它是值的有序序列,每个值都可以通过索引进行标识,定义列表可以将列表的元素放在[]中,多个元素用,进行分隔,可以使用for循环对列表元素进行遍历,也可以使用[][:]运算符取出列表中的一个或多个元素

+ +

下面的代码演示了如何定义列表、如何遍历列表以及列表的下标运算。

+ +
list1 = [1, 3, 5, 7, 100]
+print(list1) # [1, 3, 5, 7, 100]
+
+# 乘号表示列表元素的重复
+list2 = ['hello'] * 3
+print(list2) # ['hello', 'hello', 'hello']
+
+# 计算列表长度(元素个数)
+print(len(list1)) # 5
+
+# 下标(索引)运算
+print(list1[0]) # 1
+print(list1[4]) # 100
+
+# print(list1[5])  # IndexError: list index out of range
+print(list1[-1]) # 100
+print(list1[-3]) # 5
+
+list1[2] = 300
+print(list1) # [1, 3, 300, 7, 100]
+
+# 通过循环用下标遍历列表元素
+for index in range(len(list1)):
+    print(list1[index])
+
+# 通过for循环遍历列表元素
+for elem in list1:
+    print(elem)
+
+# 通过enumerate函数处理列表之后再遍历可以同时获得元素索引和值
+for index, elem in enumerate(list1):
+    print(index, elem)
+
+ +

下面的代码演示了如何向列表中添加元素以及如何从列表中移除元素。

+ +
list1 = [1, 3, 5, 7, 100]
+
+# 添加元素
+list1.append(200)
+list1.insert(1, 400)
+
+# 合并两个列表
+# list1.extend([1000, 2000])
+list1 += [1000, 2000]
+print(list1) # [1, 400, 3, 5, 7, 100, 200, 1000, 2000]
+print(len(list1)) # 9
+
+# 先通过成员运算判断元素是否在列表中,如果存在就删除该元素
+if 3 in list1:
+	list1.remove(3)
+if 1234 in list1:
+    list1.remove(1234)
+print(list1) # [1, 400, 5, 7, 100, 200, 1000, 2000]
+
+# 从指定的位置删除元素
+list1.pop(0)
+list1.pop(len(list1) - 1)
+print(list1) # [400, 5, 7, 100, 200, 1000]
+
+# 清空列表元素
+list1.clear()
+print(list1) # []
+
+ +

和字符串一样,列表也可以做切片操作,通过切片操作我们可以实现对列表的复制或者将列表中的一部分取出来创建出新的列表,代码如下所示。

+ +
fruits = ['grape', 'apple', 'strawberry', 'waxberry']
+fruits += ['pitaya', 'pear', 'mango']
+
+# 列表切片
+fruits2 = fruits[1:4]
+print(fruits2) # apple strawberry waxberry
+
+# 可以通过完整切片操作来复制列表
+fruits3 = fruits[:]
+print(fruits3) # ['grape', 'apple', 'strawberry', 'waxberry', 'pitaya', 'pear', 'mango']
+fruits4 = fruits[-3:-1]
+print(fruits4) # ['pitaya', 'pear']
+
+# 可以通过反向切片操作来获得倒转后的列表的拷贝
+fruits5 = fruits[::-1]
+print(fruits5) # ['mango', 'pear', 'pitaya', 'waxberry', 'strawberry', 'apple', 'grape']
+
+ +

下面的代码实现了对列表的排序操作。

+ +
list1 = ['orange', 'apple', 'zoo', 'internationalization', 'blueberry']
+
+list2 = sorted(list1)
+# sorted函数返回列表排序后的拷贝不会修改传入的列表
+# 函数的设计就应该像sorted函数一样尽可能不产生副作用
+
+list3 = sorted(list1, reverse=True)
+# 通过key关键字参数指定根据字符串长度进行排序而不是默认的字母表顺序
+
+list4 = sorted(list1, key=len)
+
+print(list1)
+print(list2)
+print(list3)
+print(list4)
+
+# 给列表对象发出排序消息直接在列表对象上进行排序
+list1.sort(reverse=True)
+print(list1)
+
+ +

生成式和生成器

+ +

元组

+ +

集合

+ +

字典

+ +
+

Friendly reminder: This series are my own study notes of Python-100-Days. I do not own copyright of some of the content. Nor is this a good tutorial of Python. I really appreciate it if you notice any mistakes or errors and tell me. Sorry that things copied from Python-100-Days will not be noted due to huge workload. The whole series are not allowed to be reproduced. View python-learning-readme.

+:ET \ No newline at end of file diff --git a/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/6b/47a97f6e4c0dc3d0e7551b24b2c25cc0df3f1f583574f8d1ff385f874f4a91 b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/6b/47a97f6e4c0dc3d0e7551b24b2c25cc0df3f1f583574f8d1ff385f874f4a91 new file mode 100644 index 00000000000..a3603c36f04 --- /dev/null +++ b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/6b/47a97f6e4c0dc3d0e7551b24b2c25cc0df3f1f583574f8d1ff385f874f4a91 @@ -0,0 +1,2 @@ +I"

Friendly reminder: This series are my own study notes of Python-100-Days. I do not own copyright of some of the content. Nor is this a good tutorial of Python. I really appreciate it if you notice any mistakes or errors and tell me. Sorry that things copied from Python-100-Days will not be noted due to huge workload. The whole series are not allowed to be reproduced. View python-learning-readme.

+:ET \ No newline at end of file diff --git a/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/6e/14749fe8b12983f6c345332c384bc8aec15354278c2523de91bbb25433489d b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/6e/14749fe8b12983f6c345332c384bc8aec15354278c2523de91bbb25433489d new file mode 100644 index 00000000000..334ff60d47d --- /dev/null +++ b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/6e/14749fe8b12983f6c345332c384bc8aec15354278c2523de91bbb25433489d @@ -0,0 +1,255 @@ +I"&

本节讲义 →

+ +

字符串

+ +
表示方法
+ +

由单引号或双引号括起来的单个或多个字符称为字符串,以三个双引号或单引号开头的字符串可以折行。

+ +
s1 = 'hello, world!'
+s2 = "hello, world!"
+s3 = """
+hello,
+world!
+"""
+
+print(s1, s2, s3, end='')
+
+ +
转义
+ +

例如: \n表示换行,\'表示字符'\\表示字符\

+ +

\后面还可以跟一个八进制或者十六进制数来表示字符,例如\141\x61分别为八进制和十六进制的小写字母a

+ +

\后面还可以跟Unicode字符编码来表示字符(大大提高了可玩性)

+
print('\u00A2\u132B\u26BD\u2F5B\u3093') #¢ጫ⚽⽛ん
+
+ +

若不希望转义,可以通过在字符串的最前面加上字母r来实现。

+
s1 = r'\'hello, world!\''   # \'hello, world!\'
+
+ +
字符串的运算
+ +

Python为字符串类型提供了非常丰富的运算符:

+ +
s1 = 'hello ' * 3
+print(s1) # hello hello hello
+
+s2 = 'world'
+s1 += s2
+print(s1) # hello hello hello world
+
+print('ll' in s1) # True
+print('good' in s1) # False
+
+
+str2 = 'abc123456'
+
+# 从字符串中取出指定位置的字符(下标运算)
+print(str2[2]) # c
+
+# 字符串切片(从指定的开始索引到指定的结束索引)
+#index 从 0 至 n-1,str2[l:r] 包含 l 不包含 r
+print(str2[2:5]) # c12
+print(str2[2:]) # c123456
+print(str2[2::2]) # c246
+print(str2[::2]) # ac246
+print(str2[::-1]) # 654321cba
+print(str2[-3:-1]) # 45
+
+ +

在Python中,我们还可以通过一系列的方法来完成对字符串的处理:

+
str1 = 'hello, world!'
+
+# 通过内置函数len计算字符串的长度
+print(len(str1)) # 13
+
+# 获得字符串首字母大写的拷贝
+print(str1.capitalize()) # Hello, world!
+
+# 获得字符串每个单词首字母大写的拷贝
+print(str1.title()) # Hello, World!
+
+# 获得字符串变大写后的拷贝
+print(str1.upper()) # HELLO, WORLD!
+
+# 从字符串中查找子串所在位置
+print(str1.find('or')) # 8
+print(str1.find('shit')) # -1
+# 与find类似但找不到子串时会引发异常
+# print(str1.index('or'))
+# print(str1.index('shit'))
+
+# 检查字符串是否以指定的字符串开头
+print(str1.startswith('He')) # False
+print(str1.startswith('hel')) # True
+
+# 检查字符串是否以指定的字符串结尾
+print(str1.endswith('!')) # True
+
+# 将字符串以指定的宽度居中并在两侧填充指定的字符
+print(str1.center(50, '*'))
+# 将字符串以指定的宽度靠右放置左侧填充指定的字符
+print(str1.rjust(50, ' '))
+
+
+str2 = 'abc123456'
+# 检查字符串是否由数字构成
+print(str2.isdigit())  # False
+
+# 检查字符串是否以字母构成
+print(str2.isalpha())  # False
+
+# 检查字符串是否以数字和字母构成
+print(str2.isalnum())  # True
+
+
+str3 = '  jackfrued@126.com '
+print(str3)
+
+# 获得字符串修剪左右两侧空格之后的拷贝
+print(str3.strip())
+
+ +

我们之前讲过,可以用下面的方式来格式化输出字符串:

+
a, b = 5, 10
+print('%d * %d = %d' % (a, b, a * b))
+
+ +

当然,我们也可以用字符串提供的方法来完成字符串的格式:

+
a, b = 5, 10
+print('{0} * {1} = {2}'.format(a, b, a * b))
+
+ +

Python 3.6以后,格式化字符串还有更为简洁的书写方式,就是在字符串前加上字母f,我们可以使用下面的语法糖来简化上面的代码。

+
a, b = 5, 10
+print(f'{a} * {b} = {a * b}')
+
+ +

列表 List

+ +

列表(list),与字符串类似,也是一种结构化的、非标量类型,它是值的有序序列,每个值都可以通过索引进行标识,定义列表可以将列表的元素放在[]中,多个元素用,进行分隔,可以使用for循环对列表元素进行遍历,也可以使用[][:]运算符取出列表中的一个或多个元素

+ +

下面的代码演示了如何定义列表、如何遍历列表以及列表的下标运算。

+ +
list1 = [1, 3, 5, 7, 100]
+print(list1) # [1, 3, 5, 7, 100]
+
+# 乘号表示列表元素的重复
+list2 = ['hello'] * 3
+print(list2) # ['hello', 'hello', 'hello']
+
+# 计算列表长度(元素个数)
+print(len(list1)) # 5
+
+# 下标(索引)运算
+print(list1[0]) # 1
+print(list1[4]) # 100
+
+# print(list1[5])  # IndexError: list index out of range
+print(list1[-1]) # 100
+print(list1[-3]) # 5
+
+list1[2] = 300
+print(list1) # [1, 3, 300, 7, 100]
+
+# 通过循环用下标遍历列表元素
+for index in range(len(list1)):
+    print(list1[index])
+
+# 通过for循环遍历列表元素
+for elem in list1:
+    print(elem)
+
+# 通过enumerate函数处理列表之后再遍历可以同时获得元素索引和值
+for index, elem in enumerate(list1):
+    print(index, elem)
+
+ +

下面的代码演示了如何向列表中添加元素以及如何从列表中移除元素。

+ +
list1 = [1, 3, 5, 7, 100]
+
+# 添加元素
+list1.append(200)
+list1.insert(1, 400)
+
+# 合并两个列表
+# list1.extend([1000, 2000])
+list1 += [1000, 2000]
+print(list1) # [1, 400, 3, 5, 7, 100, 200, 1000, 2000]
+print(len(list1)) # 9
+
+# 先通过成员运算判断元素是否在列表中,如果存在就删除该元素
+if 3 in list1:
+	list1.remove(3)
+if 1234 in list1:
+    list1.remove(1234)
+print(list1) # [1, 400, 5, 7, 100, 200, 1000, 2000]
+
+# 从指定的位置删除元素
+list1.pop(0)
+list1.pop(len(list1) - 1)
+print(list1) # [400, 5, 7, 100, 200, 1000]
+
+# 清空列表元素
+list1.clear()
+print(list1) # []
+
+ +

和字符串一样,列表也可以做切片操作,通过切片操作我们可以实现对列表的复制或者将列表中的一部分取出来创建出新的列表,代码如下所示。

+ +
fruits = ['grape', 'apple', 'strawberry', 'waxberry']
+fruits += ['pitaya', 'pear', 'mango']
+
+# 列表切片
+fruits2 = fruits[1:4]
+print(fruits2) # apple strawberry waxberry
+
+# 可以通过完整切片操作来复制列表
+fruits3 = fruits[:]
+print(fruits3) # ['grape', 'apple', 'strawberry', 'waxberry', 'pitaya', 'pear', 'mango']
+fruits4 = fruits[-3:-1]
+print(fruits4) # ['pitaya', 'pear']
+
+# 可以通过反向切片操作来获得倒转后的列表的拷贝
+fruits5 = fruits[::-1]
+print(fruits5) # ['mango', 'pear', 'pitaya', 'waxberry', 'strawberry', 'apple', 'grape']
+
+ +

下面的代码实现了对列表的排序操作。

+ +
list1 = ['orange', 'apple', 'zoo', 'internationalization', 'blueberry']
+
+list2 = sorted(list1)
+# sorted函数返回列表排序后的拷贝不会修改传入的列表
+# 函数的设计就应该像sorted函数一样尽可能不产生副作用
+
+list3 = sorted(list1, reverse=True)
+# 通过key关键字参数指定根据字符串长度进行排序而不是默认的字母表顺序
+
+list4 = sorted(list1, key=len)
+
+print(list1)
+print(list2)
+print(list3)
+print(list4)
+
+# 给列表对象发出排序消息直接在列表对象上进行排序
+list1.sort(reverse=True)
+print(list1)
+
+ +

生成式和生成器

+ +

元组

+ +

集合

+ +

字典

+ +
+

Friendly reminder: This series are my own study notes of Python-100-Days. I do not own copyright of some of the content. Nor is this a good tutorial of Python. I really appreciate it if you notice any mistakes or errors and tell me. Sorry that things copied from Python-100-Days will not be noted due to huge workload. The whole series are not allowed to be reproduced. View python-learning-readme.

+:ET \ No newline at end of file diff --git a/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/79/b86a10903fba21dce19980f797a164b194f1ea6d5d0b728eef4e159b8f0ff6 b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/79/b86a10903fba21dce19980f797a164b194f1ea6d5d0b728eef4e159b8f0ff6 new file mode 100644 index 00000000000..127f1db57b3 --- /dev/null +++ b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/79/b86a10903fba21dce19980f797a164b194f1ea6d5d0b728eef4e159b8f0ff6 @@ -0,0 +1,46 @@ +I" 

赋值

+
1
+2
+3
+4
+
a = 10
+b = 3
+a += b # 相当于:a = a + b
+a *= a + 2 # 相当于:a = a * (a + 2)
+
+ +

输入input

+
1
+2
+3
+
a = int(input('a = ')) #读取为int类型
+f = float(input('请输入华氏温度: ')) #显示输入提示
+a, b = map(int, input().split()) #读取在同一行的多个数字
+
+ +

输出print

+
1
+2
+3
+4
+
print(a)
+print(a + b)
+print(a,b) #输出结果会以空格隔开
+print('%.1f' % a) #输出float型变量a,保留一位小数
+
+
+

Python格式化输出

+
+ + + +

Friendly reminder: This series are my own study notes of Python-100-Days. I do not own copyright of some of the content. Nor is this a good tutorial of Python. I really appreciate it if you notice any mistakes or errors and tell me. Sorry that things copied from Python-100-Days will not be noted due to huge workload. The whole series are not allowed to be reproduced. View python-learning-readme.

+:ET \ No newline at end of file diff --git a/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/88/1e186b464f24bf211e1e135b02a4a72beada863aee13b6a5ba863753e43413 b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/88/1e186b464f24bf211e1e135b02a4a72beada863aee13b6a5ba863753e43413 new file mode 100644 index 00000000000..8f35a563282 --- /dev/null +++ b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/88/1e186b464f24bf211e1e135b02a4a72beada863aee13b6a5ba863753e43413 @@ -0,0 +1,19 @@ +I"O

Friendly reminder: This series are my own study notes of Python-100-Days. I do not own copyright of some of the content. Nor is this a good tutorial of Python. I really appreciate it if you notice any mistakes or errors and tell me. Sorry that things copied from Python-100-Days will not be noted due to huge workload. The whole series are not allowed to be reproduced. View python-learning-readme.

+ +

Tools used:

+ + + +

Catalog:

+ + +:ET \ No newline at end of file diff --git a/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/88/faa68b702f9de90c6c437690a3605924fde8445269f610cff54fdae532dabf b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/88/faa68b702f9de90c6c437690a3605924fde8445269f610cff54fdae532dabf new file mode 100644 index 00000000000..04a2e91f87a --- /dev/null +++ b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/88/faa68b702f9de90c6c437690a3605924fde8445269f610cff54fdae532dabf @@ -0,0 +1,453 @@ +I"t

本节讲义 →

+ +

目录

+ + +

字符串

+ +
表示方法
+ +

由单引号或双引号括起来的单个或多个字符称为字符串,以三个双引号或单引号开头的字符串可以折行。

+ +
1
+2
+3
+4
+5
+6
+7
+8
+
s1 = 'hello, world!'
+s2 = "hello, world!"
+s3 = """
+hello,
+world!
+"""
+
+print(s1, s2, s3, end='')
+
+ +
转义
+ +

例如: \n表示换行,\'表示字符'\\表示字符\

+ +

\后面还可以跟一个八进制或者十六进制数来表示字符,例如\141\x61分别为八进制和十六进制的小写字母a

+ +

\后面还可以跟Unicode字符编码来表示字符(大大提高了可玩性)

+ +
1
+
print('\u00A2\u132B\u26BD\u2F5B\u3093') #¢ጫ⚽⽛ん
+
+ +

若不希望转义,可以通过在字符串的最前面加上字母r来实现。

+ +
1
+
s1 = r'\'hello, world!\''   # \'hello, world!\'
+
+ +
字符串的运算
+ +

Python为字符串类型提供了非常丰富的运算符:

+ +
1
+2
+3
+4
+5
+6
+7
+8
+9
+10
+11
+12
+13
+14
+15
+16
+17
+18
+19
+20
+21
+22
+23
+24
+
s1 = 'hello ' * 3
+print(s1) # hello hello hello
+
+s2 = 'world'
+s1 += s2
+print(s1) # hello hello hello world
+
+print('ll' in s1) # True
+print('good' in s1) # False
+
+
+str2 = 'abc123456'
+
+# 从字符串中取出指定位置的字符(下标运算)
+print(str2[2]) # c
+
+# 字符串切片(从指定的开始索引到指定的结束索引)
+#index 从 0 至 n-1,str2[l:r] 包含 l 不包含 r
+print(str2[2:5]) # c12
+print(str2[2:]) # c123456
+print(str2[2::2]) # c246
+print(str2[::2]) # ac246
+print(str2[::-1]) # 654321cba
+print(str2[-3:-1]) # 45
+
+ +

在Python中,我们还可以通过一系列的方法来完成对字符串的处理:

+
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
+
str1 = 'hello, world!'
+
+# 通过内置函数len计算字符串的长度
+print(len(str1)) # 13
+
+# 获得字符串首字母大写的拷贝
+print(str1.capitalize()) # Hello, world!
+
+# 获得字符串每个单词首字母大写的拷贝
+print(str1.title()) # Hello, World!
+
+# 获得字符串变大写后的拷贝
+print(str1.upper()) # HELLO, WORLD!
+
+# 从字符串中查找子串所在位置
+print(str1.find('or')) # 8
+print(str1.find('shit')) # -1
+# 与find类似但找不到子串时会引发异常
+# print(str1.index('or'))
+# print(str1.index('shit'))
+
+# 检查字符串是否以指定的字符串开头
+print(str1.startswith('He')) # False
+print(str1.startswith('hel')) # True
+
+# 检查字符串是否以指定的字符串结尾
+print(str1.endswith('!')) # True
+
+# 将字符串以指定的宽度居中并在两侧填充指定的字符
+print(str1.center(50, '*'))
+# 将字符串以指定的宽度靠右放置左侧填充指定的字符
+print(str1.rjust(50, ' '))
+
+
+str2 = 'abc123456'
+# 检查字符串是否由数字构成
+print(str2.isdigit())  # False
+
+# 检查字符串是否以字母构成
+print(str2.isalpha())  # False
+
+# 检查字符串是否以数字和字母构成
+print(str2.isalnum())  # True
+
+
+str3 = '  jackfrued@126.com '
+print(str3)
+
+# 获得字符串修剪左右两侧空格之后的拷贝
+print(str3.strip())
+
+ +

我们之前讲过,可以用下面的方式来格式化输出字符串:

+ +
1
+2
+
a, b = 5, 10
+print('%d * %d = %d' % (a, b, a * b))
+
+ +

当然,我们也可以用字符串提供的方法来完成字符串的格式:

+ +
1
+2
+
a, b = 5, 10
+print('{0} * {1} = {2}'.format(a, b, a * b))
+
+ +

Python 3.6以后,格式化字符串还有更为简洁的书写方式,就是在字符串前加上字母f,我们可以使用下面的语法糖来简化上面的代码。

+ +
1
+2
+
a, b = 5, 10
+print(f'{a} * {b} = {a * b}')
+
+ +

列表

+ +

列表(list),与字符串类似,也是一种结构化的、非标量类型,它是值的有序序列,每个值都可以通过索引进行标识,定义列表可以将列表的元素放在[]中,多个元素用,进行分隔,可以使用for循环对列表元素进行遍历,也可以使用[][:]运算符取出列表中的一个或多个元素

+ +

下面的代码演示了如何定义列表、如何遍历列表以及列表的下标运算。

+ +
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
+
list1 = [1, 3, 5, 7, 100]
+print(list1) # [1, 3, 5, 7, 100]
+
+# 乘号表示列表元素的重复
+list2 = ['hello'] * 3
+print(list2) # ['hello', 'hello', 'hello']
+
+# 计算列表长度(元素个数)
+print(len(list1)) # 5
+
+# 下标(索引)运算
+print(list1[0]) # 1
+print(list1[4]) # 100
+
+# print(list1[5])  # IndexError: list index out of range
+print(list1[-1]) # 100
+print(list1[-3]) # 5
+
+list1[2] = 300
+print(list1) # [1, 3, 300, 7, 100]
+
+# 通过循环用下标遍历列表元素
+for index in range(len(list1)):
+    print(list1[index])
+
+# 通过for循环遍历列表元素
+for elem in list1:
+    print(elem)
+
+# 通过enumerate函数处理列表之后再遍历可以同时获得元素索引和值
+for index, elem in enumerate(list1):
+    print(index, elem)
+
+ +

下面的代码演示了如何向列表中添加元素以及如何从列表中移除元素。

+ +
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
+
list1 = [1, 3, 5, 7, 100]
+
+# 添加元素
+list1.append(200)
+list1.insert(1, 400)
+
+# 合并两个列表
+# list1.extend([1000, 2000])
+list1 += [1000, 2000]
+print(list1) # [1, 400, 3, 5, 7, 100, 200, 1000, 2000]
+print(len(list1)) # 9
+
+# 先通过成员运算判断元素是否在列表中,如果存在就删除该元素
+if 3 in list1:
+	list1.remove(3)
+if 1234 in list1:
+    list1.remove(1234)
+print(list1) # [1, 400, 5, 7, 100, 200, 1000, 2000]
+
+# 从指定的位置删除元素
+list1.pop(0)
+list1.pop(len(list1) - 1)
+print(list1) # [400, 5, 7, 100, 200, 1000]
+
+# 清空列表元素
+list1.clear()
+print(list1) # []
+
+ +

和字符串一样,列表也可以做切片操作,通过切片操作我们可以实现对列表的复制或者将列表中的一部分取出来创建出新的列表,代码如下所示。

+ +
1
+2
+3
+4
+5
+6
+7
+8
+9
+10
+11
+12
+13
+14
+15
+16
+
fruits = ['grape', 'apple', 'strawberry', 'waxberry']
+fruits += ['pitaya', 'pear', 'mango']
+
+# 列表切片
+fruits2 = fruits[1:4]
+print(fruits2) # apple strawberry waxberry
+
+# 可以通过完整切片操作来复制列表
+fruits3 = fruits[:]
+print(fruits3) # ['grape', 'apple', 'strawberry', 'waxberry', 'pitaya', 'pear', 'mango']
+fruits4 = fruits[-3:-1]
+print(fruits4) # ['pitaya', 'pear']
+
+# 可以通过反向切片操作来获得倒转后的列表的拷贝
+fruits5 = fruits[::-1]
+print(fruits5) # ['mango', 'pear', 'pitaya', 'waxberry', 'strawberry', 'apple', 'grape']
+
+ +

下面的代码实现了对列表的排序操作。

+ +
1
+2
+3
+4
+5
+6
+7
+8
+9
+10
+11
+12
+13
+14
+15
+16
+17
+18
+19
+
list1 = ['orange', 'apple', 'zoo', 'internationalization', 'blueberry']
+
+list2 = sorted(list1)
+# sorted函数返回列表排序后的拷贝不会修改传入的列表
+# 函数的设计就应该像sorted函数一样尽可能不产生副作用
+
+list3 = sorted(list1, reverse=True)
+# 通过key关键字参数指定根据字符串长度进行排序而不是默认的字母表顺序
+
+list4 = sorted(list1, key=len)
+
+print(list1)
+print(list2)
+print(list3)
+print(list4)
+
+# 给列表对象发出排序消息直接在列表对象上进行排序
+list1.sort(reverse=True)
+print(list1)
+
+ +

生成式和生成器

+ +

元组

+ +

集合

+ +

字典

+ +
+

Friendly reminder: This series are my own study notes of Python-100-Days. I do not own copyright of some of the content. Nor is this a good tutorial of Python. I really appreciate it if you notice any mistakes or errors and tell me. Sorry that things copied from Python-100-Days will not be noted due to huge workload. The whole series are not allowed to be reproduced. View python-learning-readme.

+:ET \ No newline at end of file diff --git a/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/96/cd90a15c43e26ab0db961eed13ad789f365fa35a9e8d514c74e2c20770e72f b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/96/cd90a15c43e26ab0db961eed13ad789f365fa35a9e8d514c74e2c20770e72f new file mode 100644 index 00000000000..4367f1d02cf --- /dev/null +++ b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/96/cd90a15c43e26ab0db961eed13ad789f365fa35a9e8d514c74e2c20770e72f @@ -0,0 +1,255 @@ +I"&

本节讲义 $rarr

+ +

字符串

+ +
表示方法
+ +

由单引号或双引号括起来的单个或多个字符称为字符串,以三个双引号或单引号开头的字符串可以折行。

+ +
s1 = 'hello, world!'
+s2 = "hello, world!"
+s3 = """
+hello,
+world!
+"""
+
+print(s1, s2, s3, end='')
+
+ +
转义
+ +

例如: \n表示换行,\'表示字符'\\表示字符\

+ +

\后面还可以跟一个八进制或者十六进制数来表示字符,例如\141\x61分别为八进制和十六进制的小写字母a

+ +

\后面还可以跟Unicode字符编码来表示字符(大大提高了可玩性)

+
print('\u00A2\u132B\u26BD\u2F5B\u3093') #¢ጫ⚽⽛ん
+
+ +

若不希望转义,可以通过在字符串的最前面加上字母r来实现。

+
s1 = r'\'hello, world!\''   # \'hello, world!\'
+
+ +
字符串的运算
+ +

Python为字符串类型提供了非常丰富的运算符:

+ +
s1 = 'hello ' * 3
+print(s1) # hello hello hello
+
+s2 = 'world'
+s1 += s2
+print(s1) # hello hello hello world
+
+print('ll' in s1) # True
+print('good' in s1) # False
+
+
+str2 = 'abc123456'
+
+# 从字符串中取出指定位置的字符(下标运算)
+print(str2[2]) # c
+
+# 字符串切片(从指定的开始索引到指定的结束索引)
+#index 从 0 至 n-1,str2[l:r] 包含 l 不包含 r
+print(str2[2:5]) # c12
+print(str2[2:]) # c123456
+print(str2[2::2]) # c246
+print(str2[::2]) # ac246
+print(str2[::-1]) # 654321cba
+print(str2[-3:-1]) # 45
+
+ +

在Python中,我们还可以通过一系列的方法来完成对字符串的处理:

+
str1 = 'hello, world!'
+
+# 通过内置函数len计算字符串的长度
+print(len(str1)) # 13
+
+# 获得字符串首字母大写的拷贝
+print(str1.capitalize()) # Hello, world!
+
+# 获得字符串每个单词首字母大写的拷贝
+print(str1.title()) # Hello, World!
+
+# 获得字符串变大写后的拷贝
+print(str1.upper()) # HELLO, WORLD!
+
+# 从字符串中查找子串所在位置
+print(str1.find('or')) # 8
+print(str1.find('shit')) # -1
+# 与find类似但找不到子串时会引发异常
+# print(str1.index('or'))
+# print(str1.index('shit'))
+
+# 检查字符串是否以指定的字符串开头
+print(str1.startswith('He')) # False
+print(str1.startswith('hel')) # True
+
+# 检查字符串是否以指定的字符串结尾
+print(str1.endswith('!')) # True
+
+# 将字符串以指定的宽度居中并在两侧填充指定的字符
+print(str1.center(50, '*'))
+# 将字符串以指定的宽度靠右放置左侧填充指定的字符
+print(str1.rjust(50, ' '))
+
+
+str2 = 'abc123456'
+# 检查字符串是否由数字构成
+print(str2.isdigit())  # False
+
+# 检查字符串是否以字母构成
+print(str2.isalpha())  # False
+
+# 检查字符串是否以数字和字母构成
+print(str2.isalnum())  # True
+
+
+str3 = '  jackfrued@126.com '
+print(str3)
+
+# 获得字符串修剪左右两侧空格之后的拷贝
+print(str3.strip())
+
+ +

我们之前讲过,可以用下面的方式来格式化输出字符串:

+
a, b = 5, 10
+print('%d * %d = %d' % (a, b, a * b))
+
+ +

当然,我们也可以用字符串提供的方法来完成字符串的格式:

+
a, b = 5, 10
+print('{0} * {1} = {2}'.format(a, b, a * b))
+
+ +

Python 3.6以后,格式化字符串还有更为简洁的书写方式,就是在字符串前加上字母f,我们可以使用下面的语法糖来简化上面的代码。

+
a, b = 5, 10
+print(f'{a} * {b} = {a * b}')
+
+ +

列表 List

+ +

列表(list),与字符串类似,也是一种结构化的、非标量类型,它是值的有序序列,每个值都可以通过索引进行标识,定义列表可以将列表的元素放在[]中,多个元素用,进行分隔,可以使用for循环对列表元素进行遍历,也可以使用[][:]运算符取出列表中的一个或多个元素

+ +

下面的代码演示了如何定义列表、如何遍历列表以及列表的下标运算。

+ +
list1 = [1, 3, 5, 7, 100]
+print(list1) # [1, 3, 5, 7, 100]
+
+# 乘号表示列表元素的重复
+list2 = ['hello'] * 3
+print(list2) # ['hello', 'hello', 'hello']
+
+# 计算列表长度(元素个数)
+print(len(list1)) # 5
+
+# 下标(索引)运算
+print(list1[0]) # 1
+print(list1[4]) # 100
+
+# print(list1[5])  # IndexError: list index out of range
+print(list1[-1]) # 100
+print(list1[-3]) # 5
+
+list1[2] = 300
+print(list1) # [1, 3, 300, 7, 100]
+
+# 通过循环用下标遍历列表元素
+for index in range(len(list1)):
+    print(list1[index])
+
+# 通过for循环遍历列表元素
+for elem in list1:
+    print(elem)
+
+# 通过enumerate函数处理列表之后再遍历可以同时获得元素索引和值
+for index, elem in enumerate(list1):
+    print(index, elem)
+
+ +

下面的代码演示了如何向列表中添加元素以及如何从列表中移除元素。

+ +
list1 = [1, 3, 5, 7, 100]
+
+# 添加元素
+list1.append(200)
+list1.insert(1, 400)
+
+# 合并两个列表
+# list1.extend([1000, 2000])
+list1 += [1000, 2000]
+print(list1) # [1, 400, 3, 5, 7, 100, 200, 1000, 2000]
+print(len(list1)) # 9
+
+# 先通过成员运算判断元素是否在列表中,如果存在就删除该元素
+if 3 in list1:
+	list1.remove(3)
+if 1234 in list1:
+    list1.remove(1234)
+print(list1) # [1, 400, 5, 7, 100, 200, 1000, 2000]
+
+# 从指定的位置删除元素
+list1.pop(0)
+list1.pop(len(list1) - 1)
+print(list1) # [400, 5, 7, 100, 200, 1000]
+
+# 清空列表元素
+list1.clear()
+print(list1) # []
+
+ +

和字符串一样,列表也可以做切片操作,通过切片操作我们可以实现对列表的复制或者将列表中的一部分取出来创建出新的列表,代码如下所示。

+ +
fruits = ['grape', 'apple', 'strawberry', 'waxberry']
+fruits += ['pitaya', 'pear', 'mango']
+
+# 列表切片
+fruits2 = fruits[1:4]
+print(fruits2) # apple strawberry waxberry
+
+# 可以通过完整切片操作来复制列表
+fruits3 = fruits[:]
+print(fruits3) # ['grape', 'apple', 'strawberry', 'waxberry', 'pitaya', 'pear', 'mango']
+fruits4 = fruits[-3:-1]
+print(fruits4) # ['pitaya', 'pear']
+
+# 可以通过反向切片操作来获得倒转后的列表的拷贝
+fruits5 = fruits[::-1]
+print(fruits5) # ['mango', 'pear', 'pitaya', 'waxberry', 'strawberry', 'apple', 'grape']
+
+ +

下面的代码实现了对列表的排序操作。

+ +
list1 = ['orange', 'apple', 'zoo', 'internationalization', 'blueberry']
+
+list2 = sorted(list1)
+# sorted函数返回列表排序后的拷贝不会修改传入的列表
+# 函数的设计就应该像sorted函数一样尽可能不产生副作用
+
+list3 = sorted(list1, reverse=True)
+# 通过key关键字参数指定根据字符串长度进行排序而不是默认的字母表顺序
+
+list4 = sorted(list1, key=len)
+
+print(list1)
+print(list2)
+print(list3)
+print(list4)
+
+# 给列表对象发出排序消息直接在列表对象上进行排序
+list1.sort(reverse=True)
+print(list1)
+
+ +

生成式和生成器

+ +

元组

+ +

集合

+ +

字典

+ +
+

Friendly reminder: This series are my own study notes of Python-100-Days. I do not own copyright of some of the content. Nor is this a good tutorial of Python. I really appreciate it if you notice any mistakes or errors and tell me. Sorry that things copied from Python-100-Days will not be noted due to huge workload. The whole series are not allowed to be reproduced. View python-learning-readme.

+:ET \ No newline at end of file diff --git a/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/bc/cf53a441d255e403111202b3ed3fe2f891cee97a750218df4a0a097009c552 b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/bc/cf53a441d255e403111202b3ed3fe2f891cee97a750218df4a0a097009c552 new file mode 100644 index 00000000000..1f1b1e17797 --- /dev/null +++ b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/bc/cf53a441d255e403111202b3ed3fe2f891cee97a750218df4a0a097009c552 @@ -0,0 +1,39 @@ +I"

函数

+ +
定义函数
+ +

使用def关键字来定义函数,通过return关键字来返回一个值,举个栗子:

+ +
def fac(num):   # 阶乘,实际上Python的math模块中有factorial函数,这里仅做演示
+    result = 1
+    for n in range(1, num + 1):
+        result *= n
+    return result
+
+ + + +

模块

+ +

由于Python没有函数重载的概念,那么后面的定义会覆盖之前的定义,也就意味着两个函数同名函数实际上只有一个是存在的。

+ +
def foo():
+    print('hello, world!')
+
+def foo():
+    print('goodbye, world!')
+
+# 下面的代码会输出什么呢?
+foo()   # goodbye, world!
+
+ +
+

整篇除了函数的定义之外都不太懂,看 课件

+
+ +
+

Friendly reminder: This series are my own study notes of Python-100-Days. I do not own copyright of some of the content. Nor is this a good tutorial of Python. I really appreciate it if you notice any mistakes or errors and tell me. Sorry that things copied from Python-100-Days will not be noted due to huge workload. The whole series are not allowed to be reproduced. View python-learning-readme.

+:ET \ No newline at end of file diff --git a/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/bc/dd8a5deafe1ef81a841dbd68ccce817763fd4324f08ca600bd434f53653379 b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/bc/dd8a5deafe1ef81a841dbd68ccce817763fd4324f08ca600bd434f53653379 new file mode 100644 index 00000000000..688ebe0d1bb --- /dev/null +++ b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/bc/dd8a5deafe1ef81a841dbd68ccce817763fd4324f08ca600bd434f53653379 @@ -0,0 +1,2 @@ +I"

本节讲义 ->

+:ET \ No newline at end of file diff --git a/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/c8/eca608adf23904bb1d18f9910ec19fcef73b718e6398cacfe1fae48c012485 b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/c8/eca608adf23904bb1d18f9910ec19fcef73b718e6398cacfe1fae48c012485 new file mode 100644 index 00000000000..013ef7fe6ad --- /dev/null +++ b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/c8/eca608adf23904bb1d18f9910ec19fcef73b718e6398cacfe1fae48c012485 @@ -0,0 +1,150 @@ +I"

注释

+ +

单行注释 - 以#和空格开头的部分 +多行注释 - 三个引号开头,三个引号结尾 (单引号与双引号仅有微小区别,可以忽略)

+ +

单行注释

+
1
+
a = b + c # 这是一条注释
+
+

多行注释

+
1
+2
+3
+4
+5
+6
+
print("下面是多行注释")
+"""
+print("注释")
+print("注释不会编译")
+print("可以看到print的字体颜色改变了")
+"""
+
+ +

数据类型

+ +

常用数据类型:

+ + + +

变量

+ + + +

运算符

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
运算符描述
[] [:]下标,切片
**指数
~ + -按位取反, 正负号
* / % //乘,除,模,整除
+ -加,减
>> <<右移,左移
&按位与
^ \|按位异或,按位或
<= < > >=小于等于,小于,大于,大于等于
== !=等于,不等于
is is not身份运算符
in not in成员运算符
not or and逻辑运算符
= += -= *= /= %= //= **= &= |= ^= >>= <<=(复合)赋值运算符
+ +
+

说明: 在实际开发中,如果搞不清楚运算符的优先级,可以使用括号来确保运算的执行顺序。

+
+ +
+ +

Friendly reminder: This series are my own study notes of Python-100-Days. I do not own copyright of some of the content. Nor is this a good tutorial of Python. I really appreciate it if you notice any mistakes or errors and tell me. Sorry that things copied from Python-100-Days will not be noted due to huge workload. The whole series are not allowed to be reproduced. View python-learning-readme.

+:ET \ No newline at end of file diff --git a/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/cb/50172a143ca00e637885f846be8ab063cb4cb250e292ad4ed862dce6812792 b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/cb/50172a143ca00e637885f846be8ab063cb4cb250e292ad4ed862dce6812792 new file mode 100644 index 00000000000..2403d389b5f --- /dev/null +++ b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/cb/50172a143ca00e637885f846be8ab063cb4cb250e292ad4ed862dce6812792 @@ -0,0 +1,148 @@ +I"Q

注释

+ +

单行注释 - 以#和空格开头的部分 +多行注释 - 三个引号开头,三个引号结尾 (单引号与双引号仅有微小区别,可以忽略)

+ +

单行注释

+
1
+
a = b + c # 这是一条注释
+
+

多行注释

+
1
+2
+3
+4
+5
+6
+
print("下面是多行注释")
+"""
+print("注释")
+print("注释不会编译")
+print("可以看到print的字体颜色改变了")
+"""
+
+ +

数据类型

+ +

常用数据类型:

+ + + +

变量

+ + + +

运算符

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
运算符描述
[] [:]下标,切片
**指数
~ + -按位取反, 正负号
* / % //乘,除,模,整除
+ -加,减
>> <<右移,左移
&按位与
^ \|按位异或,按位或
<= < > >=小于等于,小于,大于,大于等于
== !=等于,不等于
is is not身份运算符
in not in成员运算符
not or and逻辑运算符
= += -= *= /= %= //= **= &= |= ^= >>= <<=(复合)赋值运算符
+ +
+

说明: 在实际开发中,如果搞不清楚运算符的优先级,可以使用括号来确保运算的执行顺序。

+
+ +

Friendly reminder: This series are my own study notes of Python-100-Days. I do not own copyright of some of the content. Nor is this a good tutorial of Python. But if you find mistakes or errors in the notes, please tell me. I really appreciate that. The whole series are not allowed to be reproduced. View python-learning-readme/.

+:ET \ No newline at end of file diff --git a/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/d0/8e9a0c9f35298f8db1bd039d2f5ccaf176d67b96a80968acaf2e072928f96e b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/d0/8e9a0c9f35298f8db1bd039d2f5ccaf176d67b96a80968acaf2e072928f96e new file mode 100644 index 00000000000..a6e40423e09 --- /dev/null +++ b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/d0/8e9a0c9f35298f8db1bd039d2f5ccaf176d67b96a80968acaf2e072928f96e @@ -0,0 +1,82 @@ +I"

在Python中同样有两种循环:for-in循环和while循环

+ +

for-in 循环

+
1
+2
+3
+4
+5
+6
+7
+8
+
"""
+用for循环实现1~100求和
+"""
+
+sum = 0
+for x in range(101):
+    sum += x
+print(sum)
+
+

其中,range()函数可以更灵活:

+ + + +

注意: range(n)产生的是0n-1的序列!

+ +

while 循环

+ +
1
+2
+3
+4
+5
+6
+7
+8
+9
+10
+
"""
+用while循环实现1~100求和
+"""
+
+i = 1
+sum = 0
+while i <= 100 :
+    i = i + 1
+    sum =sum + i
+print(sum)
+
+ +

while循环中,可以使用break强制结束循环,以及使用continue函数直接跳到下一次循环的开始。

+ +

循环的嵌套

+

举个栗子,九九乘法表

+
1
+2
+3
+4
+5
+6
+7
+8
+
"""
+九九乘法表
+"""
+
+for i in range(1, 9 + 1):
+    for j in range(1, i + 1):
+        print('%d*%d=%d' % (i, j, i * j), end='\t')
+    print()
+
+
+

关于逗号,的作用,可以参考这篇文章,应该是对的

+
+ +
+

Friendly reminder: This series are my own study notes of Python-100-Days. I do not own copyright of some of the content. Nor is this a good tutorial of Python. I really appreciate it if you notice any mistakes or errors and tell me. Sorry that things copied from Python-100-Days will not be noted due to huge workload. The whole series are not allowed to be reproduced. View python-learning-readme.

+:ET \ No newline at end of file diff --git a/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/d9/b5ab6cec9fad149bd31eb7db62afa0b2d95159590ce066db7e4bf311c7d7ef b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/d9/b5ab6cec9fad149bd31eb7db62afa0b2d95159590ce066db7e4bf311c7d7ef new file mode 100644 index 00000000000..dc6c112b34e --- /dev/null +++ b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/d9/b5ab6cec9fad149bd31eb7db62afa0b2d95159590ce066db7e4bf311c7d7ef @@ -0,0 +1,453 @@ +I"t

本节讲义 →

+ +

目录

+ + +

字符串

+ +
表示方法
+ +

由单引号或双引号括起来的单个或多个字符称为字符串,以三个双引号或单引号开头的字符串可以折行。

+ +
1
+2
+3
+4
+5
+6
+7
+8
+
s1 = 'hello, world!'
+s2 = "hello, world!"
+s3 = """
+hello,
+world!
+"""
+
+print(s1, s2, s3, end='')
+
+ +
转义
+ +

例如: \n表示换行,\'表示字符'\\表示字符\

+ +

\后面还可以跟一个八进制或者十六进制数来表示字符,例如\141\x61分别为八进制和十六进制的小写字母a

+ +

\后面还可以跟Unicode字符编码来表示字符(大大提高了可玩性)

+ +
1
+
print('\u00A2\u132B\u26BD\u2F5B\u3093') #¢ጫ⚽⽛ん
+
+ +

若不希望转义,可以通过在字符串的最前面加上字母r来实现。

+ +
1
+
s1 = r'\'hello, world!\''   # \'hello, world!\'
+
+ +
字符串的运算
+ +

Python为字符串类型提供了非常丰富的运算符:

+ +
1
+2
+3
+4
+5
+6
+7
+8
+9
+10
+11
+12
+13
+14
+15
+16
+17
+18
+19
+20
+21
+22
+23
+24
+
s1 = 'hello ' * 3
+print(s1) # hello hello hello
+
+s2 = 'world'
+s1 += s2
+print(s1) # hello hello hello world
+
+print('ll' in s1) # True
+print('good' in s1) # False
+
+
+str2 = 'abc123456'
+
+# 从字符串中取出指定位置的字符(下标运算)
+print(str2[2]) # c
+
+# 字符串切片(从指定的开始索引到指定的结束索引)
+#index 从 0 至 n-1,str2[l:r] 包含 l 不包含 r
+print(str2[2:5]) # c12
+print(str2[2:]) # c123456
+print(str2[2::2]) # c246
+print(str2[::2]) # ac246
+print(str2[::-1]) # 654321cba
+print(str2[-3:-1]) # 45
+
+ +

在Python中,我们还可以通过一系列的方法来完成对字符串的处理:

+
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
+
str1 = 'hello, world!'
+
+# 通过内置函数len计算字符串的长度
+print(len(str1)) # 13
+
+# 获得字符串首字母大写的拷贝
+print(str1.capitalize()) # Hello, world!
+
+# 获得字符串每个单词首字母大写的拷贝
+print(str1.title()) # Hello, World!
+
+# 获得字符串变大写后的拷贝
+print(str1.upper()) # HELLO, WORLD!
+
+# 从字符串中查找子串所在位置
+print(str1.find('or')) # 8
+print(str1.find('shit')) # -1
+# 与find类似但找不到子串时会引发异常
+# print(str1.index('or'))
+# print(str1.index('shit'))
+
+# 检查字符串是否以指定的字符串开头
+print(str1.startswith('He')) # False
+print(str1.startswith('hel')) # True
+
+# 检查字符串是否以指定的字符串结尾
+print(str1.endswith('!')) # True
+
+# 将字符串以指定的宽度居中并在两侧填充指定的字符
+print(str1.center(50, '*'))
+# 将字符串以指定的宽度靠右放置左侧填充指定的字符
+print(str1.rjust(50, ' '))
+
+
+str2 = 'abc123456'
+# 检查字符串是否由数字构成
+print(str2.isdigit())  # False
+
+# 检查字符串是否以字母构成
+print(str2.isalpha())  # False
+
+# 检查字符串是否以数字和字母构成
+print(str2.isalnum())  # True
+
+
+str3 = '  jackfrued@126.com '
+print(str3)
+
+# 获得字符串修剪左右两侧空格之后的拷贝
+print(str3.strip())
+
+ +

我们之前讲过,可以用下面的方式来格式化输出字符串:

+ +
1
+2
+
a, b = 5, 10
+print('%d * %d = %d' % (a, b, a * b))
+
+ +

当然,我们也可以用字符串提供的方法来完成字符串的格式:

+ +
1
+2
+
a, b = 5, 10
+print('{0} * {1} = {2}'.format(a, b, a * b))
+
+ +

Python 3.6以后,格式化字符串还有更为简洁的书写方式,就是在字符串前加上字母f,我们可以使用下面的语法糖来简化上面的代码。

+ +
1
+2
+
a, b = 5, 10
+print(f'{a} * {b} = {a * b}')
+
+ +

列表

+ +

列表(list),与字符串类似,也是一种结构化的、非标量类型,它是值的有序序列,每个值都可以通过索引进行标识,定义列表可以将列表的元素放在[]中,多个元素用,进行分隔,可以使用for循环对列表元素进行遍历,也可以使用[][:]运算符取出列表中的一个或多个元素

+ +

下面的代码演示了如何定义列表、如何遍历列表以及列表的下标运算。

+ +
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
+
list1 = [1, 3, 5, 7, 100]
+print(list1) # [1, 3, 5, 7, 100]
+
+# 乘号表示列表元素的重复
+list2 = ['hello'] * 3
+print(list2) # ['hello', 'hello', 'hello']
+
+# 计算列表长度(元素个数)
+print(len(list1)) # 5
+
+# 下标(索引)运算
+print(list1[0]) # 1
+print(list1[4]) # 100
+
+# print(list1[5])  # IndexError: list index out of range
+print(list1[-1]) # 100
+print(list1[-3]) # 5
+
+list1[2] = 300
+print(list1) # [1, 3, 300, 7, 100]
+
+# 通过循环用下标遍历列表元素
+for index in range(len(list1)):
+    print(list1[index])
+
+# 通过for循环遍历列表元素
+for elem in list1:
+    print(elem)
+
+# 通过enumerate函数处理列表之后再遍历可以同时获得元素索引和值
+for index, elem in enumerate(list1):
+    print(index, elem)
+
+ +

下面的代码演示了如何向列表中添加元素以及如何从列表中移除元素。

+ +
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
+
list1 = [1, 3, 5, 7, 100]
+
+# 添加元素
+list1.append(200)
+list1.insert(1, 400)
+
+# 合并两个列表
+# list1.extend([1000, 2000])
+list1 += [1000, 2000]
+print(list1) # [1, 400, 3, 5, 7, 100, 200, 1000, 2000]
+print(len(list1)) # 9
+
+# 先通过成员运算判断元素是否在列表中,如果存在就删除该元素
+if 3 in list1:
+	list1.remove(3)
+if 1234 in list1:
+    list1.remove(1234)
+print(list1) # [1, 400, 5, 7, 100, 200, 1000, 2000]
+
+# 从指定的位置删除元素
+list1.pop(0)
+list1.pop(len(list1) - 1)
+print(list1) # [400, 5, 7, 100, 200, 1000]
+
+# 清空列表元素
+list1.clear()
+print(list1) # []
+
+ +

和字符串一样,列表也可以做切片操作,通过切片操作我们可以实现对列表的复制或者将列表中的一部分取出来创建出新的列表,代码如下所示。

+ +
1
+2
+3
+4
+5
+6
+7
+8
+9
+10
+11
+12
+13
+14
+15
+16
+
fruits = ['grape', 'apple', 'strawberry', 'waxberry']
+fruits += ['pitaya', 'pear', 'mango']
+
+# 列表切片
+fruits2 = fruits[1:4]
+print(fruits2) # apple strawberry waxberry
+
+# 可以通过完整切片操作来复制列表
+fruits3 = fruits[:]
+print(fruits3) # ['grape', 'apple', 'strawberry', 'waxberry', 'pitaya', 'pear', 'mango']
+fruits4 = fruits[-3:-1]
+print(fruits4) # ['pitaya', 'pear']
+
+# 可以通过反向切片操作来获得倒转后的列表的拷贝
+fruits5 = fruits[::-1]
+print(fruits5) # ['mango', 'pear', 'pitaya', 'waxberry', 'strawberry', 'apple', 'grape']
+
+ +

下面的代码实现了对列表的排序操作。

+ +
1
+2
+3
+4
+5
+6
+7
+8
+9
+10
+11
+12
+13
+14
+15
+16
+17
+18
+19
+
list1 = ['orange', 'apple', 'zoo', 'internationalization', 'blueberry']
+
+list2 = sorted(list1)
+# sorted函数返回列表排序后的拷贝不会修改传入的列表
+# 函数的设计就应该像sorted函数一样尽可能不产生副作用
+
+list3 = sorted(list1, reverse=True)
+# 通过key关键字参数指定根据字符串长度进行排序而不是默认的字母表顺序
+
+list4 = sorted(list1, key=len)
+
+print(list1)
+print(list2)
+print(list3)
+print(list4)
+
+# 给列表对象发出排序消息直接在列表对象上进行排序
+list1.sort(reverse=True)
+print(list1)
+
+ +

生成式和生成器

+ +

元组

+ +

集合

+ +

字典

+ +
+

Friendly reminder: This series are my own study notes of Python-100-Days. I do not own copyright of some of the content. Nor is this a good tutorial of Python. I really appreciate it if you notice any mistakes or errors and tell me. Sorry that things copied from Python-100-Days will not be noted due to huge workload. The whole series are not allowed to be reproduced. View python-learning-readme.

+:ET \ No newline at end of file diff --git a/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/da/b09ed8a154161612e04afdc9f1999b53390fe9439960e76f15ac671e41557e b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/da/b09ed8a154161612e04afdc9f1999b53390fe9439960e76f15ac671e41557e new file mode 100644 index 00000000000..3f72659837e --- /dev/null +++ b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/da/b09ed8a154161612e04afdc9f1999b53390fe9439960e76f15ac671e41557e @@ -0,0 +1,2 @@ +I"

本节讲义 →

+:ET \ No newline at end of file diff --git a/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/e6/3c57c8b4d19a837b286788b30abc83c634e7c0b02117460c32af3b94bc0a9d b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/e6/3c57c8b4d19a837b286788b30abc83c634e7c0b02117460c32af3b94bc0a9d new file mode 100644 index 00000000000..ea932b9e563 --- /dev/null +++ b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/e6/3c57c8b4d19a837b286788b30abc83c634e7c0b02117460c32af3b94bc0a9d @@ -0,0 +1,2 @@ +I"

本节讲义 &rarr

+:ET \ No newline at end of file diff --git a/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/e6/7202aa0b32a0f8ba48b92b32bf107ce292bf219e963a88f3e05bf270b1ea4d b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/e6/7202aa0b32a0f8ba48b92b32bf107ce292bf219e963a88f3e05bf270b1ea4d new file mode 100644 index 00000000000..b44f1778e46 --- /dev/null +++ b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/e6/7202aa0b32a0f8ba48b92b32bf107ce292bf219e963a88f3e05bf270b1ea4d @@ -0,0 +1,31 @@ +I"

两分支 If-Else 语句

+
if username == 'admin' and password == '123456':
+    print('身份验证成功!')
+else:
+    print('身份验证失败!')
+
+ +

多分支 If-Elseif-Else 语句

+
x = float(input('x = '))
+if x > 1:
+    y = 3 * x - 5
+elif x >= -1:
+    y = x + 2
+else:
+    y = 5 * x + 3
+
+ +

悬空Else分支

+

使用pass命令执行“不执行任何操作”

+
x = float(input('x = '))
+if x > 1:
+    print(x)
+else
+    pass
+
+

Attention:尽量避免没有必要的嵌套,嵌套既影响可读性也增加bug出现概率

+ +

Attention: 同时注意,与Pascal不同,if和else语句末尾有冒号”:”

+ +

Friendly reminder: This series are my own study notes of Python-100-Days. I do not own copyright of some of the content. Nor is this a good tutorial of Python. I really appreciate it if you notice any mistakes or errors and tell me. Sorry that things copied from Python-100-Days will not be noted due to huge workload. The whole series are not allowed to be reproduced. View python-learning-readme.

+:ET \ No newline at end of file diff --git a/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/e8/d065ba4dd0b3f274f3079f8cb2a46505c910c97b0b874d75aff04d64b6e1ce b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/e8/d065ba4dd0b3f274f3079f8cb2a46505c910c97b0b874d75aff04d64b6e1ce new file mode 100644 index 00000000000..b1cce283f81 --- /dev/null +++ b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/e8/d065ba4dd0b3f274f3079f8cb2a46505c910c97b0b874d75aff04d64b6e1ce @@ -0,0 +1,52 @@ +I"

函数

+ +
定义函数
+ +

使用def关键字来定义函数,通过return关键字来返回一个值,举个栗子:

+ +
1
+2
+3
+4
+5
+
def fac(num):   # 阶乘,实际上Python的math模块中有factorial函数,这里仅做演示
+    result = 1
+    for n in range(1, num + 1):
+        result *= n
+    return result
+
+ + + +

模块

+ +

由于Python没有函数重载的概念,那么后面的定义会覆盖之前的定义,也就意味着两个函数同名函数实际上只有一个是存在的。

+ +
1
+2
+3
+4
+5
+6
+7
+8
+
def foo():
+    print('hello, world!')
+
+def foo():
+    print('goodbye, world!')
+
+# 下面的代码会输出什么呢?
+foo()   # goodbye, world!
+
+ +
+

整篇除了函数的定义之外都不太懂,看 课件

+
+ +
+

Friendly reminder: This series are my own study notes of Python-100-Days. I do not own copyright of some of the content. Nor is this a good tutorial of Python. I really appreciate it if you notice any mistakes or errors and tell me. Sorry that things copied from Python-100-Days will not be noted due to huge workload. The whole series are not allowed to be reproduced. View python-learning-readme.

+:ET \ No newline at end of file diff --git a/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/e9/7d1b313f2cc81e6a0dc63959b232c59e98452b42953419695d9f79dccbdb8e b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/e9/7d1b313f2cc81e6a0dc63959b232c59e98452b42953419695d9f79dccbdb8e new file mode 100644 index 00000000000..5feec2a618b --- /dev/null +++ b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/e9/7d1b313f2cc81e6a0dc63959b232c59e98452b42953419695d9f79dccbdb8e @@ -0,0 +1,2 @@ +I"

在Python中同样有两种循环:for-in循环和while循环

+:ET \ No newline at end of file diff --git a/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/f2/6eff824ca3b30418abc8afc8e08505f1e281553b6545c26884e9d4c60f2ce0 b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/f2/6eff824ca3b30418abc8afc8e08505f1e281553b6545c26884e9d4c60f2ce0 new file mode 100644 index 00000000000..7d5c4a466a8 --- /dev/null +++ b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/f2/6eff824ca3b30418abc8afc8e08505f1e281553b6545c26884e9d4c60f2ce0 @@ -0,0 +1,148 @@ +I"P

注释

+ +

单行注释 - 以#和空格开头的部分 +多行注释 - 三个引号开头,三个引号结尾 (单引号与双引号仅有微小区别,可以忽略)

+ +

单行注释

+
1
+
a = b + c # 这是一条注释
+
+

多行注释

+
1
+2
+3
+4
+5
+6
+
print("下面是多行注释")
+"""
+print("注释")
+print("注释不会编译")
+print("可以看到print的字体颜色改变了")
+"""
+
+ +

数据类型

+ +

常用数据类型:

+ + + +

变量

+ + + +

运算符

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
运算符描述
[] [:]下标,切片
**指数
~ + -按位取反, 正负号
* / % //乘,除,模,整除
+ -加,减
>> <<右移,左移
&按位与
^ \|按位异或,按位或
<= < > >=小于等于,小于,大于,大于等于
== !=等于,不等于
is is not身份运算符
in not in成员运算符
not or and逻辑运算符
= += -= *= /= %= //= **= &= |= ^= >>= <<=(复合)赋值运算符
+ +
+

说明: 在实际开发中,如果搞不清楚运算符的优先级,可以使用括号来确保运算的执行顺序。

+
+ +

Friendly reminder: This series are my own study notes of Python-100-Days. I do not own copyright of some of the content. Nor is this a good tutorial of Python. But if you find mistakes or errors in the notes, please tell me. I really appreciate that. The whole series are not allowed to be reproduced. View python-learning-readme.

+:ET \ No newline at end of file diff --git a/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/f7/e941076127616402732ef140aef2ce103feeee5bb63ec202fa886814c0e2af b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/f7/e941076127616402732ef140aef2ce103feeee5bb63ec202fa886814c0e2af new file mode 100644 index 00000000000..37007c84cc5 --- /dev/null +++ b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/f7/e941076127616402732ef140aef2ce103feeee5bb63ec202fa886814c0e2af @@ -0,0 +1,148 @@ +I"

注释

+ +

单行注释 - 以#和空格开头的部分 +多行注释 - 三个引号开头,三个引号结尾 (单引号与双引号仅有微小区别,可以忽略)

+ +

单行注释

+
1
+
a = b + c # 这是一条注释
+
+

多行注释

+
1
+2
+3
+4
+5
+6
+
print("下面是多行注释")
+"""
+print("注释")
+print("注释不会编译")
+print("可以看到print的字体颜色改变了")
+"""
+
+ +

数据类型

+ +

常用数据类型:

+ + + +

变量

+ + + +

运算符

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
运算符描述
[] [:]下标,切片
**指数
~ + -按位取反, 正负号
* / % //乘,除,模,整除
+ -加,减
>> <<右移,左移
&按位与
^ \|按位异或,按位或
<= < > >=小于等于,小于,大于,大于等于
== !=等于,不等于
is is not身份运算符
in not in成员运算符
not or and逻辑运算符
= += -= *= /= %= //= **= &= |= ^= >>= <<=(复合)赋值运算符
+ +
+

说明: 在实际开发中,如果搞不清楚运算符的优先级,可以使用括号来确保运算的执行顺序。

+
+ +

Friendly reminder: This series are my own study notes of Python-100-Days. I do not own copyright of some of the content. Nor is this a good tutorial of Python. I really appreciate it if you notice any mistakes or errors and tell me. Sorry that things copied from Python-100-Days will not be noted due to huge workload. The whole series are not allowed to be reproduced. View python-learning-readme.

+:ET \ No newline at end of file diff --git a/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/fd/85b957800aa99cc09024ca992ab938a3857380123ddeba79385ee4cd05808e b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/fd/85b957800aa99cc09024ca992ab938a3857380123ddeba79385ee4cd05808e new file mode 100644 index 00000000000..81197d2eafa --- /dev/null +++ b/.jekyll-cache/Jekyll/Cache/Jekyll--Converters--Markdown/fd/85b957800aa99cc09024ca992ab938a3857380123ddeba79385ee4cd05808e @@ -0,0 +1,48 @@ +I"

两分支 If-Else 语句

+
1
+2
+3
+4
+
if username == 'admin' and password == '123456':
+    print('身份验证成功!')
+else:
+    print('身份验证失败!')
+
+ +

多分支 If-Elseif-Else 语句

+
1
+2
+3
+4
+5
+6
+7
+
x = float(input('x = '))
+if x > 1:
+    y = 3 * x - 5
+elif x >= -1:
+    y = x + 2
+else:
+    y = 5 * x + 3
+
+ +

悬空Else分支

+

使用pass命令执行“不执行任何操作”

+
1
+2
+3
+4
+5
+
x = float(input('x = '))
+if x > 1:
+    print(x)
+else
+    pass
+
+

Attention:尽量避免没有必要的嵌套,嵌套既影响可读性也增加bug出现概率

+ +

Attention: 同时注意,与Pascal不同,if和else语句末尾有冒号”:”

+ +
+

Friendly reminder: This series are my own study notes of Python-100-Days. I do not own copyright of some of the content. Nor is this a good tutorial of Python. I really appreciate it if you notice any mistakes or errors and tell me. Sorry that things copied from Python-100-Days will not be noted due to huge workload. The whole series are not allowed to be reproduced. View python-learning-readme.

+:ET \ No newline at end of file diff --git a/_posts/2020-01-02-python-learning-00-readme.md b/_posts/2020-01-02-python-learning-00-readme.md index 2876aebbe86..32c0dfe7b48 100644 --- a/_posts/2020-01-02-python-learning-00-readme.md +++ b/_posts/2020-01-02-python-learning-00-readme.md @@ -10,7 +10,8 @@ tags: --- -**Friendly reminder:** This series are my own study notes of **[Python-100-Days](https://github.com/jackfrued/Python-100-Days)**. I do not own copyright of some of the content. Nor is this a good tutorial of Python. But if you find mistakes or errors in the notes, please tell me. I really appreciate that. **The whole series are not allowed to be reproduced**. +**Friendly reminder:** This series are my own study notes of **[Python-100-Days](https://github.com/jackfrued/Python-100-Days)**. I do not own copyright of some of the content. Nor is this a good tutorial of Python. I really appreciate it if you notice any mistakes or errors and tell me. Sorry that things copied from [Python-100-Days](https://github.com/jackfrued/Python-100-Days) will not be noted due to huge workload. **The whole series are not allowed to be reproduced**. View **[python-learning-readme](https://mikelyou.com/2020/01/02/python-learning-00-readme/)**. +------------ ## Tools used: @@ -22,4 +23,5 @@ tags: ## Catalog: -- +- [#00 - Readme](https://mikelyou.com/2020/01/02/python-learning-00-readme/) +- [#01 - Notes, Variables and Operators](https://mikelyou.com/2020/01/02/python-learning-01-notes-variables-operators/) diff --git a/_posts/2020-01-02-python-learning-01-notes-variables-operators.md b/_posts/2020-01-02-python-learning-01-notes-variables-operators.md new file mode 100644 index 00000000000..fc0288692c0 --- /dev/null +++ b/_posts/2020-01-02-python-learning-01-notes-variables-operators.md @@ -0,0 +1,43 @@ +--- +layout: post +title: "Python Learning #02 - Input, Output and Assignment" +subtitle: '输入,输出,赋值' +author: "Mike Lyou" +header-style: text +tags: + - Python + - 学习笔记 +--- + +#### 赋值 +```python +a = 10 +b = 3 +a += b # 相当于:a = a + b +a *= a + 2 # 相当于:a = a * (a + 2) +``` + +#### 输入`input` +```python +a = int(input('a = ')) #读取为int类型 +f = float(input('请输入华氏温度: ')) #显示输入提示 +a, b = map(int, input().split()) #读取在同一行的多个数字 +``` + +#### 输出`print` +```python +print(a) +print(a + b) +print(a,b) #输出结果会以空格隔开 +print('%.1f' % a) #输出float型变量a,保留一位小数 +``` +> [Python格式化输出](https://www.cnblogs.com/fat39/p/7159881.html) + +- 使用`end`函数使`print`不换行 +```python +for i in range(1, 5+1): + print (i, end = " ") #1 2 3 4 5 +``` + +------------ +**Friendly reminder:** This series are my own study notes of **[Python-100-Days](https://github.com/jackfrued/Python-100-Days)**. I do not own copyright of some of the content. Nor is this a good tutorial of Python. I really appreciate it if you notice any mistakes or errors and tell me. Sorry that things copied from [Python-100-Days](https://github.com/jackfrued/Python-100-Days) will not be noted due to huge workload. **The whole series are not allowed to be reproduced**. View **[python-learning-readme](https://mikelyou.com/2020/01/02/python-learning-00-readme/)**. diff --git a/_posts/2020-01-02-python-learning-01-note-variable-operator.md b/_posts/2020-01-02-python-learning-02-input-output-assignment.md similarity index 88% rename from _posts/2020-01-02-python-learning-01-note-variable-operator.md rename to _posts/2020-01-02-python-learning-02-input-output-assignment.md index ebbaf446d3f..380fcd2a043 100644 --- a/_posts/2020-01-02-python-learning-01-note-variable-operator.md +++ b/_posts/2020-01-02-python-learning-02-input-output-assignment.md @@ -1,6 +1,6 @@ --- layout: post -title: "Python Learning #01 - Note, Variable and Operator" +title: "Python Learning #01 - Notes, Variables and Operators" subtitle: '注释,变量,运算符' author: "Mike Lyou" header-style: text @@ -82,6 +82,6 @@ print(type(e)) # >**说明:** 在实际开发中,如果搞不清楚运算符的优先级,可以使用括号来确保运算的执行顺序。 +------------- - -**Friendly reminder:** This series are my own study notes of **[Python-100-Days](https://github.com/jackfrued/Python-100-Days)**. I do not own copyright of some of the content. Nor is this a good tutorial of Python. But if you find mistakes or errors in the notes, please tell me. I really appreciate that. **The whole series are not allowed to be reproduced**. +**Friendly reminder:** This series are my own study notes of **[Python-100-Days](https://github.com/jackfrued/Python-100-Days)**. I do not own copyright of some of the content. Nor is this a good tutorial of Python. I really appreciate it if you notice any mistakes or errors and tell me. Sorry that things copied from [Python-100-Days](https://github.com/jackfrued/Python-100-Days) will not be noted due to huge workload. **The whole series are not allowed to be reproduced**. View **[python-learning-readme](https://mikelyou.com/2020/01/02/python-learning-00-readme/)**. diff --git a/_posts/2020-01-02-python-learning-03-branch-structure.md b/_posts/2020-01-02-python-learning-03-branch-structure.md new file mode 100644 index 00000000000..390356122c9 --- /dev/null +++ b/_posts/2020-01-02-python-learning-03-branch-structure.md @@ -0,0 +1,45 @@ +--- +layout: post +title: "Python Learning #03 - Branch Structure" +subtitle: '分支结构 - if 语句' +author: "Mike Lyou" +header-style: text +tags: + - Python + - 学习笔记 +--- + +#### 两分支 `If-Else` 语句 +```python +if username == 'admin' and password == '123456': + print('身份验证成功!') +else: + print('身份验证失败!') +``` + +#### 多分支 `If-Elseif-Else` 语句 +```python +x = float(input('x = ')) +if x > 1: + y = 3 * x - 5 +elif x >= -1: + y = x + 2 +else: + y = 5 * x + 3 +``` + +#### 悬空Else分支 +使用`pass`命令执行“不执行任何操作” +```python +x = float(input('x = ')) +if x > 1: + print(x) +else + pass +``` +**Attention**:尽量避免没有必要的嵌套,嵌套既影响可读性也增加bug出现概率 + +**Attention**: 同时注意,与Pascal不同,if和else语句末尾有冒号":" + +------------ +**Friendly reminder:** This series are my own study notes of **[Python-100-Days](https://github.com/jackfrued/Python-100-Days)**. I do not own copyright of some of the content. Nor is this a good tutorial of Python. I really appreciate it if you notice any mistakes or errors and tell me. Sorry that things copied from [Python-100-Days](https://github.com/jackfrued/Python-100-Days) will not be noted due to huge workload. **The whole series are not allowed to be reproduced**. View **[python-learning-readme](https://mikelyou.com/2020/01/02/python-learning-00-readme/)**. diff --git a/_posts/2020-01-02-python-learning-04-loop-structure.md b/_posts/2020-01-02-python-learning-04-loop-structure.md new file mode 100644 index 00000000000..7ec830c516d --- /dev/null +++ b/_posts/2020-01-02-python-learning-04-loop-structure.md @@ -0,0 +1,65 @@ +--- +layout: post +title: "Python Learning #04 - Loop Structure" +subtitle: '循环结构' +author: "Mike Lyou" +header-style: text +tags: + - Python + - 学习笔记 +--- + +在Python中同样有两种循环:`for-in`循环和`while`循环 + +#### for-in 循环 +```python +""" +用for循环实现1~100求和 +""" + +sum = 0 +for x in range(101): + sum += x +print(sum) +``` +其中,`range()`函数可以更灵活: + +- `range(101)`可以产生一个0到100的整数序列。 +- `range(1, 100)`可以产生一个1到99的整数序列。 +- `range(1, 100, 2)`可以产生一个1到99的奇数序列,其中2是步长,即数值序列的增量。 + +**注意: `range(n)`产生的是`0`至`n-1`的序列!** + +#### while 循环 + +```python +""" +用while循环实现1~100求和 +""" + +i = 1 +sum = 0 +while i <= 100 : + i = i + 1 + sum =sum + i +print(sum) +``` + +在`while`循环中,可以使用`break`强制结束循环,以及使用`continue`函数直接跳到下一次循环的开始。 + +#### 循环的嵌套 +举个栗子,九九乘法表 +```python +""" +九九乘法表 +""" + +for i in range(1, 9 + 1): + for j in range(1, i + 1): + print('%d*%d=%d' % (i, j, i * j), end='\t') + print() +``` +>关于逗号`,`的作用,可以参考[这篇文章](https://blog.csdn.net/liuzx32/article/details/7831247),应该是对的 + +------------ +**Friendly reminder:** This series are my own study notes of **[Python-100-Days](https://github.com/jackfrued/Python-100-Days)**. I do not own copyright of some of the content. Nor is this a good tutorial of Python. I really appreciate it if you notice any mistakes or errors and tell me. Sorry that things copied from [Python-100-Days](https://github.com/jackfrued/Python-100-Days) will not be noted due to huge workload. **The whole series are not allowed to be reproduced**. View **[python-learning-readme](https://mikelyou.com/2020/01/02/python-learning-00-readme/)**. diff --git a/_posts/2020-01-02-python-learning-05-function-module.md b/_posts/2020-01-02-python-learning-05-function-module.md new file mode 100644 index 00000000000..bc352e99608 --- /dev/null +++ b/_posts/2020-01-02-python-learning-05-function-module.md @@ -0,0 +1,52 @@ +--- +layout: post +title: "Python Learning #05 - Founction and Module" +subtitle: '函数和模块' +author: "Mike Lyou" +header-style: text +tags: + - Python + - 学习笔记 +--- + + +## 函数 + +##### 定义函数 + +使用`def`关键字来定义函数,通过`return`关键字来返回一个值,举个栗子: + +```python +def fac(num): # 阶乘,实际上Python的math模块中有factorial函数,这里仅做演示 + result = 1 + for n in range(1, num + 1): + result *= n + return result +``` + +- Python 函数与其他语言的区别在于,**在Python中,函数的参数可以有默认值,也支持使用可变参数**,~~所以Python并不需要像其他语言一样支持函数的重载,因为我们在定义一个函数的时候可以让它有多种不同的使用方式(听不懂)~~ +- 如果在调用函数的时候,没有传入对应参数的值,将使用该参数的默认值(如果有的话) + +## 模块 + +由于Python没有函数重载的概念,那么后面的定义会覆盖之前的定义,也就意味着两个函数同名函数实际上只有一个是存在的。 + +```python +def foo(): + print('hello, world!') + +def foo(): + print('goodbye, world!') + +# 下面的代码会输出什么呢? +foo() # goodbye, world! +``` + +## 使用模块管理函数 + +不是hin懂,以后再看。前往 [课件](https://github.com/jackfrued/Python-100-Days/blob/master/Day01-15/06.函数和模块的使用.md#用模块管理函数). + + + +------------ +**Friendly reminder:** This series are my own study notes of **[Python-100-Days](https://github.com/jackfrued/Python-100-Days)**. I do not own copyright of some of the content. Nor is this a good tutorial of Python. I really appreciate it if you notice any mistakes or errors and tell me. Sorry that things copied from [Python-100-Days](https://github.com/jackfrued/Python-100-Days) will not be noted due to huge workload. **The whole series are not allowed to be reproduced**. View **[python-learning-readme](https://mikelyou.com/2020/01/02/python-learning-00-readme/)**. diff --git a/_posts/2020-01-02-python-learning-05-string-list-tuple-dict-set.md b/_posts/2020-01-02-python-learning-05-string-list-tuple-dict-set.md new file mode 100644 index 00000000000..79ac2dbfb8c --- /dev/null +++ b/_posts/2020-01-02-python-learning-05-string-list-tuple-dict-set.md @@ -0,0 +1,294 @@ +--- +layout: post +title: "Python Learning #06 - String, List, Tuple, Dict and Set" +subtitle: '字符串,列表,元组,字典,集合' +author: "Mike Lyou" +header-style: text +tags: + - Python + - 学习笔记 +--- + + + +[本节讲义 →](https://github.com/jackfrued/Python-100-Days/blob/master/Day01-15/07.%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%92%8C%E5%B8%B8%E7%94%A8%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84.md) + +## 目录 +- [字符串](#字符串) +- [列表](#列表) +- [元组](#元组) +- [字典](#字典) +- [集合](#集合) + +## 字符串 + +###### 表示方法 + +由单引号或双引号括起来的单个或多个字符称为字符串,以三个双引号或单引号开头的字符串可以折行。 + +```python +s1 = 'hello, world!' +s2 = "hello, world!" +s3 = """ +hello, +world! +""" + +print(s1, s2, s3, end='') +``` + +###### 转义 + +例如: `\n`表示换行,`\'`表示字符`'`,`\\`表示字符`\` + +`\`后面还可以跟一个八进制或者十六进制数来表示字符,例如`\141`和`\x61`分别为八进制和十六进制的小写字母`a` + +`\`后面还可以跟Unicode字符编码来表示字符(大大提高了可玩性) + +```python +print('\u00A2\u132B\u26BD\u2F5B\u3093') #¢ጫ⚽⽛ん +``` + +若不希望转义,可以通过在字符串的最前面加上字母`r`来实现。 + +```python +s1 = r'\'hello, world!\'' # \'hello, world!\' +``` + +###### 字符串的运算 + +Python为字符串类型提供了非常丰富的运算符: + +```python +s1 = 'hello ' * 3 +print(s1) # hello hello hello + +s2 = 'world' +s1 += s2 +print(s1) # hello hello hello world + +print('ll' in s1) # True +print('good' in s1) # False + + +str2 = 'abc123456' + +# 从字符串中取出指定位置的字符(下标运算) +print(str2[2]) # c + +# 字符串切片(从指定的开始索引到指定的结束索引) +#index 从 0 至 n-1,str2[l:r] 包含 l 不包含 r +print(str2[2:5]) # c12 +print(str2[2:]) # c123456 +print(str2[2::2]) # c246 +print(str2[::2]) # ac246 +print(str2[::-1]) # 654321cba +print(str2[-3:-1]) # 45 +``` + +在Python中,我们还可以通过一系列的方法来完成对字符串的处理: +```python +str1 = 'hello, world!' + +# 通过内置函数len计算字符串的长度 +print(len(str1)) # 13 + +# 获得字符串首字母大写的拷贝 +print(str1.capitalize()) # Hello, world! + +# 获得字符串每个单词首字母大写的拷贝 +print(str1.title()) # Hello, World! + +# 获得字符串变大写后的拷贝 +print(str1.upper()) # HELLO, WORLD! + +# 从字符串中查找子串所在位置 +print(str1.find('or')) # 8 +print(str1.find('shit')) # -1 +# 与find类似但找不到子串时会引发异常 +# print(str1.index('or')) +# print(str1.index('shit')) + +# 检查字符串是否以指定的字符串开头 +print(str1.startswith('He')) # False +print(str1.startswith('hel')) # True + +# 检查字符串是否以指定的字符串结尾 +print(str1.endswith('!')) # True + +# 将字符串以指定的宽度居中并在两侧填充指定的字符 +print(str1.center(50, '*')) +# 将字符串以指定的宽度靠右放置左侧填充指定的字符 +print(str1.rjust(50, ' ')) + + +str2 = 'abc123456' +# 检查字符串是否由数字构成 +print(str2.isdigit()) # False + +# 检查字符串是否以字母构成 +print(str2.isalpha()) # False + +# 检查字符串是否以数字和字母构成 +print(str2.isalnum()) # True + + +str3 = ' jackfrued@126.com ' +print(str3) + +# 获得字符串修剪左右两侧空格之后的拷贝 +print(str3.strip()) +``` + +我们之前讲过,可以用下面的方式来格式化输出字符串: + +```python +a, b = 5, 10 +print('%d * %d = %d' % (a, b, a * b)) +``` + +当然,我们也可以用字符串提供的方法来完成字符串的格式: + +```python +a, b = 5, 10 +print('{0} * {1} = {2}'.format(a, b, a * b)) +``` + +Python 3.6以后,格式化字符串还有更为简洁的书写方式,就是在字符串前加上字母f,我们可以使用下面的语法糖来简化上面的代码。 + +```python +a, b = 5, 10 +print(f'{a} * {b} = {a * b}') +``` + +## 列表 + +列表(list),与字符串类似,也是一种结构化的、非标量类型,它是值的有序序列,每个值都可以通过索引进行标识,定义列表可以将列表的元素放在`[]`中,多个元素用`,`进行分隔,可以使用`for`循环对列表元素进行遍历,也可以使用`[]`或`[:]`运算符取出列表中的一个或多个元素 + +下面的代码演示了如何定义列表、如何遍历列表以及列表的下标运算。 + +```python +list1 = [1, 3, 5, 7, 100] +print(list1) # [1, 3, 5, 7, 100] + +# 乘号表示列表元素的重复 +list2 = ['hello'] * 3 +print(list2) # ['hello', 'hello', 'hello'] + +# 计算列表长度(元素个数) +print(len(list1)) # 5 + +# 下标(索引)运算 +print(list1[0]) # 1 +print(list1[4]) # 100 + +# print(list1[5]) # IndexError: list index out of range +print(list1[-1]) # 100 +print(list1[-3]) # 5 + +list1[2] = 300 +print(list1) # [1, 3, 300, 7, 100] + +# 通过循环用下标遍历列表元素 +for index in range(len(list1)): + print(list1[index]) + +# 通过for循环遍历列表元素 +for elem in list1: + print(elem) + +# 通过enumerate函数处理列表之后再遍历可以同时获得元素索引和值 +for index, elem in enumerate(list1): + print(index, elem) +``` + +下面的代码演示了如何向列表中添加元素以及如何从列表中移除元素。 + +```python +list1 = [1, 3, 5, 7, 100] + +# 添加元素 +list1.append(200) +list1.insert(1, 400) + +# 合并两个列表 +# list1.extend([1000, 2000]) +list1 += [1000, 2000] +print(list1) # [1, 400, 3, 5, 7, 100, 200, 1000, 2000] +print(len(list1)) # 9 + +# 先通过成员运算判断元素是否在列表中,如果存在就删除该元素 +if 3 in list1: + list1.remove(3) +if 1234 in list1: + list1.remove(1234) +print(list1) # [1, 400, 5, 7, 100, 200, 1000, 2000] + +# 从指定的位置删除元素 +list1.pop(0) +list1.pop(len(list1) - 1) +print(list1) # [400, 5, 7, 100, 200, 1000] + +# 清空列表元素 +list1.clear() +print(list1) # [] +``` + +和字符串一样,列表也可以做切片操作,通过切片操作我们可以实现对列表的复制或者将列表中的一部分取出来创建出新的列表,代码如下所示。 + +```python +fruits = ['grape', 'apple', 'strawberry', 'waxberry'] +fruits += ['pitaya', 'pear', 'mango'] + +# 列表切片 +fruits2 = fruits[1:4] +print(fruits2) # apple strawberry waxberry + +# 可以通过完整切片操作来复制列表 +fruits3 = fruits[:] +print(fruits3) # ['grape', 'apple', 'strawberry', 'waxberry', 'pitaya', 'pear', 'mango'] +fruits4 = fruits[-3:-1] +print(fruits4) # ['pitaya', 'pear'] + +# 可以通过反向切片操作来获得倒转后的列表的拷贝 +fruits5 = fruits[::-1] +print(fruits5) # ['mango', 'pear', 'pitaya', 'waxberry', 'strawberry', 'apple', 'grape'] +``` + +下面的代码实现了对列表的排序操作。 + +```python +list1 = ['orange', 'apple', 'zoo', 'internationalization', 'blueberry'] + +list2 = sorted(list1) +# sorted函数返回列表排序后的拷贝不会修改传入的列表 +# 函数的设计就应该像sorted函数一样尽可能不产生副作用 + +list3 = sorted(list1, reverse=True) +# 通过key关键字参数指定根据字符串长度进行排序而不是默认的字母表顺序 + +list4 = sorted(list1, key=len) + +print(list1) +print(list2) +print(list3) +print(list4) + +# 给列表对象发出排序消息直接在列表对象上进行排序 +list1.sort(reverse=True) +print(list1) +``` + +###### [使用生成式和生成器创建列表](https://github.com/jackfrued/Python-100-Days/blob/master/Day01-15/07.%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%92%8C%E5%B8%B8%E7%94%A8%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84.md#%E7%94%9F%E6%88%90%E5%BC%8F%E5%92%8C%E7%94%9F%E6%88%90%E5%99%A8) + +## [元组](https://github.com/jackfrued/Python-100-Days/blob/master/Day01-15/07.%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%92%8C%E5%B8%B8%E7%94%A8%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84.md#%E4%BD%BF%E7%94%A8%E5%85%83%E7%BB%84) + +## [字典](https://github.com/jackfrued/Python-100-Days/blob/master/Day01-15/07.%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%92%8C%E5%B8%B8%E7%94%A8%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84.md#%E4%BD%BF%E7%94%A8%E5%AD%97%E5%85%B8) + +## [集合](https://github.com/jackfrued/Python-100-Days/blob/master/Day01-15/07.%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%92%8C%E5%B8%B8%E7%94%A8%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84.md#%E4%BD%BF%E7%94%A8%E9%9B%86%E5%90%88) + + + + +------------ +**Friendly reminder:** This series are my own study notes of **[Python-100-Days](https://github.com/jackfrued/Python-100-Days)**. I do not own copyright of some of the content. Nor is this a good tutorial of Python. I really appreciate it if you notice any mistakes or errors and tell me. Sorry that things copied from [Python-100-Days](https://github.com/jackfrued/Python-100-Days) will not be noted due to huge workload. **The whole series are not allowed to be reproduced**. View **[python-learning-readme](https://mikelyou.com/2020/01/02/python-learning-00-readme/)**.