环境 Python Education 2021 + tabnine Education (这个补全还有点好用)

11.for - while

# 作者 : Llonvne 
# 邮箱 : work@llonvne.cn

# for 循环 格式:
# for item in my_list(可迭代对象):
# 	代码块

# item 你每次循环得到的对象
# 其中最重要的可迭代对象(iterable object)
# 类型举例:列表、元组、字典与集合

# exp.1 520的一万种表示方法
word = ['520','Iloveyou','我爱你','521']
for item in word:
    print(item,end=' ')
print()

# 其中 word 就是 可迭代对象
word = ['520', 'Iloveyou', '我爱你', '521']
for item in word:
    print('说法:',item)

# exp.2 for 里面可以嵌套 if
for item in word:
    if item == '我爱你':
        print('我爱你一生一世')
    else:
        print(item)


# range 函数 range(a,b) 会生成一个 从 a 到 b-1 的 等差数列 ,也是一个可迭代对象
# exp.3 range 1
r = range(1,10)
print(type(r))

# range 的 a 可以缺省 如果缺省 则默认为 0 例如 range(n) 会生成 0 - n-1
# exp.4 range 2
for i in range(4):
    print(i)
for i in range(1,5):
    print(i)


# exp.5 99乘法表
for i in range(1,10):
    for j in range(i,10):
        print("%2d * %2d = %2d " %(i,j,i*j),end='')
    print()

# break , continue
# break 打破当前循环,直接跳出
# continue 直接进入下一次循环

# for - else
# 一种 Python 特殊的语法结构 大意就是 当for被执行完毕(即不使用 break方法退出)
for i in range(3):
    break
else:print('break')
for i in range(3):
    pass
else:print('pass')

# 我们发现当 for 被强制退出 就不会执行 print 而 pass 后就会
# pass 类似与 C语言中的单个分号 用于占位置

# while 循环
# while 条件:
#    代码块
answer = 12
guess = int(input('开始猜吧:'))
while guess != answer :
    if guess >= answer:
        print('Big!')
    else :
        print('Small')
    guess = int(input('重新猜吧!:'))
print('Right')

12.元组

# 作者 : Llonvne 
# 邮箱 : work@llonvne.cn

# 元组 不可变列表 (tuple)
# 元组用小括号定义

# exp.1 520又是我!
a = (5,2,0)
print(a,type(a))

# exp.2 Only 520
a = (520)
print(a,type(a))
a = (520,)
print(a,type(a))

# 只有一个元素的元组 需要加一个小逗号来表示,否则会被认为是括号哦!
# 元祖也可以使用 索引访问
# 元祖拥有部分列表的方法 不包括那些对自身有修改的
# 元祖可以跟列表相互转换

# exp.3 变变变!
list1 = [5,2,0]
print(type(tuple(list1)))
print(type(list((tuple(list1)))))

# zip 函数 先看例子

# exp.4 zip ? 松鼠!
chinese = [520,1314]
english = ['I love you','All the life']
C_E = zip(chinese,english)
print(C_E,type(C_E))
print(list(C_E))

# 通过转换为列表 我们发现 zip 函数 把对应元素 变成元祖 放在一起

# exp.5 zip eat!
chinese = [520,1314,'Llonvne']
english = ['I love you','All the life']
z = zip(chinese,english)
print(list(z))

# 如果长度不一致 会以短为准,长的会消失!

# exp.6 unzip
chinese = [520,1314,'Llonvne']
english = ['I love you','All the life']
z = zip(chinese,english)
print('ZIP')
print(z)
unzip = zip(*z)
print('UNZIP')
print(unzip)
print(list(unzip))

# 如果在前面添加一个* 就是解压

# 元祖的功能:
# 1:不可修改
# 2:速度快
# 3:作为函数的返回值

13.字典

# 作者 : Llonvne
# 邮箱 : work@llonvne.cn

# 字典
# 定义: my_dict = {键1:值1,键2:值2......}

# exp.1 520 dictionary
dic_520 = {'520':'1314','我爱你':'一生一世'}
print(dic_520,type(dic_520))
print(dic_520['520'])

