# 0307 ## 上課筆記 >註解說程式語法 同起點就是同段落 :判斷式到此為止 if 5 > 2: print("Five is greater than two!") >type() 型態 x=5 type(x) print(type(x)) >"字串" 沒有""就是數字 x=5 y="hi" print(x) print(y) >字串對字串作加法=字串接上字串 字串對數字作加法會無法成立 x="5" y="3" print(type(x)) print(type(y)) x+y >#引用現成的模組 from sympy.interactive import printing printing.init_printing(use_latex=True) from sympy import * x = symbols('x') #一元多項式,相乘,展開 f = poly((x+1)*(x+1)*(x+1)) print(f) display(f) #二元多項式 x,y,z = symbols('x y z') f = poly('(x+2*y+3)**2') display(f) >#範例3-4:二元一次方程式聯立求解 from sympy import * x, y = symbols('x y') #二元一次聯立方程式 f1 = Eq(x + 2*y - 6, 1) f2 = Eq(2*x - y - 3, 3) p1 = None p1 = plot_implicit(f1,show=False) p2 = plot_implicit(f2,show=False) p1.extend(p2) p1.show() #求解兩條線的交叉點(solve(f1,f2)) print('解二元一次聯立方程式=', solve((f1,f2),(x,y))) >from sympy import * x, y = symbols('x y') #二元一次聯立方程式 f1 = Eq(x**2 + y**2-12, 0) f2 = Eq(2*x - y - 6, 0) #畫出圖 #使用plot_implicit(Eq()畫出兩個圖的方法:使用extend(p2),最後再p1.show()) p1 = None p1 = plot_implicit(f1,(x,-5,5),(y,-5,5),show=False) p2 = plot_implicit(f2,(x,-5,5),(y,-5,5),show=False) p1.extend(p2) p1.show() #求解兩條線的交叉點(solve(f1,f2)) print('解二元一次聯立方程式=', solve((f1,f2),(x,y))) from sympy.interactive import printing printing.init_printing(use_latex=True) display(solve((f1,f2),(x,y))) >from sympy import * x=symbols('x') f = Function('f')(x) #一階導數(對x) = 一階微分(對x) = diff(函數, x) f = 1/x print('求y=1/x的微分=', diff(f, x)) f = 3*x**2 + 2*x + 5 print('求 y=3*x^2 + 2*x + 5的微分=', diff(f, x)) >fruits = ["apple", "banana", "cherry"] x, y, z = fruits print(x) print(y) print(z) >""字串處理 >a=input("enter a age") b=input("enter b age") print(a+b) enter a age10 enter b age10 1010 >前a為字串型態,後a設定int會轉為數字型態 >a=input("enter a age") b=input("enter b age") a=int(a) b=int(b) print(a+b) enter a age10 enter b age10 20 >[]串列 apple元素 ,容器 ''編號 >fruit=['watermelon','apple','strawberry','banana','grape','pineapple'] >len在看串列有多長有幾個 list和[]都是串列 可放數字字串也可混搭 >thislist = ["apple", "banana", "cherry"] print(thislist) >apple是0 banana是2 -1是cherry 從0開始 >thislist = ["apple", "banana", "cherry"] print(thislist[1]) >thislist = [5, 7, 1, 2, 8] thislist.sort() print(thislist)
{"metaMigratedAt":"2023-06-16T22:03:43.190Z","metaMigratedFrom":"Content","title":"0307","breaks":true,"contributors":"[{\"id\":\"4dfebb48-62e3-4093-b22e-bde00bb0bc86\",\"add\":2150,\"del\":8}]"}
Expand menu