# 请不要修改示例 print(5 / 8) print('这是我的第一个程序') # 请打印一些消息 print('Hello') #print('Python') # 计算阴影部分面积面积 print(4*4-3*2) # 创建一个输入指令,并将其放在输出指令的括号中。 print(input('请输入')) # 创建存储数字的变量 number=50 print(number) # 打印出变量,查看变量的值 # 1.创建表示长的变量 length 并赋值 6 length=6 # 2.创建表示宽的变量 width 并赋值 4 width=4 # 3.创建表示长方形周长的变量 perimeter,并计算 perimeter=length+width+length+width # 4.将周长打印输出。 print(perimeter) # 1.创建表示体重的变量 weight 并赋值 65 weight=65 # 2.创建表示身高的变量 height 并赋值 1.76 height=1.76 # 3.创建表示BMI值的变量 bmi 并计算 bmi=weight/height**2 # 4.将 bmi 打印输出 print(bmi) # 创建变量 string string='这是一个字符串' # 创建变量 bools bools=True num = 10 type1=type(num) type2=type(float(num)) type3=type(str(num)) type4=type(bool(num)) data1 = 15 data2 = 2.5 data3 = 'python' # 计算 data1 + data2,将结果保存在 result1 中 result1=(float(data1)+data2) # 将 result1的类型 打印出来 print(type(result1)) # 将 data1 和 data3 组合在一起,并将结果保存在 result2 中 result2=(str(data1)+data3) # 打印出result2 print(result2) # 记录总价格 price = 0 # 运动鞋价格 sneakers = 300 price +=sneakers print('此时的总价格为:', price) # 计算购买毛衣后的总价格,并打印输出 sweater=200 price+=sweater print('此时的总价格为:',price) # 计算购买书后的总价格,并打印输出 book=50 book_num=3 price+=book*book_num print('此时的总价格为:',price) # 创建函数并打印输出。 def func(): print('这个函数计算数字的平方') # 调用函数,点击运行。 func() # 改变函数。 def func(num): square=num**2 print(square) # 调用函数,并传递参数 3。 func(3) # 给函数添加返回值。 def func(num): square = num * num return square # 将调用的结果保存到变量 result 中。 result = func(3) print(result) # 1.给函数增加一个参数。 def func(num,index): square = num**index return square # 1.调用函数,传递参数:2,3。 result = func(2,3) print(result) # 2.将传递的参数位置改变为:3, 2。并打印输出。 result=func(3,2) print(result) # 1.求指数的函数。 def func(num,index): square = num ** index return square # 1.调用函数,并传递参数:3, 2。 result = func(3, 2) print(result) # 2.调用函数,给定关键字 index = 3, num = 2。 result=func(2,3) print(result) # 1.给参数 index 添加默认值 2。 def func(num, index=2): square = num ** index return square # 1.调用函数,传递参数:num = 3。 result = func(index=2,num=3) print(result) # 2.调用函数,传递参数:num = 3, index = 3。 result=func(num=3,index=3) print(result) # 请编写fib函数 def fib(num): if num==1: return 1 if num==2: return 1 else: return fib(num-1) + fib(num-2) def check_triangle(a,b,c): if a>0 and b>0 and c>0: if(a+b>c)and(a+c>b)and(b+c>a): return 1 else: return 0 else: return -1 # 请创建变量 number 并且赋值10 number=10 # 编写程序的 if 语句 if number<0: number=-number print(number) # 编写程序的 else 语句 else: print(number) # 请创建变量 score 并计算平均成绩 def score(a,b,c): d=(a+b+c)/3 return d score=score(77,90,85) print(score) # 编写程序的判断过程 if score>=90: print("A等") elif score>=80: print("B等") elif score>=70: print("C等") elif score>=60: print("D等") else: print("E等") bmi = 50 if bmi < 18.5: message = '过轻' elif bmi < 24: message = '正常' # 请在此处继续编写代码 elif bmi<28: message = '超重' elif bmi<30: message = '一级肥胖' elif bmi<40: message = '二级肥胖' else: message = '三级肥胖' print(message) a = 10 b = 20 # 将表达式的结果保存在 istrue 中。 istrue=(a + b) % (b - a) >= a ** (a / b) and b > a # 将 istrue 打印输出。 print(istrue) bbaaca # 请在此处编写你的程序 i = 1 a = '*' while i <= 5: print(a) a+='**' i+=1 # 请在此处编写你的程序 a = " " c = "*" i = 4 j = 0 while i>=0: b = i*a d = 2*j*c+"*" print(b+d) i -= 1 j += 1 # 请在此处编写你的程序 a = " " c = "*" i = 3 j = 0 while i>=0: b = i*a d = 2*j*c+"*" print(b+d) i -= 1 j += 1 i = 1 while i<4: b = i*a d = 2*(3-i)*c+"*" print(b+d) i +=1 # 请在此处编写你的程序 sum=0 for i in range(1,91): sum+=i i+=1 print(sum) # 请在此处编写你的程序 word='happy' string='I am very happy to learn Python.' isin=word in string print(isin) # 请在此处编写你的程序 sums=0 i=0 while i<100: sums+=i i+=2 print(sums) # 请在此处编写你的程序 for i in range(1,9+1): for j in range(1,i+1): print("%dx%d=%d\t"%(j,i,i*j),end=" ") print() # 添加数据 178 到列表末尾 heights = [175, 180, 170, 165,178] # 改正错误 new_heights = [65,32,48,92] orders = ['苹果', '香蕉'] # 1.将 orders + ['西瓜', '葡萄'] 的结果赋给 new_orders。 new_orders=orders+['西瓜', '葡萄'] # 2.修改错误 broken_prices = [5, 3, 6, 4, 5] + [3] # 3.初始化列表 emplist。 emplist=[None]*10 print(emplist) employees = ['张三', '李四', '王五', '赵六', '钱七', '周八', '孙九', '刘大', '冯二'] # 取出索引为4的元素,并保存到 index4 中。 employees[4] index4= '钱七' # 打印出列表 employees 的长度。 print(len(employees)) # 运行代码,更改错误。 print(employees[8]) suitcase = ['衬衫', '裤子', '睡衣', '裤子', '书', '衬衫'] beginning = suitcase[0:2] # 1.输出列表 beginning 的长度 print(len(beginning)) # 2.修改 beginning,取出 suitcase 的前4个元素 beginning = suitcase[0:4] # 3.创建新列表 middle=suitcase[2:4] shopping_list = ['鸡蛋', '黄油', '牛奶', '黄瓜', '果汁', '麦片'] # 1.输出列表长度 print(len(shopping_list)) # 2.使用 -1 取出列表最后一位元素,并保存到 last_element 中 last_element=shopping_list[-1] # 3.取出索引为 5 的元素,并保存到 element5 中 element5=shopping_list[5] # 4.打印输出 element5 和 last_element。 a=element5 [0:] b=last_element[0:] print("element5的内容为:%s,last_element的内容为:%s"%(a,b)) # 1.创建保存邀请名单的列表并保存数据 invitation=['张明','李华','王风'] # 2.将李华替换成赵胜 invitation[1]='赵胜' # 3.添加邀请嘉宾 invitation.insert(0,'张三') invitation.insert(3,'李四') invitation.append('王五') # 4.将嘉宾分桌 table1=invitation[0:3] table2=invitation[3:6] # 5.发出邀请 for i in invitation[0:3]: a='table1' print("邀请 %s 来参加我的生日会,你将坐在 %s 桌。"%(i,a)) for m in invitation[3:6]: b='table2' print("邀请 %s 来参加我的生日会,你将坐在 %s 桌。"%(m,b)) scores = [96, 75, 61, 82, 64, 49] print(scores) # 2.计算总成绩,并输出 score_sum=sum(scores) print("总成绩为:%s"%(score_sum)) # 3.计算学生个数,并输出 count_num=len(scores) print("学生个数为:%s"%(count_num)) # 4.计算平均分,并输出 score_avg=score_sum/count_num print("平均分为:%s"%(score_avg)) addresses = ['朝阳区', '海淀区', '丰台区', '大兴区', '通州区'] cities = ['北京', '上海', '天津', '重庆', '澳门', '深圳'] sorted_cities = cities.sort() # 1.使用 sort 对 addresses 排序 addresses.sort() # 2.打印 addresses print(addresses) # 3.打印 sorted_cities print(sorted_cities) games = ['Portal', 'Minecraft', 'Pacman', 'Tetris', 'The Sims', 'Pokemon'] # 使用 sorted 对 games 排序,并将结果赋值给 games_sorted games_sorted=sorted(games) # 输出 games 和 games_sorted。 print("列表games:%s"%games) print("列表games_sorted:%s"%games_sorted) citys = ['北京', '廊坊', '天津', '沧州'] # 将列表反转 citys.reverse() # 循环打印 for i in range(1,5): print("第 %s 站,我到了 %s 旅游"%(i,citys[i-1])) classes = [ ['张伟', '李华', '王明'], ['张三', '李四', '黄骅'], ['钱倩', '尹旷', '王五'], ['唐三', '戴全', '马红'] ] # 将每个元素保存到新列表中。 playground = [] for i in range(0,4): for j in range(0,3): playground.append(classes[i][j]) for i in range(0,12): print(playground[i]) # 请在这里编写你的程序 a=input("是否输入(y/n):") if a =='y': b=input("请输入姓名") c=input("请输入年龄") d=input("请输入性别") e=input("是否输入(y/n):") if e=='y': f=input("请输入姓名") g=input("请输入年龄") h=input("请输入性别") i=input("是否输入(y/n):") if i =='n': print([[b,c,d],[f,g,h]]) list1 = [23, 95, 87, 12, 33, 75, 66, 41] # 请在这里编写你的程序 n = len(list1) for x in range(n-1): for y in range(x+1,n): if list1[x]>list1[y]: list1[x],list1[y]=list1[y],list1[x] print(list1) # 将分数手动存储到元组中 soc_tuple=(58,67,72,69) print(soc_tuple) soc_tuple = (58, 67, 72, 69) # 将元组 soc_tuple 转换为列表 soc_list。 soc_list=list(soc_tuple) # 修改列表第一个数据,将该数据加 5 后保存到原位置。 soc_list[0]=soc_list[0]+5 # 将列表 soc_list 转换为元组 soc_tuple2。 soc_tuple2=tuple(soc_list) # 请创建字典soc_dict soc_dict = {'张三':58,'李四':67,'王五':72,'赵六':69} sign = {'双鱼': '2.19-3.20', '天蝎': '10.24-11.22', '金牛': '4.20-5.20', '白羊': '3.21-4.19', '双子': '5.21-6.21'} # 请编写你的代码 date = sign['天蝎'] print(date) # 请创建字典profile 存储信息 profile = { '张三' : {'性别':'男', '身高':'178', '籍贯':'北京'}, '李四' : {'性别':'女', '身高':'155', '籍贯':'上海'}, '王五' : {'性别':'女', '身高':'162', '籍贯':'深圳'}, '赵六' : {'性别':'男', '身高':'168', '籍贯':'重庆'} } diction = {'张三': '双鱼座', '李四': '白羊座', '王五': '摩羯座'} diction['尹旷'] = '天蝎座' diction['李四'] = '双子座' del diction['李四'] # 每位同学选的课程 zhangsan = ['Python', 'Java', 'PHP'] lisi = ['C++', 'C#', 'Java'] wangwu = ['Ruby', 'Python', 'SQL'] zhangliu = ['Java', 'Python', 'Basic'] # 请统计课程 course = set(zhangsan) | set(lisi) | set(wangwu) | set(zhangliu) python = {'张三', '李四', '王五'} python.add('小明') python.remove('张三') ruby = {'张三',} # 已知两个集合 x = {1, 3, 5, 7, 9} y = {1, 2, 3, 4, 5} intersection = x & y union_set = x | y difference_set = x - y # 将字符串保存在 string 中 string = 'They say "this\'s a string"' # 将字符串打印输出 print(string) # 转义的字符串 address = 'C:\\Users\\Administrator\\Nopper' print('转义字符串:', address) # 请创建原始字符串,并输出 原始字符串:**** address_r = r'C:\Users\Administrator\Nopper' print('原始字符串:', address_r) # 将文本保存到变量 lines_string 中,并输出。 lines_string = '''你好! python''' print(lines_string) # 1.将你的名字的拼音保存到 name 中,并用空格分隔 name = 'zhangsan' # 2.将 name 的第 2 到第 4 位字符保存到 name1 中。 name1 = name[1:4] # 3.将 name 的倒数第 2 到倒数第 4 位字符保存到 name2 中。 name2 = name[-4:-1] # 4.将 name1 和 name2 打印输出。 print('name1:%s'%(name1)) print('name2:%s'%(name2)) song_title = "you are not alone" song_author = "Michael Jackson" # 1.将歌名制作成标题格式并保存 song_title_fixed = song_title.title() # 2.打印 song_title 和 song_title_fixed print('song_title:%s'%(song_title)) print('song_title_fixed:%s'%(song_title_fixed)) # 3.将作者名设为全部大写并保存 song_author_fixed = song_author.upper() # 4.打印输出 song_author 和 song_author_fixed print('song_author:%s'%(song_author)) print('song_author_fixed:%s'%(song_author_fixed)) string = 'I like Pyrhon, Pyrhon is very powerful, and Pyrhon can do a lot of things.' # 将 Pyrhon 改为 Python 并保存到 string_fixed 中 string_fixed = string.replace('Pyrhon','Python') lyric = 'Another day has gone' # 将 lyric 分割,并保存到列表 line_one_words 中。 line_one_words = lyric.split() # 将列表中的单词拼接成字符串 lyric1。 lyric1 = ''.join(line_one_words) # 请编写函数 poem_title_card。 def poem_title_card(poet,title): string = '该诗《{}》的作者是{}'.format(poet,title) return string lyric = " Another day has gone!!!!!! " # 1.删除 lyric 中没必要的空格,并保存到 lyric1 中。 lyric1 = lyric.strip() # 2.删除 lyric1 中没必要的感叹号(!),并保存到 lyric2 中。 lyric2 = lyric1.strip('!') # 请创建Person类 class Person: pass # 请创建Person类的对象person class Person: def __init__(self): print("一个脑袋") print("一双手") print("两条腿") bmw=Person() # 请创建Person类 class Bird: def __init__(self, beak, wing, claw): print(beak) print(wing) print(claw) bird = Bird("有鸟喙","有一双翅膀","有一对爪子") # 请编写代码 class Fruit: name = '水果' color = '每种水果都有颜色' taste = '每种水果味道都不一样' def __init__(self): print(self.name) print(self.color) print(self.taste) fruit = Fruit() # 请创建Person类 class Bird: def eat(self, eat): print(eat) def fly(self, fly): print(fly) def sleep(self, sleep): print(sleep) bird = Bird() bird.eat("必须得吃东西才能活下去") bird.fly( "翅膀就是用来飞的") bird.sleep("不睡觉就没有精神") # 请编写代码 class Fruit: name = '水果' color = '每种水果都有颜色' taste = '每种水果味道都不一样' def __init__(self): print(self.name) print(self.color) print(self.taste) fruit = Fruit() # 请创建Person类 class Bird: def eat(self, eat): print(eat) def fly(self, fly): print(fly) def sleep(self, sleep): print(sleep) bird = Bird() bird.eat("必须得吃东西才能活下去") bird.fly( "翅膀就是用来飞的") bird.sleep("不睡觉就没有精神") #请创建Person类 class Fruit: __name ='水果名称:' def __init__(self, name): print(self.__name + name ) fruit = Fruit("apple") # 定义 TVshow 类 class TVshow: list_film = ['战狼', '大闹天宫', '流浪地球', '熊出没'] def __init__ (self, show): self.__show = show @property def show(self): return self.__show @show.setter def show(self, value): self.__show = value # 创建验证实例 tvshow = TVshow('战狼') print('当前播放:', tvshow.show) tvshow.show = '熊出没' print('当前播放:', tvshow.show) class Fruit: color = '绿色' def harvest(self, color): print('水果是:' + color + '的!') print('水果已经收获...') print('水果原来是:' + Fruit.color + '的!') # 继续编写你的代码 class Apple(Fruit): color = "红色" def __init__(self): print("这是苹果" ) class Orange(Fruit): color = "橙色" def __init__(self): print("这是橘子") apple = Apple() orange = Orange( ) # Apple 类添加属性color 赋值为红色,构造方法打印这是苹果 # Orange类添加属性color 赋值为橙色,构造方法打印这是橘子 class Person: def __init__(self, limb='双手双脚'): self.limb = limb # 编写你的代码 class Asian(Person): def __init__(self): super().__init__() print("亚洲人的肤色是黄色的,正常人都有" + self.limb) class Caucasian(Person): def __init__(self): super().__init__() print("欧洲人的肤色是白色的,正常人都有" + self.limb) # 实例化类 asian = Asian() european = Caucasian() class Fruit: color = '绿色' def harvest(self, color): print('水果是:' + color + '的!') print('水果已经收获...') print('水果原来是:' + Fruit.color + '的!') # 请继续编写你的代码 class Orange(Fruit): def harvest(self, color): print('橘子是: ' + color + '的! ') print( '橘子已经收获...') print( '橘子原来是:' + self.color+ '的! ') orange = Orange() orange.harvest( "橙色")