# 請不要修改示例 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( "橙色")