# 第四週筆記
## python與數學公式:
### 二元一次方程式聯立求解
```
from sympy import *
x, y = symbols('x y')
#二元一次聯立方程式
f1 = Eq(x + 2*y - 8, 0)
f2 = Eq(2*x - y - 6, 0)
#方程式畫出圖
#函數f(x)=...的畫圖用:plot(f1,line_color='red')
#方程式的畫圖用:plot_implicit(eq1,line_color='red')
#語法:plot_implicit(expr, x_var=None, y_var=None, adaptive=True, depth=0, points=300, line_color='blue', show=True, **kwargs
#預設只能畫出一個圖
#錯誤指令:plot_implicit(f1, f2)
#進階:同時畫出兩個方程式equation的方法
#使用plot_implicit(Eq()畫出兩個圖的方法:使用extend(p2),最後再p1.show())
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.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 = symbols('x y')
f = poly('(x+2*y+3)**2')
display(f)
```

### 解微分、微積分公式
```
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))
#常見的幾類微積分基本公式
f = sin(x)
print('求sin(x)的微分=', diff(f, x))
f = cos(x)
print('求cos(x)的微分=', diff(f, x))
#標準指數e的微分
f = E**x
print('求E**x的微分=', diff(f, x))
f = exp(x)
print('求exp(x)的微分=', diff(f, x))
#自然對數e的微分
#自然對數 = ln()
#10為底的對數 = log()
f = ln(x)
print('求自然對數ln(x)的微分=', diff(f, x))
#多項式的微分
#如果方程式有未知參數n,就必須要加上''
f = 'x**n'
print('求x**n的微分=', diff(f, x))
f = x**5
print('求x**5的微分=', diff(f, x))
```

## 自行建立變數:
```
username = input("Enter username:")
print("Username is: " + username)
```

## 變數型態轉換:
```
a = input("Enter a age:")
b = input("Enter a age:")
a=int(a)
b=int(b)
print(a+b)
```

## 顯示變數型態:
```
fruit=['watermelon','apple','strawberry','banana','grape','pineapple']
print(type(fruit))
print(fruit)
```

## 變數命名原則與傳統:
### 原則:
Legal:
```
myvar = "John"
my_var = "John"
_my_var = "John"
myVar = "John"
MYVAR = "John"
myvar2 = "John"
```
Illegal:
```
2myvar = "John"
my-var = "John"
my var = "John"
```
### 傳統:
Camel Case:
```
myVariableName = "John"
```
Pascal Case:
```
MyVariableName = "John"
```
Snake Case:
```
my_variable_name = "John"
```
## 指定變數內容:
```
thislist = ["apple", "banana", "cherry"]
print(thislist)
```

## 串列的操作:
### List Length:
```
thislist = ["apple", "banana", "cherry"]
print(len(thislist))
```

### Access Items:
```
thislist = ["apple", "banana", "cherry"]
print(thislist[1])
```

```
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[2:5])
```

```
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[:4])
```

### Change Item Value:
```
thislist = ["apple", "banana", "cherry"]
thislist[1:3] = ["watermelon"]
print(thislist)
```

### Append Items:
```
thislist = ["apple", "banana", "cherry"]
thislist.append("orange")
print(thislist)
```

### Insert Items:
```
thislist = ["apple", "banana", "cherry"]
thislist.insert(2, "watermelon")
print(thislist)
```

### Remove Specified Item:
```
thislist = ["apple", "banana", "cherry"]
thislist.remove("banana")
print(thislist)
```

### Sort List Alphanumerically:
```
thislist = ["orange", "mango", "kiwi", "pineapple", "banana"]
thislist.sort()
print(thislist)
```