# 简单来说 字典就是特殊的列表,或者说 列表是特殊的字典
# 列表是一种以自然数(包含0)为索引的字典
# 而字典则可以创建任意的对应关系

# 简单来说 列表创建的关系为
# 0 -> list1[0]  从一个 数字 到 一个 对象
# 类似与字典 则是 key - word 的 一组组对应关系 拼起来

# exp.2 增加元素!
dic_520 = {'520':'1314','我爱你':'一生一世'}
dic_520['I love you'] = 'for the whole life'
print(dic_520['I love you'])

# 字典中添加元素 格式 字典名字[key] = word
# 为什么不需要使用 类似于 append 或者其他的方法呢?
# 因为在字典内部不存在 前后顺序, 而是各自独立的 键值对 形式存储

# exp.3 修改元素
dic_520 = {'520':'1314','我爱你':'一生一世'}
dic_520['520'] = {'13144444'}
print(dic_520['520'])

# 字典元素修改方法为直接对其进行赋值

# exp.4 删除元素
del dic_520['520']
print(dic_520)

# 使用 del 来删除字典中元素

# exp.5 清空字典
dic_520 = {'520':'1314','我爱你':'一生一世'}
dic_520.clear()
print(dic_520)

# 使用 clear 方法来清空字典

# exp.6 删除字典
dic_520 = {'520':'1314','我爱你':'一生一世'}
del dic_520
# print(dic_520)

# del + 字典名字 会把字典完全清除,注意除非特殊需要,一般我们不推荐使用这种做法
# 宁愿空置 也不要删除!

# exp.7 blank dict
my_dict = {}

# 使用一个花括号来生成一个空字典

# exp.8 len of dictionary
dic_520 = {'520':'1314','我爱你':'一生一世'}
print(len(dic_520))

# exp.8 a Good Write dictionary
Good = {'520':'1314',
        '我爱你':'一生一世',
        'I love you':'for the whole life'}
bad  = {'520':'1314','我爱你':'一生一世','I love you':'for the whole life'}

# 换行是一个好习惯!

# 可能有人会问 既然 字典是由 不同的对象 索引的 那么如何进行 遍历呢?
# 这里就要用到 python 的可迭代对象了!
# 先看 例子

# exp.9 让我看看 !
my_dict = {"red": "红色",
           "green": "绿色",
           "blue": "蓝色"}

# 输出所有的 键
for item in my_dict:
    print(item)
# 将 键 和 值 组合成 元组 输出!
for item in my_dict.items():
    print(item)
# 将变量与元组之间的赋值输出
for key,value in my_dict.items():
    print("key = '{}' , word = '{}'".format(key,value))
# 其中 变量元组赋值如下所示
a,b = (1,2)
print(a,b)

# 使用 keys 方法获得所有键 (这个方法与不写相同,但建议写!)
for key in my_dict.keys():
    print(key)
# 使用上诉方法获得键值后也可以遍历输出了!
for key in my_dict.keys():
    print(my_dict[key])

# exp.10 套娃

# list - dictionary
list1 = [
    {'520','1314'},
    {'我爱你','一生一世'}
]

# 这是一个列表 里面包含了 两个字典
# 实际上 列表 字典 可以随便你套玩,想怎么套 就这么套

# exp.11 字典产生!
list1 = {1,2,3,4}
list2 = {'a','b','c','d'}
dict1 = dict.fromkeys(list1,'数字!')
print(dict1)

# 可以使用 formkeys 的 方法把一个列表 转换为初值 里面默认值为 NONE ,
# 可以通过第二个参数来给定初值, 要注意的是 如果第二个参数给一个列表,
# 那么每个参数都是这个列表,不会分解!

# exp.12 多此一举?
my_dict = {"red": "红色",
           "green": "绿色",
           "blue": "蓝色"}
print(my_dict.get('red'))

