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"RFriendly 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.
+ +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.
+ +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,保留一位小数
+
+ ++ +
end
函数使print
不换行
+ 1
+2
+
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. 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"If-Else
语句1
+2
+3
+4
+
if username == 'admin' and password == '123456':
+ print('身份验证成功!')
+else:
+ print('身份验证失败!')
+
由单引号或双引号括起来的单个或多个字符称为字符串,以三个双引号或单引号开头的字符串可以折行。
+ +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),与字符串类似,也是一种结构化的、非标量类型,它是值的有序序列,每个值都可以通过索引进行标识,定义列表可以将列表的元素放在[]
中,多个元素用,
进行分隔,可以使用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"!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),与字符串类似,也是一种结构化的、非标量类型,它是值的有序序列,每个值都可以通过索引进行标识,定义列表可以将列表的元素放在[]
中,多个元素用,
进行分隔,可以使用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,保留一位小数
+
+ ++ +
end
函数使print
不换行
+ 1
+2
+
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. 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"pIf-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
+
使用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)
+
使用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"'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
+
使用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"& + +由单引号或双引号括起来的单个或多个字符称为字符串,以三个双引号或单引号开头的字符串可以折行。
+ +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),与字符串类似,也是一种结构化的、非标量类型,它是值的有序序列,每个值都可以通过索引进行标识,定义列表可以将列表的元素放在[]
中,多个元素用,
进行分隔,可以使用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),与字符串类似,也是一种结构化的、非标量类型,它是值的有序序列,每个值都可以通过索引进行标识,定义列表可以将列表的元素放在[]
中,多个元素用,
进行分隔,可以使用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,保留一位小数
+
+ ++ +
end
函数使print
不换行
+ 1
+2
+
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. 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"OFriendly 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.
+ +由单引号或双引号括起来的单个或多个字符称为字符串,以三个双引号或单引号开头的字符串可以折行。
+ +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"& + +由单引号或双引号括起来的单个或多个字符称为字符串,以三个双引号或单引号开头的字符串可以折行。
+ +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),与字符串类似,也是一种结构化的、非标量类型,它是值的有序序列,每个值都可以通过索引进行标识,定义列表可以将列表的元素放在[]
中,多个元素用,
进行分隔,可以使用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的字体颜色改变了")
+"""
+
type
函数检查变量的类型
+ 1
+2
+3
+4
+5
+6
+7
+8
+9
+10
+
a = 100
+b = 12.345
+c = 1 + 5j
+d = 'hello, world'
+e = True
+print(type(a)) # <class 'int'>
+print(type(b)) # <class 'float'>
+print(type(c)) # <class 'complex'>
+print(type(d)) # <class 'str'>
+print(type(e)) # <class 'bool'>
+
int()
:将一个数值或字符串转换成整数,可以指定进制。float()
:将一个字符串转换成浮点数。str()
:将指定的对象转换成字符串形式,可以指定编码。chr()
:将整数转换成该编码对应的字符串(一个字符)。ord()
:将字符串(一个字符)转换成对应的编码(整数)。运算符 | +描述 | +
---|---|
[] [:] |
+ 下标,切片 | +
** |
+ 指数 | +
~ + - |
+ 按位取反, 正负号 | +
* / % // |
+ 乘,除,模,整除 | +
+ - |
+ 加,减 | +
>> << |
+ 右移,左移 | +
& |
+ 按位与 | +
^ \| |
+ 按位异或,按位或 | +
<= < > >= |
+ 小于等于,小于,大于,大于等于 | +
== != |
+ 等于,不等于 | +
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的字体颜色改变了")
+"""
+
type
函数检查变量的类型
+ 1
+2
+3
+4
+5
+6
+7
+8
+9
+10
+
a = 100
+b = 12.345
+c = 1 + 5j
+d = 'hello, world'
+e = True
+print(type(a)) # <class 'int'>
+print(type(b)) # <class 'float'>
+print(type(c)) # <class 'complex'>
+print(type(d)) # <class 'str'>
+print(type(e)) # <class 'bool'>
+
int()
:将一个数值或字符串转换成整数,可以指定进制。float()
:将一个字符串转换成浮点数。str()
:将指定的对象转换成字符串形式,可以指定编码。chr()
:将整数转换成该编码对应的字符串(一个字符)。ord()
:将字符串(一个字符)转换成对应的编码(整数)。运算符 | +描述 | +
---|---|
[] [:] |
+ 下标,切片 | +
** |
+ 指数 | +
~ + - |
+ 按位取反, 正负号 | +
* / % // |
+ 乘,除,模,整除 | +
+ - |
+ 加,减 | +
>> << |
+ 右移,左移 | +
& |
+ 按位与 | +
^ \| |
+ 按位异或,按位或 | +
<= < > >= |
+ 小于等于,小于,大于,大于等于 | +
== != |
+ 等于,不等于 | +
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
循环
1
+2
+3
+4
+5
+6
+7
+8
+
"""
+用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
的序列!
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" +: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
+
+
+使用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
循环
单行注释 - 以#和空格开头的部分 +多行注释 - 三个引号开头,三个引号结尾 (单引号与双引号仅有微小区别,可以忽略)
+ +1
+
a = b + c # 这是一条注释
+
1
+2
+3
+4
+5
+6
+
print("下面是多行注释")
+"""
+print("注释")
+print("注释不会编译")
+print("可以看到print的字体颜色改变了")
+"""
+
type
函数检查变量的类型
+ 1
+2
+3
+4
+5
+6
+7
+8
+9
+10
+
a = 100
+b = 12.345
+c = 1 + 5j
+d = 'hello, world'
+e = True
+print(type(a)) # <class 'int'>
+print(type(b)) # <class 'float'>
+print(type(c)) # <class 'complex'>
+print(type(d)) # <class 'str'>
+print(type(e)) # <class 'bool'>
+
int()
:将一个数值或字符串转换成整数,可以指定进制。float()
:将一个字符串转换成浮点数。str()
:将指定的对象转换成字符串形式,可以指定编码。chr()
:将整数转换成该编码对应的字符串(一个字符)。ord()
:将字符串(一个字符)转换成对应的编码(整数)。运算符 | +描述 | +
---|---|
[] [:] |
+ 下标,切片 | +
** |
+ 指数 | +
~ + - |
+ 按位取反, 正负号 | +
* / % // |
+ 乘,除,模,整除 | +
+ - |
+ 加,减 | +
>> << |
+ 右移,左移 | +
& |
+ 按位与 | +
^ \| |
+ 按位异或,按位或 | +
<= < > >= |
+ 小于等于,小于,大于,大于等于 | +
== != |
+ 等于,不等于 | +
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的字体颜色改变了")
+"""
+
type
函数检查变量的类型
+ 1
+2
+3
+4
+5
+6
+7
+8
+9
+10
+
a = 100
+b = 12.345
+c = 1 + 5j
+d = 'hello, world'
+e = True
+print(type(a)) # <class 'int'>
+print(type(b)) # <class 'float'>
+print(type(c)) # <class 'complex'>
+print(type(d)) # <class 'str'>
+print(type(e)) # <class 'bool'>
+
int()
:将一个数值或字符串转换成整数,可以指定进制。float()
:将一个字符串转换成浮点数。str()
:将指定的对象转换成字符串形式,可以指定编码。chr()
:将整数转换成该编码对应的字符串(一个字符)。ord()
:将字符串(一个字符)转换成对应的编码(整数)。运算符 | +描述 | +
---|---|
[] [:] |
+ 下标,切片 | +
** |
+ 指数 | +
~ + - |
+ 按位取反, 正负号 | +
* / % // |
+ 乘,除,模,整除 | +
+ - |
+ 加,减 | +
>> << |
+ 右移,左移 | +
& |
+ 按位与 | +
^ \| |
+ 按位异或,按位或 | +
<= < > >= |
+ 小于等于,小于,大于,大于等于 | +
== != |
+ 等于,不等于 | +
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
+
使用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)) #