2023年4月24日 星期一

連宸宏期中考VS Code編輯Python圖形使用者介面GUI

期中考

解說

題目

# 集合{},字典{key:value,},元組(),清單或陣列[]
p = ("台積電", "鴻海", "聯發科")
r = {"台積電", "鴻海", "聯發科"}
s = ["台積電", "鴻海", "聯發科"]
t = ["中華電", "台塑化", "台達電"]
d= {2330:"台積電",2317:"鴻海",2454:"聯發科"}
u = s 
v = s.copy()
s = s.extend(t)
print(u)
print(v)
print(len(d))
i = 0
for a in p: #然後嘗試取代p為r,s,t,u
  i = i+1
  print("台灣第" + str(i) + "大的公司是")
  print("    " + a)

w3schools

微軟VS Code編輯Python圖形使用者介面

Python程式碼

from tkinter import * #從函式庫 tkinter 輸入所有 * 方法
from math import *    #從函式庫 math 輸入所有 * 方法
t = (3,4,5,6,7,8,9,10,11,12,16,20) #宣告一元組tuple(...)
tk = Tk()
tk.title("郭台銘視窗使用者介面GUI")
canvas = Canvas(tk, width=500, height=500)
canvas.pack()

def show(event):                        #定義由事件event(按鈕選單)呼叫的函數show
   cx, cy, cr = 210, 210, 200           #宣告圓中心座標cx, cy半徑cr
   x, y =[],[]                          #宣告二陣列[...]
   k = s.get()                          #取得 ge t按鈕選單的選擇變數
   u = 2 * pi / k                       #使用模組 math 圓周率 pi
   for i in range(k):
      x.append(cx + cr*cos(i*u))        #加入陣列的元素
      y.append(cy + cr*sin(i*u))        #使用模組 math 三角函數cos, sin
   for i in range(k-1):
      canvas.create_line(x[i], y[i], x[i+1], y[i+1])
   canvas.create_line(x[k-1], y[k-1], x[0], y[0])   #可考慮增加width寬度,fill顏色
   
def diagonal():
   cx, cy, cr = 210, 210, 200           #宣告圓中心座標cx, cy半徑cr外來學繼承 inheritance
   x, y =[],[]                          #宣告二陣列[...]
   k = s.get()                          #取得 ge t按鈕選單的選擇變數
   u = 2 * pi / k                       #模組 math 圓周率 pi
   for i in range(k):
      x.append(cx + cr*cos(i*u)) #加入陣列的元素
      y.append(cy + cr*sin(i*u))
   for i in range(k):
      for j in range(i+2, k):
         canvas.create_line(x[i], y[i], x[j], y[j], fill="blue", width=3) 
         
s = IntVar(tk)
combo = OptionMenu(tk, s, *t, command = show).pack()         #下拉式按鈕combobox
button = Button(tk, text="對角線", command = diagonal).pack()#按鈕button
tk.mainloop()

VS Code截圖Python

140影片

242影片

2023年4月10日 星期一

連宸宏python陣列array

W3schools陣列截圖

W3schools陣列程式碼

#連宸宏拷貝自 201單元
"""for x in 'Takming':     #迴圈逐字元印出
   print("字母: %s" % x)三引號框起註解
"""
fruits = ['台積電', '鴻海', '聯發科'] #台灣市場價值最高的三公司
for x in fruits:        # 
   print ("公司: %s" % x)
print(fruits)
fruits.append("中華電")
print("使用append")
print(fruits)
fruits.clear()
print("使用clear")
print(fruits)
fruits = ['台積電', '鴻海', '聯發科', '中華電']
chicken = fruits.copy()
#和 chicken = fruits 有何不同?
print(chicken)
fruits.append("中華電")
print(fruits.count("中華電"))
print(fruits.count("台積電"))
for x in fruits:
  print(x) 
  if x == "中華電": #判斷式是否banana
    print('I hate 中華電.')
  if x == "聯發科": #判斷式是否cherry
    print('I like cherry.')
  if x == "鴻海": #判斷式是否apple
    print('You are my sweet apple.')
#體會到只要我有耐心與興趣,我也可以當一個專業的程式開發人員
#連宸宏w3schools練習
#for x in 'Takming gold': #迴圈逐字字元
#   print("字母: %s" % x)
fruits = ['台積電', '鴻海', '聯發科']
for x in fruits:      # 迴圈印出元素
   print ("當前公司: %s"% x)
print ("append") #append在末尾增加
fruits.append("中華電") #append接元素
print(fruits)
print("移除所有元素clear")
fruits.clear() #Removes all the elements from the list
print(fruits)
fruits = ['台積電', '鴻海', '聯發科',"中華電"]
print(fruits)
pig = fruits.copy()
print("輸出pig用copy方法" + str(pig))
print("使用copy和直接賦予值 pig = fruits有何不同? ")
dog = fruits
print("輸出dog直接=" + str(dog))
print("fruits.extend(pig)")
fruits.extend(pig) #extend接陣列或清單
print(fruits)
fruits.extend(['台塑化','台達電','富邦金'])
print(fruits)
#print("幾個台積電? " + str(fruits.count("台積電)))
print(fruits.index("富邦金"))
fruits.insert(10,"國泰金")
print(fruits)
print(fruits.index("富邦金"))
print("fruits元素個數" + str(len(fruits)))
fruits.pop(4)
print(fruits)
fruits.reverse() #反序排列
print(fruits)
fruits.sort() #排序按照字碼
print(fruits)
#體會到只要我有耐心與興趣,我也可以當一個專業的程式開發人員

W3schools陣列方法Array Methods

 Python有一套內建方法(built-in methods).
MethodDescription
append()Adds an element at the end of the list
clear()Removes all the elements from the list
copy()Returns a copy of the list
count()Returns the number of elements with the specified value
extend()Add the elements of a list (or any iterable), to the end of the current list
index()Returns the index of the first element with the specified value
insert()Adds an element at the specified position
pop()Removes the element at the specified position
remove()Removes the first item with the specified value
reverse()Reverses the order of the list
sort()Sorts the list