1 ''' 2 #華氏轉換攝氏 3 f = float(input('清輸入華氏溫度:')) 4 c = (f - 32) / 1.8 5 print('%.1f華氏度 = %.1f攝氏度' % (f, c)) 6 ''' 7 8 ''' 9 #使用變量保存數據並進行運算 10 a = 123 11 b = 321 12 print(a + b) 13 print(a - b) 14 print(a * b) 15 print(a / b) 16 print(a // b)#向下取整除 17 print(a ** b)#冪 18 print(a % b)#模除 19 ''' 20 21 ''' 22 #使用type()函數檢查數據的類型 23 a = 12 24 b = 12.44 25 c = 12 + 6j 26 d = '劉詩琪我喜歡你啊' 27 e = True 28 print(type(a))#int 29 print(type(b))#float 30 print(type(c))#complex 31 print(type(d))#str 32 print(type(e))#bool 33 ''' 34 35 ''' 36 #使用input()函數獲取鍵盤輸入(字符串) 37 #使用int()函數將輸入的字符串轉換成整數 38 #使用print()函數輸出帶佔位符的字符串 39 a = int(input(' a = ')) 40 b = int(input(' b = ')) 41 print('%d + %d = %d' % (a, b, a + b)) 42 print('%d - %d = %d' % (a, b, a - b)) 43 print('%d * %d = %d' % (a, b, a * b)) 44 print('%d / %d = %f' % (a, b, a / b)) 45 print('%d // %d = %d' % (a, b, a // b)) 46 print('%d %% %d = %d' % (a, b, a % b)) 47 print('%d ** %d = %d' % (a, b, a ** b)) 48 #上面的print函數中輸出的字符串使用了佔位符語法,其中%d是整數的佔位符,%f是小數的佔位符,%%表示百分號(由於百分號表明了佔位符,因此帶佔位符的字符串中要表示百分號必須寫成%%),字符串以後的%後面跟的變量值會替換掉佔位符而後輸出到終端中 49 ''' 50 51 ''' 52 #賦值運算符和複合賦值運算符 53 a = 10 54 b = 3 55 a += b#a = a + b 56 a *= a + 2#a = a * (a + 2) 57 print(a) 58 ''' 59 60 ''' 61 #比較、邏輯和身份運算符的使用 62 flag0 = 1 == 1 63 flag1 = 3 > 2 64 flag2 = 2 < 1 65 flag3 = flag1 and flag2 66 flag4 = flag1 or flag2 67 flag5 = not (1 != 2) 68 print('flag0 =', flag0) 69 print('flag1 =', flag1) 70 print('flag2 =', flag2) 71 print('flag3 =', flag3) 72 print('flag4 =', flag4) 73 print('flag5 =', flag5) 74 print(flag1 is True) 75 print(flag2 is not False) 76 ''' 77 78 ''' 79 #輸入半徑計算圓的周長和麪積 80 import math 81 radius = float(input('請輸入圓的半徑:')) 82 permeter = 2 * math.pi * radius 83 area = math.pi * radius ** 2 84 print('周長:%.2f' % permeter) 85 print('面積:%.2f' % area) 86 ''' 87 88 89 #輸入年份判斷是否是閏年 90 year = int(input('請輸入年份: ')) 91 is_year = (year % 4 == 0 and year % 100 != 0) or \ 92 year % 400 ==0 93 print(is_year)
1 """ 2 用Python的turtle模塊繪製國旗 3 """ 4 import turtle 5 6 7 def draw_rectangle(x, y, width, height): 8 """繪製矩形""" 9 turtle.goto(x, y) 10 turtle.pencolor('red') 11 turtle.fillcolor('red') 12 turtle.begin_fill() 13 for i in range(2): 14 turtle.forward(width) 15 turtle.left(90) 16 turtle.forward(height) 17 turtle.left(90) 18 turtle.end_fill() 19 20 21 def draw_star(x, y, radius): 22 """繪製五角星""" 23 turtle.setpos(x, y) 24 pos1 = turtle.pos() 25 turtle.circle(-radius, 72) 26 pos2 = turtle.pos() 27 turtle.circle(-radius, 72) 28 pos3 = turtle.pos() 29 turtle.circle(-radius, 72) 30 pos4 = turtle.pos() 31 turtle.circle(-radius, 72) 32 pos5 = turtle.pos() 33 turtle.color('yellow', 'yellow') 34 turtle.begin_fill() 35 turtle.goto(pos3) 36 turtle.goto(pos1) 37 turtle.goto(pos4) 38 turtle.goto(pos2) 39 turtle.goto(pos5) 40 turtle.end_fill() 41 42 43 def main(): 44 """主程序""" 45 turtle.speed(12) 46 turtle.penup() 47 x, y = -270, -180 48 # 畫國旗主體 49 width, height = 540, 360 50 draw_rectangle(x, y, width, height) 51 # 畫大星星 52 pice = 22 53 center_x, center_y = x + 5 * pice, y + height - pice * 5 54 turtle.goto(center_x, center_y) 55 turtle.left(90) 56 turtle.forward(pice * 3) 57 turtle.right(90) 58 draw_star(turtle.xcor(), turtle.ycor(), pice * 3) 59 x_poses, y_poses = [10, 12, 12, 10], [2, 4, 7, 9] 60 # 畫小星星 61 for x_pos, y_pos in zip(x_poses, y_poses): 62 turtle.goto(x + x_pos * pice, y + height - y_pos * pice) 63 turtle.left(turtle.towards(center_x, center_y) - turtle.heading()) 64 turtle.forward(pice) 65 turtle.right(90) 66 draw_star(turtle.xcor(), turtle.ycor(), pice) 67 # 隱藏海龜 68 turtle.ht() 69 # 顯示繪圖窗口 70 turtle.mainloop() 71 72 73 if __name__ == '__main__': 74 main()
1 #!/usr/bin/env python 2 # -*-coding: UTF-8-*- 3 #畫正方形 4 import turtle 5 6 turtle.pensize(4) 7 turtle.pencolor('red') 8 9 turtle.forward(100) 10 turtle.right(90) 11 turtle.forward(100) 12 turtle.right(90) 13 turtle.forward(100) 14 turtle.right(90) 15 turtle.forward(100) 16 17 turtle.mainloop()
今天算是屁股坐住了,碼了一點,但效率不高,重複的知識也蠻多,明天繼續,go on, go on!!!python
🎑🏞🌅🌄🌠🎇🌁🌌🌉🌃🏙🌆函數