# 这个方法 在 正常工作时 与直接给定索引值相同,但是 这个方法在找不到时会默认返回一个None
# 非常友好,在不确定该值一定存在建议使用
# 同时可以是使用 该方法为特定的值设置不存在的返回值 如果 'brown'不存在 就会返回 'No brown'
my_dict.get('brown','NO brown')
print(my_dict)

# exp.13 多此一举? 2
my_dict = {"red": "红色",
           "green": "绿色",
           "blue": "蓝色"}

# 在不给定参数时 如果不存在 会默认插入一个None ,存在就直接返回
print(my_dict.setdefault('brown'))
print(my_dict)
# 给定参数 会帮你插入字典
print(my_dict.setdefault('Yellow','No yellow'))
print(my_dict.setdefault('Yellow'))
print(my_dict)

# exp.14 pop it 2 (说实话 我也不知道是不是第二个,反正不是第一个!)
my_dict = {"red": "红色",
           "green": "绿色",
           "blue": "蓝色"}

print(my_dict.pop('red','Not Found'))
print(my_dict)

# pop 的 第一个参数 为 要删除的值 ,第二个是 未找到时的错误返回值
# 字典类型的pop删除不会返回被删除的值!

14.集合

# 作者 : Llonvne 
# 邮箱 : work@llonvne.cn

# 集合 :无序 + 唯一

# 集合里面的类型必须是 不可变类型 : 字符串,数字,元组
# 可变类型:列表 字典,都不可以

# 但是集合本身是可变的,可以增加和减少内容

# exp.1 集合的建立
my_set = {1,2,2,3,4,5}
print(my_set,type(my_set))

# 在python 中集合也适用 大括号来建立,如果里面有键值对的形式就是字典,否则就是集合!

# exp.2 set function

empty = set()
print('---')
print(empty)

# 当 set 收到一个字符串 被把它解释为一个字符串数组 去重复 无序输出
print(set('aaaaaapppawdnwadnqawfneaifwndwabfef'),type(set('awdwajdhwlabfdanwflwadnwakdjb')))

# 当 set 收到一个 列表时 会自动去重复,无序返回
list1 = ('1',1,'2',2,'apple','apple')
print(set(list1))

# 集合操作当符号
# & 交集 == 集合当 intersection方法
# | 并集 == union 方法
# - 差集 == difference 方法 A - B 不要B有的 B - A 不要 A 的
# ^ 对称差集 == symmetric_difference 方法 (不要都有的)

# exp.3 & | - ^
a = {1,2,3,4,5}
b = {1,2,3}
c = {'a','b','c',1,2,3}
# & 或者 intersection方法
print(a & b)
print(a.intersection(b))
# | or union
print(a | b | c)
print(c.union(a.union(b)))
# - or difference
print(a - c)
print(a.difference(c))
# ^ 或者 symmetric_difference
print(a ^ b)
print(a.symmetric_difference(b))

# exp.4 set.add
my_set = {1,2,3}
my_set.add('5')
print(my_set)

# exp.5 remove discard
my_set = {1,2,3}
my_set.remove(1)
print(my_set)


# remove 方法可以删除元素,不存在会报错!
# discard 也可以 但不会报错!
my_set = {1,2,3}
my_set.discard(1)
print(my_set)
my_set.discard(4)
print(my_set)

# exp.5 抽奖
奖池 = {'谢谢惠顾','谢谢惠顾','谢谢惠顾','谢谢惠顾','谢谢惠顾'}
print(奖池.pop())

# 使用 pop 方法将随机删除一个元素,被删除的元素将被返回
# 集合为空会报错

# exp.6 清空 set
my_set = {1,2,3}
my_set.clear()
print(my_set)


# exp.7 集合其他元素
set1 = {'1','2','3'}
set2 = {'1'}
print(set1.isdisjoint(set2))

# isdisjoint 这个方法判断两个集合有没有相同元素,没有返回 True

# A.issubset(B) 判断A 是不是 B 的子集
# A.issuperset(B) 夫集

# undate 将一个集合(参数)添加到另一个集合(方法所有者)
# 该函数与 & 的不同 update 会直接改变调用者的集合 变成 & 集
# 而 & 只是返回值是 &
# A = A & B 等价于 a.update(B)

