#输出1-100连续数字
# for i in range(1,101):
# print(i)
#输出100-1且间隔的为2数字
# for i in range(100,1,-2):
# print(i)
#使用continue输出数字1 2 3 4 5 6 8 9 10
# for i in range(1,11): #无空格版
# if i == 7:
# continue
# print(i)
# for i in range(1,11): #有空格
# if i == 7:
# print(' ')
# else:
# print(i)
#打印 1-99之间的所有奇数
# for i in range(1,100,2):
# print(i)
#打印 1-99之间所有能被3整除数
# for i in range(1,100):
# if i%3 == 0:
# print(i)
# 打印 1-99之间所有数总和
# num = 0
# i = 1
# while i < 100:
# num += i
# i += 1
# print(num)
# 打印 1-99之间所有奇数总和
# num = 0
# i = 1
# while i < 100:
# if i%2 == 1:
# num += i
# i +=1
# print(num)
# 打印 1-2+3-4+5-6+7 ...+99 之间所有奇数总和
# i = 1
# num = 0
# while i < 100:
# if i%2 == 1:
# num += i
# else:
# num -= i
# i += 1
# print(num)
# 打印 1-2+3-4+5-6+7 ...+99 之间所有奇数总和排除66的数字
# i = 1
# num = 0
# while i < 100:
# if i != 66:
# if i%2 == 1:
# num += i
# else:
# num -= i
# i += 1
# print(num)
# 字符串格式化 %s字符串 %d数字
# msg = 'my name is %s, my age %d'%('Ebola',23)
# print(msg)
# 用数字循环打印1,10,3
# flag = False
# for i in range(1,10,2):
# print(i)
# if flag:
# break
# for i in range(10,20):
# flag = True
# print(i)
# break
# for i in range(1,10,2):
# print(i)
# if i == 3:
# break
# for i in range(10,13):
# print(i)
# break
######### 索引为奇数值,删除
# li = [11, 22, 33, 44, 66]
# li1 = []
# #还思路偶数位置添加到新列表
# for a,b in enumerate(li):
# print(a,b)
# if a%2 == 0:
# li1.append(b)
# print(li1)
###切片思想
# li = li[0:5:2]
# print(li)
#倒删除
# for i in range(len(li)-1,-1,-1):
# if i%2==1:
# del li[i]
# print(li)
####文件修改实例
# with open('文件使用实例','w',encoding='utf-8') as f ,open('文件使用实例1','w', enconding='utf-8')as f1:
# for i in f:
# i.
###############################################################################
# 1.变量名命名规则(3分)
# 由字母、数字、下划线组成;数字不能开头;不能为python中特殊字符;见名知意,一般用下划线将单词连接
# 2.字节和位关系(2分)
# 1个字节由8个二进制数表示,1个二进制数代表1位,即1byte=8bite
# 3.看代码写结果(2分)
# name = “wupeiqi”
# result = name.upper()
# print(name)
# print(result)
# 输出结果:'wupeiqi' 'WUPEIQI' #字符串功能不改变原内容
#
# 4.“埃博拉”使用utf-8编码时,占用字节以及位数;使用gbk编码时,占用字节以及位数(2分)
# utf-8(汉字占三个字节)时占用9个字节,54位;使用gbk(汉字占两个字节)时占用6个字节,48位
# 查看代码使用占字符
# print(bytes("李泉",encoding='utf-8'))
# print(bytes("李泉",encoding='gbk'))
# 5.简述 一下两段代码的区别?(2分)
# #代码1:
# n1 = “wupeiqi”
# n2 = n1
# #代码2:
# n1 = “wupeiqi”
# n2 = “wupeiqi”
# n1 n2指向同一个内存地址
# n1,n2两个内存地址是完全不同的
# 6.默认字符串 中的10个功能并描述作用(10分)
# (1)upper() 全部变大写
# (2)lower() 全部变小写
# (3)strip() 首尾去空格、制表符及换行符等,指定去除的内容
# (4)split() 将字符串分割
# (5)captalisize() 首字母大写
# (6)isdecimal() 是否全部为数字
# (7)startswith() 是否以什么开头
# (8)endswith() 是否以什么结尾
# (9)lstrip() 左去空格
# (10)rstrip() 右去空格
# (11)replace() 替代
# (12)join() 连接-以一个字符串为连接符连接一个可迭代对象中的内容,内容的每一项必须为字符串
# (13)isdigit() 是否为纯数字
# (14)isalpha() 是否为纯字母
# (15)isspace() 是否为纯空格
# 7.书写布尔值为False的常用值(2分)
# 0 none "" [] () {}
# 8.书写Python3和Python2的三个不同(3分)
# 默认编码不同:py2为ascii,py3为unicode
# 除法不同:py2中"/"得整数商,py3中为正常除法
# print形式不同:py2要不要括号都可以,py3必须要有括号
# 9.简述深浅拷贝(2分)
# 浅拷贝直接使用copy()方法即可,只拷贝第一层数据;深拷贝需要引入copy模块,将数据的所有层都进行拷贝
# 10.分别使用for和while循环实现 1 - 2 + 3 - 4 + 5 …+99 (8分)
# sum=0
# for i in range(1,100):
# if i%2==0:
# sum-=i
# else:
# sum+=i
# print(sum)
#
# sum=0
# i=1
# while i<100:
# if i%2==0:
# sum-=i
# else:
# sum+=i
# i+=1
# print(sum)
#
# 11.使用range实现打印 100,99,98…1,0 (2分)
# for i in range(100,-1,-1):
# print(i)
# 12. 看代码写结果(3分)
#
# n1 = [11,22,33]
# n2 = n1
# n3 = n1.copy()
# n1[1] = 666
# print(n1)
# print(n2)
# print(n3)
#
# 输出结果分别为:[11,666,33] [11,666,33] [11,22,33]
# # 13. 打印 9*9 乘法表(8分)即:
# 1 * 1 = 1
# 1 * 2 = 2 2 * 2 = 4
# 1 * 3 = 3 2 * 3 = 6 …
# …
# 1 * 9 = 9 2 * 9 = 18 …
# for i in range(1,10):
# for j in range(1,i+1):
# print('%d*%d=%d'%(j,i,i*j),end=' ')
# print()
# 14. 判断 一下代码是否正确,如果错误则改正,否则书写结果(4分)
# name = "你{0},{1} 无理 取闹"
# n1 = name.format('冷酷','无情')
# print(n1)
# n2 = name.format(**['冷酷',' 无情']) #错误:列表打散用1个*
# print(n2)
# name = "你{xx},{oo} 无理 取闹"
# n3 = name.format(oo='冷酷',xx='无情')
# print(n3)
# n4 = name.format(*{'xx': '冷酷', 'oo':'无情'}) #字典打散用2个*
# print(n4)
# 15. 计算 用户输入的内容中索引为奇数并且值为数字的个数(7分)
# 方法一 切片
# count = input('请输入内容')
# num = 0
# count = count[1::2]
# for i in count:
# if i.isdigit():
# num += 1
# print(num)
# 方法二
# count = input('请输入内容')
# num = 0
# for i in range(len(count)):
# if i%2==1:
# if count[i].isdigit():
# num += 1
# print(num)
# 16. 实现购物车(8分)
# 功能要求:
# 要求用户输入自己拥有总资产,例如:2000
# 显示商品列表,让用户根据序号选择商品,加入购物车
# 购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。
# 购买成功时,需要打印购物清单
# 商品列表:
# v = [
# {"name": "电脑", "price": 1999},
# {"name": " 鼠标", "price": 10},
# {"name": "游艇", "price": 20},
# {"name": "美 女 ", "price": 998},
# ]
# v = [
# {"name": "电脑", "price": 1999},
# {"name": " 鼠标", "price": 10},
# {"name": "游艇", "price": 20},
# {"name": "美 女 ", "price": 998},
# ]
# lis_car = []
# print('--------------欢迎光临-------------')
# user_money = input('请输入您的金额')
# if int(user_money) > 0:
# while 1:
# for x,y in enumerate(v,1):
# print(x,y['name'].strip(),y['price'])
# stor_num_user = input('请输入您要购买的商品序号/退出请按Q/结算B')
# if stor_num_user.strip().isdigit():
# if 0 < int(stor_num_user) < len(v)+1:
# lis_car.append(v[int(stor_num_user)-1]['price'])
# else:
# print('请输入有效数字')
# elif stor_num_user.strip().isalpha():
# if stor_num_user.strip().upper() == 'B':
# lis_car_money = sum(lis_car)
# if lis_car_money < int(user_money):
# print('剁手成功')
# break
# else:
# print('余额不足')
# break
# else:
# print('输入正确退出按键')
# else:
# print('余额为0,请充值再购物')
# 17.看代码书写结果(3分)
# for i in range(0,5):
# print(i)
# for j in (0,i): #不是range 仔细!!!!
# print(j)
# 代码结果为:0 0 0 1 0 1 2 0 2 3 0 3 4 0 4
# 18.看代码书写结果(3分)
# while True:
# for i in range(10):
# print(i)
# if i == 5:
# continue
# else:
# break
# 输出结果为:0 1 2 3 4 5 6 7 8 9
#
# 19.补充代码(5分)
# 有如下值集合 [11,22,33,44,55,66,77,88,99,90],将所有 大于 66 的值保存 至字典的第 一个key中,将小于 66 的值保存至第二个key的值中。即: {'k1': 大于66的所有值列表, 'k2': 小于66的所有值列表}
# li = [11,22,33,44,55,66,77,88,99,90]
# li1 = []
# li2 = []
# dic = {}
# dic['key1'] = li1
# dic['key2'] = li2
# for i in li:
# if i > 66:
# li1.append(i)
# elif i < 66:
# li2.append(i)
# else:
# pass
# print(dic)
# #思路二:
# li = [11,22,33,44,55,66,77,88,99,90]
# dic={}
# for i in li:
# if i > 66:
# if 'k1' in dic:
# dic['k1'].append(i)
# else:
# dic['k1'] = [i]
# elif i < 66:
# if 'k2' in dic:
# dic['k2'].append(i)
# else:
# dic['k2'] = [i]
# else:
# pass
# print(dic)
# 20.写代码,将列表 li = [11,22,33,44,55]的第一个值和最后一个值相加并插入索引为3的位置(3分)
# li = [11,22,33,44,55]
# a = int(li[1])+int(li[-1])
# li.insert(3,a)
# print(li)
# 21.写代码,有以下数字:1,2,3,4,5,6,7,8 共八个数字,能组成多少个互不相同且无重复的两位数?(8分)
# count = 0
# for i in range(1,9):
# for j in range(1,9):
# if i != j:
# count += 1
# print(count)
# 22.写代码,有以下列表,请找到列表中任意两个数字相加等于9的元素的索引(8分)
# nums = [2,7,11,15,1,8,7]
# # 结果为: [(0,1),(4,5)...]
# li1 = []
# for i in range(len(nums)):
# for j in range(i+1,len(nums)):
# if nums[i] + nums[j] == 9:
# li1.append((i,j))
# print(li1)
# 23. 看代码,书写结果(2分)
# dic = dict.fromkeys(['k1',True,(11,22)],[])
# dic['k1'].append(6)
# dic[('k2')].append(7)
# print(dic)
# {'k1': [6, 7],'k2':[6,7] True: [6, 7], (11, 22): [6, 7]}
# 24.基于文件实现用户登录程序,提示用户输入用户名和密码,检查用户名是否正确。保存用户名密码的文件user.txt
# username = input('请输入用户名')
# with open('user.txt','r',encoding='gbk') as f:
# a = f.readline()
# l1 = a.strip().split('|')
# if l1[0] == username:
# passwd = input('请输入密码')
# if passwd == l1[1]:
# print('登陆成功')
# else:
# print('密码错误')
# else:
# print('账号错误')
# 25. 看代码,书写结果(2分)
# name = "ebola"
# userlist = ['狗狗','露露']
# userlist.extend(name) #迭代去增加
# print(userlist)
# 26.列举int、bool、str、list、tuple、dict、set中可以作为字典的Key的类型(2分)
# int、bool、str、tuple
# 27.转换(6分,每个1分,最后 一个2分)
# # a.将字符串 s = "alex" 转换成列表
# s = "alex"
# lis = list(s)
# b.将字符串 s = "alex" 转换成元祖
# s = "alex"
# val=tuple(s)
# c.将列 表 li = ["alex", "seven"] 转换成元组
# li = ["alex", "seven"]
# def func(*args):
# li = args
# print(li)
# func(*li)
# val = tuple(li)
# d.将元祖 tu = ('Alex', "seven") 转换成列表
# li = ('alex', 'seven')
# val=list(li)
# print(val)
# e.将列表 li = ["alex", "seven"] 转换成字典且字典的key按照10开始向后递增
# li = ["alex", "seven"]
# dic = {}
# x = 0
# for i in range(10,len(li)+10):
# dic[i] = [li[x]]
# x += 1
# print(dic)
# li = ["alex", "seven"]
# dic = {}
# x = 10
# for i in li:
# dic[x] = i
# x += 1
# print(dic)
# # 1.li = [1,2,3,5] 在列表中元素5的前边进行添加一个‘3’
#
# li.insert(-2,'3')
#
# # 2. dic={‘key’:’v1’} dic2={‘key2’:’v2’} 将俩个字典进行合并
# dic.upadte(dic2)
#排序
# lis =[8,5,2,0,4,6]
# lis.sort()
# print(lis)
# # 4.for i in range(3)
# # Print i+1
# 以上代码有什么问题
# 冒号 括号
#
# # 5.a=‘hello world’ 对这个变量进行顺序反转
# a[-1::-1]
# # 6.d={‘k1’:{2:{‘k3’:[{(1,2,3):6},2,3,4]}}}将(1,2,3)对应的值修改成66
# d[k1][2][k3][0][(1,2,3)] = 66