#pyg401.py 元组及应用
tuple = ("apple") # 定义元组
print(tuple[0]) # 打印第一个元素
print (type(tuple)) # 打印定义的tuple的类型
tuple = ("apple",) # 定义元组,注意后面的逗号不可少
print (tuple[0]) # 打印第一个元素
print (type(tuple)) # 打印定义的tuple的类型
tuple = ("apple", "banana", "grape", "orange") # 定义元组
print (tuple[-1])
print (tuple[-2])
tuple2 = tuple[1:3] # 分片,第二个元素到第三个元素(不包括第四个)
tuple3 = tuple[0:-2] # 分片,从第一个元素到倒数第二个元素(不包括倒数第二个)
tuple4 = tuple[2:-2] # 分片,从第三个元素到倒第二个元素(不包括倒数第二个)
print (tuple2)
print (tuple3)
print (tuple4)
fruit1 = ("apple", "banana")
fruit2 = ("grape", "orange")
tuple = (fruit1, fruit2)
print (tuple)
print ("tuple[0][1] =", tuple[0][1])
print ("tuple[1][1] =", tuple[1][1])
#打包
tuple = ("apple", "banana", "grape", "orange")
#解包
a, b, c, d = tuple
print (a, b, c, d)
tuple = (("apple", "banana"),("grape", "orange"),("watermelon",),("grapefruit",))
for i in range(len(tuple)):
print ("tuple[%d] :" % i, "" ,)
for j in range(len(tuple[i])):
print (tuple[i][j], "" ,)
print()
#pyg4002.py 列表及应用
list = ["apple", "banana", "grape", "orange"] # 定义列表
print (list)
print (list[2])
list.append("watermelon") # 列表末尾添加元素
list.insert(1, "grapefruit") # 列表中插入元素
print (list)
list.remove("grape") # 列表中移除grape
print (list)
# list.remove("a") # 列表中移除a,因为当前列表中并没有a,所以将抛错
print (list.pop()) # 打印从列表中弹出的元素,即最后一个元素
print (list)
list = ["apple", "banana", "grape", "orange"]
print (list[-2])
print (list[1:3])
print (list[-3:-1])
list = [["apple", "banana"],["grape", "orange"],["watermelon"],["grapefruit"]]
for i in range(len(list)): # 遍历列表
print ("list[%d] :" % i, "" ,)
for j in range(len(list[i])):
print (list[i][j], "" ,)
print()
list1 = ["apple", "banana"]
list2 = ["grape", "orange"]
list1.extend(list2) # list1连接list2
print (list1)
list3 = ["watermelon"]
list1 = list1 + list3 # 将list1与list3连接后赋给list1
print (list1)
list1 += ["grapefruit"] # 使用+=给list1连接上[“grapefruit”]
print (list1)
list1 = ["apple", "banana"] * 2
print (list1)
list = ["apple", "banana", "grape", "orange"]
print (list.index("grape")) # 打印grape的索引
print (list.index("orange")) # 打印orange的索引
print ("orange" in list) # 判断orange是否在列表中
list = ["banana", "apple", "orange", "grape"]
list.sort() # 排序
print ("Sorted list:", list)
list.reverse() # 反转
print ("Reversed list:", list)
# 堆栈的实现
list = ["apple", "grape", "grape"]
list.append("orange") # 将orange压入堆栈
print (list)
print ("弹出的元素:", list.pop()) # 从堆栈中弹出最后压入的元素
print (list)
#队列的实现
list = ["apple", "grape", "grape"]
list.append("orange") # 队尾加入orange
print (list)
print ("弹出的元素:", list.pop(0)) # 弹出第一个元素
print (list)
#PYG4003.py 字典
dict = {"a" : "apple", "b" : "banana", "g" : "grape", "o" : "orange"}
print (dict)
print (dict["a"]) # 打印键a对应的值
dict = {1 : "apple", 2 : "banana", 3 : "grape", 4 : "orange"}
print (dict)
print (dict[2])
#字典的添加、删除、修改操作
dict = {"a" : "apple", "b" : "banana", "g" : "grape", "o" : "orange"}
dict["w"] = "watermelon"
del(dict["a"]) # 删除字典中键为a 的元素
dict["g"] = "grapefruit" # 修改字典中键为g的值
print (dict.pop("b")) # 弹出字典中键为b的元素
print (dict)
dict.clear() # 清除字典中所有元素
print (dict)
#字典的遍历
dict = {"a" : "apple", "b" : "banana", "g" : "grape", "o" : "orange"}
for k in dict:
print ("dict[%s] =" % k,dict[k])
#字典items()的使用
dict = {"a" : "apple", "b" : "banana", "c" : "grape", "d" : "orange"}
print (dict.items())
#使用列表、字典作为字典的值
dict = {"a" : ("apple",), "bo" : {"b" : "banana", "o" : "orange"}, "g" : ["grape","grapefruit"]}
print (dict["a"])
print (dict["a"][0])
print (dict["bo"])
print (dict["bo"]["o"])
print (dict["g"])
print (dict["g"][1])
dict = {"a" : "apple", "b" : "banana", "c" : "grape", "d" : "orange"}
#输出key的列表
print (dict.keys())
#输出value的列表
print (dict.values())
#get()的等价语句
D = {"key1" : "value1", "key2" : "value2"}
if "key1" in D:
print (D["key1"])
else:
print ("None")
#字典中元素的获取方法
dict = {"a" : "apple", "b" : "banana", "c" : "grape", "d" : "orange"}
print (dict)
print (dict.get("c", "apple")) # 使用get获取键为c的值,若不存在返回默认值apple
print (dict.get("e", "apple")) # 使用get获取键为e的值,若不存在返回默认值apple
#udpate()的等价语句
D = {"key1" : "value1", "key2" : "value2"}
E = {"key3" : "value3", "key4" : "value4"}
for k in E:
D[k] = E[k]
print (D)
#字典E中含有字典D中的key
D = {"key1" : "value1", "key2" : "value2"}
E = {"key2" : "value3", "key4" : "value4"}
for k in E:
D[k] = E[k]
print (D)
#字典的更新
dict = {"a" : "apple", "b" : "banana"}
print (dict)
dict2 = {"c" : "grape", "d" : "orange"}
dict.update(dict2) # 使用update方法更新dict
print (dict)
# 设置默认值
dict = {}
dict.setdefault("a")
print (dict)
dict["a"] = "apple"
dict.setdefault("a", "None")
print (dict)
#调用sorted()排序
dict = {"a" : "apple", "b" : "grape", "c" : "orange", "d" : "banana"}
print (dict)
#按照key排序
print (sorted(dict.items(), key=lambda d: d[0]))
#按照value排序
print (sorted(dict.items(), key=lambda d: d[1]))
#字典的浅拷贝
dict = {"a" : "apple", "b" : "grape"}
dict2 = {"c" : "orange", "d" : "banana"}
dict2 = dict.copy() # 拷贝dict并赋给dict2
print (dict2)
#字典的深拷贝
import copy
dict = {"a" : "apple", "b" : {"g" : "grape","o" : "orange"}}
dict2 = copy.deepcopy(dict) # 深拷贝
dict3 = copy.copy(dict) # 浅拷贝
dict2["b"]["g"] = "orange"
print (dict)
dict3["b"]["g"] = "orange"
print (dict)
#索引操作
tuple = ("apple", "banana", "grape", "orange")
list = ["apple", "banana", "grape", "orange"]
str = "apple"
print (tuple[0])
print (tuple[-1])
print (list[0])
print (list[-1])
print (str[0])
print (str[-1])
#分片操作
tuple = ("apple", "banana", "grape", "orange")
list = ["apple", "banana", "grape", "orange"]
str = "apple"
print (tuple[:3])
print (tuple[3:])
print (tuple[1:-1])
print (tuple[:])
print (list[:3])
print (list[3:])
print (list[1:-1])
print (list[:])
print (str[:3])
print (str[3:])
print (str[1:-1])
print (str[:])
# pyg405.py 每日一帖
import datetime # 导入日期时间类
# 定义一个列表
mot = ["坚持下去不是因为我很坚强,而是因为我别无选择",
"含泪播种的人一定能笑着收获",
"做对的事情比把事情做对重要",
"命运给予我们的不是失望之酒,而是机会之杯",
"明日永远新鲜如初,纤尘不染",
"求知若饥,虚心若愚",
"成功将属于那些从不说“不可能”的人"]
week = ["一","二","三","四","五","六","日"]
day=datetime.datetime.now().weekday() # 获取当前星期
print("每日一帖")
print("今天是星期",week[day],mot[day]) # 输出今天的每日一帖
print(' ')
print("===============每日一帖=================")
day=0
i=0
while day+i<=6:
print("星期",week[day+i],mot[day+i]) # 输出每日一帖
i+=1