# intersection_update 此方法用于求多个集合的交集
# difference_update 删除集合内与另一集合重复的元素
# symmetric_difference_update 类似对称差集的用法
# max , min ,len
a = {122,12,242,232,123,12414,12,1,214,23}
# 要时刻集合无序性质
print(a)
print(sorted(a))

# 创建一个空集合
a = set()

# 要注意 空集合 在 python 中的记号就是 set()
# set() 就是代表空集合 {}默认是字典 别弄混!

15.函数

# 作者 : Llonvne 
# 邮箱 : work@llonvne.cn

# 集合 :无序 + 唯一

# 集合里面的类型必须是 不可变类型 : 字符串,数字,元组
# 可变类型:列表 字典,都不可以

# 但是集合本身是可变的,可以增加和减少内容

# exp.1 集合的建立
my_set = {1,2,2,3,4,5}
print(my_set,type(my_set))

# 在python 中集合也适用 大括号来建立,如果里面有键值对的形式就是字典,否则就是集合!

# exp.2 set function

empty = set()
print('---')
print(empty)

# 当 set 收到一个字符串 被把它解释为一个字符串数组 去重复 无序输出
print(set('aaaaaapppawdnwadnqawfneaifwndwabfef'),type(set('awdwajdhwlabfdanwflwadnwakdjb')))

# 当 set 收到一个 列表时 会自动去重复,无序返回
list1 = ('1',1,'2',2,'apple','apple')
print(set(list1))

# 集合操作当符号
# & 交集 == 集合当 intersection方法
# | 并集 == union 方法
# - 差集 == difference 方法 A - B 不要B有的 B - A 不要 A 的
# ^ 对称差集 == symmetric_difference 方法 (不要都有的)

# exp.3 & | - ^
a = {1,2,3,4,5}
b = {1,2,3}
c = {'a','b','c',1,2,3}
# & 或者 intersection方法
print(a & b)
print(a.intersection(b))
# | or union
print(a | b | c)
print(c.union(a.union(b)))
# - or difference
print(a - c)
print(a.difference(c))
# ^ 或者 symmetric_difference
print(a ^ b)
print(a.symmetric_difference(b))

# exp.4 set.add
my_set = {1,2,3}
my_set.add('5')
print(my_set)

# exp.5 remove discard
my_set = {1,2,3}
my_set.remove(1)
print(my_set)


# remove 方法可以删除元素,不存在会报错!
# discard 也可以 但不会报错!
my_set = {1,2,3}
my_set.discard(1)
print(my_set)
my_set.discard(4)
print(my_set)

# exp.5 抽奖
奖池 = {'谢谢惠顾','谢谢惠顾','谢谢惠顾','谢谢惠顾','谢谢惠顾'}
print(奖池.pop())

# 使用 pop 方法将随机删除一个元素,被删除的元素将被返回
# 集合为空会报错

# exp.6 清空 set
my_set = {1,2,3}
my_set.clear()
print(my_set)


# exp.7 集合其他元素
set1 = {'1','2','3'}
set2 = {'1'}
print(set1.isdisjoint(set2))

# isdisjoint 这个方法判断两个集合有没有相同元素,没有返回 True

# A.issubset(B) 判断A 是不是 B 的子集
# A.issuperset(B) 夫集

# undate 将一个集合(参数)添加到另一个集合(方法所有者)
# 该函数与 & 的不同 update 会直接改变调用者的集合 变成 & 集
# 而 & 只是返回值是 &
# A = A & B 等价于 a.update(B)

# intersection_update 此方法用于求多个集合的交集
# difference_update 删除集合内与另一集合重复的元素
# symmetric_difference_update 类似对称差集的用法
# max , min ,len
a = {122,12,242,232,123,12414,12,1,214,23}
# 要时刻集合无序性质
print(a)
print(sorted(a))

# 创建一个空集合
a = set()

# 要注意 空集合 在 python 中的记号就是 set()
# set() 就是代表空集合 {}默认是字典 别弄混!