## 老師出題
```
lst=[60474,17966,96949,7521,93184,13290,80559,23407,32934,71592,53071,34576,73225,3462,78216,59119,30438,76030,79730,52719,98568,15524,58507,29695,72782,17540,20441,10752,10368,15180,22476,89952,55597,79926,68829,14250,32526,63262,61107,48936,610,57348,47497,81307,8367,23222,88503,28267,99952,23017,65085,47323,49821,32584,30972,9306,92592,61028,60358,39581,43770,94085,2831,98577,9931,58366,10600,40871,33713,1943,66767,9008,38609,50848,35469,15344,56239,86374,92828,55399,81438,35771,28739,24107,73219,89005,20861,92589,78616,96892,50728,45848,14244,8317,41248,36275,44338,26250,65693,20100,49674,12638,77506,79208,92274,25047,44884,26438,71968,27278,84019,75581,45024,47008,29499,94332,67852,46627,75689,38086,66675,55510,99517,60920,16406,87296,21488,18666,46501,7931,37377,94523,24889,28897,10315,82434,80519,29107,44336,59352,23395,73953,21217,19202,90315,48111,27022,92919,96696,22907,98326,75601,51744,78102,69792,36919,31270,23196,22309,569,94654,20295,37463,8919,99690,88852,90442,60094,69823,19703,64637,99867,39237,80980,74844,39017,18123,17988,18758,78634,3900,60379,55664,75615,95607,84908,56887,37708,58283,36994,71767,34062,55478,89828,69537,18160,35602,29494,30669,15612,52730,83730,36887,14528,59823,10236,58379,14673,51888,33971,48235,65681,17805,89998,74435,35827,78983,91095,57954,26058,66204,6975,97470,77037,47456,9607,69076,32617,26242,45940,94631,94257,45348,26917,3597,5704,69491,51613,67360,35126,93154,78994,99492,4162,63247,614,83821,33381,10056,34381,71705,74457,88928,50610,78013,41957,21557,82585,30466,39203,51609,88352,2244,34762,45872,91011,17088,6790,67257,26855,48072,48567,64293,40010,91290,3476,97079,6450,82581,91910,15534,61864,21908,65746,39500,82936,63954,88610,43249,39700,10793,66872,79996,92659,80228,9448,36590,61013,63469,26326,45445,47884,73967,21421,41899,19425,59210,36787,46378,56889,21495,43109,39921,9629,63582,19728,59478,34575,13151,3160,80212,81673,12398,29540,46928,50948,92761,86367,18091,63985,16796,75872,29856,57874,76781,92032,77341,67395,1210,26808,97008,73529,21358,65406,57923,74493,33171,21373,39342,75194,73467,69751,7697,97136,26415,70025,67578,59263,9695,75549,3704,83015,44715,6355,94385,99832,52454,83896,95925,72639,63444,94120,53960,8562,78817,28772,19604,6988,95929,95999,14315,86760,76211,23417,57539,5172,22222,22066,86309,80030,10268,68539,88993,30638,64092,25792,25528,72472,85757,28298,58715,14301,93604,40702,67783,55978,82709,73851,75038,25531,38871,17120,19271,82070,46602,91853,6345,91049,88101,15101,11779,6937,14933,74936,86133,89392,39264,3452,67439,82525,31475,48481,35519,98495,7668,84749,50908,36193,15074,9943,32107,2454,78780,17441,73188,52262,12251,26378,88538,64900,26907,21220,42911,80689,19391,90065,52189,52659,84670,88416,49692,35312,52707,38748,94510,66022,38210,46574,30275,86702,94672,6859,40450,89265,9116,18408,43824,87379,43610,87405,57130,70461,7967,81441,86876,83236,23967,58510,72518,32186,3873,70751,75079,22076,88073,8094,4970,93711,30998,20906,76629,47752,84911,31192,92113,75978,916,56421,55344,49046,6966,906,559,97913,28989,28883,6673,60028,80743,31948,71555,84297,12306,35094,66324,86015,88965,22594,46507,50596,87151,78700,68888,60814,89127,73980,58956,90709,26727,77493,7075,16224,57058,67500,66243,39027,32113,46728,60235,55317,55742,48032,55701,97697,20978,92715,37691,66691,35911,61606,82544,96662,36618,66005,44064,83106,7252,57263,90880,86945,97148,93141,7807,2158,94743,53134,52161,44451,83301,26224,67045,41691,36845,96128,56995,96052,49665,77379,21994,12101,50781,20754,31528,14421,60652,61289,87165,43245,81328,99739,67422,62196,62054,26905,64093,37926,46451,15815,62532,32190,92588,51428,59333,80126,84301,6168,35580,41369,59051,63554,53901,40095,62267,89278,16920,97229,52355,28772,6914,3501,85171,98735,76454,96349,46418,28947,541,91375,7952,44510,93425,63661,12004,57208,7070,93393,57434,42489,83896,45284,88835,29183,80434,13657,82776,1603,41967,68000,45223,96843,48292,67776,58436,99792,31665,52951,5522,99140,76258,73437,26591,61873,71810,67653,94574,79167,30600,86199,79441,56074,67194,89844,41060,20481,9736,13111,82822,99654,84090,2789,52637,36230,77089,21133,45263,54075,20490,78147,79307,94458,21291,32662,54777,77369,20061,37415,90747,3986,86743,71143,56036,30041,87021,71480,27905,74425,91619,34398,84115,29960,93170,53829,59936,32507,18886,25900,10733,50702,67533,84141,53024,79655,83924,14916,93701,24564,84376,1008,6671,33780,62350,68628,70665,12685,9513,24293,41579,56848,57664,82633,35302,46170,12039,25451,52995,68637,32448,21123,18192,60572,75160,36053,56135,66903,26351,50447,52485,43290,45181,46527,17594,64269,20066,32353,73597,85298,1735,37705,93019,93115,99872,23406,41006,26152,73624,88968,44229,11071,22834,40419,37824,19242,30767,37724,34603,44863,53402,14986,84614,22704,55032,7696,79197,90976,54602,17729,72995,88492,53301,77950,19428,29406,90649,29720,43448,17250,29641,87767,37312,78671,52406,59912,98235,97169,14969,35170,56749,71954,6300,66,74337,80048,86325,43251,60685,35196,81393,55588,29910,74193,62810,89124,51834,35790,71976,29880,33740,50152,53503,70096,37842,39555,42783,85955,66714,89907,58554,46412,35883,79642,39000,7557,90735,32707,67678,94646,71467,98581,60029,46083,20384,54084,61437,93525,29061,74883,69115,48006,72679,84083,80011,91330,93250,56278,72601,89269,78193,80427,70237,37724,60329,77822,97563,34200,10799,45878,53492,13908,21883,11098,92929,33389,5176,38927,53380,23667,57248,75461,96729,41616,23908,73188,47394,83216,15678,14966,89285,78562,18500,65491,15701,36043,29544,90224,55044,2805,55240,4076,62952,59260,35103,73696,72740,27671,56186,81530,54220,78740,18390,1788,65513,20311,25507,94132,85910,72418,32787,40910,65682,73274,91053,92856,24527,59648,9922,21190,35254,19773,20016,14120,43837,97898,5071,68705,15689,77188,45124,41379,8266,59847,72765,96880,97889,42809,7592,59273,253,52574,23282,56491,24496,94267,83905,56776,99059,5045,47520,60227,14569,71282,91760,99493,78543,96843,83285,33663,52042,44303,58556,11784]
lst.sort()
print(lst)
```