小破站看的教程 IT私塾 跟着做练习
# -*- codeing = utf-8 -*-
# @Time : 2022/2/24 14:25
# @Author: CXZ
# @Flie : 字典_函数.py
# @Software: PyCharm
#字典的定义 用的是花括号{}
'''
info = {"name":"chenxiaozhen","age":"17"}
#字典的访问
print(info["name"])
print(info["age"])
print(info.get("gender")) #使用get的方法,没有找到对应的键,默认返回 none
print(info.get("gender","m")) # 没有找到 可以设定默认返回 “m”
#chenxiaozhen
#17
'''
#增
# info = {"name":"chenxiaozhen","age":"17"}
# newid = input("请输入新的学号 ")
# info["id"] = newid
#
# print(info["id"])
#删
# del info 删除整个info变量 后面访问会报错
# info.clear() 清空字典里面所有内容 访问不会报错
#改 直接赋值替换
#查
# info = {"name":"chenxiaozhen","age":"17"}
# print(info.keys()) #得到所有的键 列表形式
# print(info.values()) #得到所有的值 列表形式
# print(info.items()) #得到所有的项 列表
#遍历所有的键值对
# for key,value in info.items():
# print("key=%s,value=%s"%(key,value))
#使用枚举函数,同时拿到列表中的下标和元素内容
# myList = ["a","b","c","d"]
#
# for i,x in enumerate(myList):
# print(i,x)
#
# 0 a
# 1 b
# 2 c
# 3 d
#定义函数
'''
def add2Num(a,b):
return a+b
result = add2Num(2,11)
def divid(a,b):
shang = a//b
yushu = a%b
return shang,yushu
sh,yu = divid(5,2)
print("shang: %d,yushu: %d"%(sh,yu))
# shang: 2,yushu: 1 python 可以有多个返回值同时返回 获取方式也是直接获取
'''
'''
def print_Vline():
x=input("请输入横线数")
i=0
while i < int(x):
print("----------")
i+=1
print_Vline()
'''
```python
# -*- codeing = utf-8 -*-
# @Time : 2022/2/24 16:23
# @Author: CXZ
# @Flie : 文件操作.py
# @Software: PyCharm
'''
f = open('text.txt','r')
#f.write("hello world")
content = f.readlines()
print(content)
i = 1
for temp in content:
#print(i,temp)
print("%d:%s" % (i, temp))
i+=1
f.close()
'''
# import os
#
# os.rename("text.txt","text1.txt")
# try:
# print("-----test-----1--")
# f = open("text1.txt","r")
# print("------test-----2--")
#
# print(num)
#
# except(NameError,IOError) as result: result 将可能产生的所有异常类型,都放到下面的小括号中
# print(result)
# -----test-----1--
# ------test-----2--
# name 'num' is not defined
#except Exception as result: 可以承接任何异常
import time
try:
f = open("text1.txt","r")
try:
while True:
content = f.readline()
if len(content) == 0:
break
time.sleep(2)
print(content)
finally:
f.close()
print("文件关闭")
except Exception as result:
print("发生异常")```