本文採用CC-BY-NC授權
投影片及內文的外部索引資料之版權屬原著作人所有
上課投影片 Day1
上課投影片 Day2
上課投影片 Day3
%reset
來清除目前暫存區的變數print()
:將變數印出來s = input("提示字串")
:使用者輸入
s=0
for i in range(100):
s=s+i+1
print(s)
a = input("Please input an int:")
a = int(a)
for i in range(1, a+1):
if i%2 == 0 or i%3 == 0:
if i%6 == 0:
pass
else:
print(i)
a = input("Please input an int:")
a = int(a)
d=[]
for i in range(1, a+1):
if i%6 ==0:
continue
if i%2 == 0 or i%3 == 0:
d.append(i)
print(len(d))
err_num = 0.0001
a=-10
b=-9
while abs(a-b) > err_num:
fa = a**2-6*a+4
fb = b**2-6*b+4
x1 = a-(b-a)*fa/(fb-fa)
a, b = b, x1
print(b)
def g(year):
if year%4 == 0:
if year%100 == 0:
if year%400 == 0:
print('閏年')
else:
print('平年')
else:
print('閏年')
else:
print('平年')
years = input("Please input a year:")
years = int(years)
g(years)
def C(n, r):
if r == 0 or n == r:
ans = 1
else:
ans = C(n-1, r-1)+C(n-1, r)
return ans
s = C(4, 2)
print(s)
F=[]
F.append(1)
F.append(1)
n = input("Please input a number:")
n = int(n)
for i in range(2, n+1):
F.append(F[i-1]+F[i-2])
print(F[n])
a=96
b=36
F=[]
F.append(a)
F.append(b)
i = 2
while True:
t = F[i-2]%F[i-1]
F.append(t)
i = i+1
if t == 0:
break
print(F[-2])
L = [123, 456406 ,5, 6, 40, 0]
n=len(L)
for j in range(n):
for i in range(n-1):
if L[i] > L[i+1]:
L[i], L[i+1] = L[i+1], L[i]
版面編排
相關自學網站:莫煩python tkinter
參考範例
import tkinter as tk
win = tk.Tk()
win.title('tk test')
win.geometry('300x200')
win.resizable(1, 0)
t = 0
def on_click():
global t, L
t=t+1
L.config(text=str(t))
b = tk.Button(win, text='hit me', width=20, height=5, bg='green', command=on_click)
b.pack()
L = tk.Label(win, text='hello', width=20)
L.pack()
e = tk.Entry(win)
e.pack()
win.mainloop()
import tkinter as tk
import random
ans = random.randint(1, 100) #產生亂數
#視窗基本設定
win = tk.Tk()
win.title('tk test')
win.geometry('300x200')
win.resizable(1, 0)
t = 0
#判斷要大一點還是小一點
def on_click():
global t, L, ans, e, L2
guest_num = int(e.get())
if guest_num > ans:
L2.config(text='Smaller')
elif guest_num < ans:
L2.config(text='Bigger')
else:
L2.config(text='Get it!!')
b = tk.Button(win, text='Guest!', width=20, command=on_click)
L = tk.Label(win, text='Guest a number between 1 and 100', width=30)
e = tk.Entry(win)
L2 = tk.Label(win, text='', width=20)
L.pack()
b.pack()
e.pack()
L2.pack()
win.mainloop() #執行視窗
import tkinter as tk
import pylab as plt
import numpy as np
win = tk.Tk()
win.title('tk test')
win.geometry('300x600')
win.resizable(1, 1)
#初始化圖片
fig = plt.figure()
ax = fig.subplots()
a=0
b=0
x = list(np.arange(-10, 10, 0.1))
y=[]
for i in range(len(x)):
ans = 0
y.append(ans)
p, = ax.plot(x, y)
ax.set_ylim([-10, 10])
def draw_a(value):
global a, b, p
a = float(value) #更新全域變數數值
x = list(np.arange(-10, 10, 0.1))
y=[]
#用for迴圈描點
for i in range(len(x)):
ans = a*x[i]+b
y.append(ans)
p.set_data(x, y) #plot資料更新
fig.canvas.draw() #圖片更新
def draw_b(value):
global a, b, p
b = float(value) #更新全域變數數值
x = list(np.arange(-10, 10, 0.1))
y=[]
#用for迴圈描點
for i in range(len(x)):
ans = a*x[i]+b
y.append(ans)
p.set_data(x, y) #plot資料更新
fig.canvas.draw() #圖片更新
#建立scale
s1 = tk.Scale(win, from_=0, to=1, resolution=0.1, orient=tk.HORIZONTAL, length=350, showvalue=1, tickinterval=0.5, label = 'a', command=draw_a)
s2 = tk.Scale(win, from_=0, to=1, resolution=0.1, orient=tk.HORIZONTAL, length=350, showvalue=1, tickinterval=0.5, label = 'b', command=draw_b)
s3 = tk.Scale(win, from_=0, to=1, resolution=0.1, orient=tk.HORIZONTAL)
s1.pack()
s2.pack()
s3.pack()
win.mainloop()
import tkinter as tk
import pylab as plt
import numpy as np
win = tk.Tk()
win.title('tk test')
win.geometry('300x600')
win.resizable(1, 1)
fig = plt.figure()
ax = fig.subplots()
a=0
b=0
c=0
x = list(np.arange(-10, 10, 0.1))
y=[]
for i in range(len(x)):
ans = 0
y.append(ans)
p, = ax.plot(x, y)
ax.set_ylim([-10, 10])
def draw():
global a, b, c, p
x = np.linspace(-10, 10, 500)
#y = a*np.cos(x*b)+c
#y = x**a+b
y = 2**(-a*x)*np.cos(x*b)+c
p.set_data(x, y)
fig.canvas.draw()
def draw_a(value):
global a
a = float(value)
draw()
def draw_b(value):
global b
b = float(value)
draw()
def draw_c(value):
global c
c = float(value)
draw()
s1 = tk.Scale(win, from_=0, to=5, resolution=0.1, orient=tk.HORIZONTAL, length=350, showvalue=1, tickinterval=0.5, label = 'a', command=draw_a)
s2 = tk.Scale(win, from_=0, to=5, resolution=0.1, orient=tk.HORIZONTAL, length=350, showvalue=1, tickinterval=0.5, label = 'b', command=draw_b)
s3 = tk.Scale(win, from_=0, to=5, resolution=0.1, orient=tk.HORIZONTAL, length=350, showvalue=1, tickinterval=0.5, label = 'c', command=draw_c)
s1.pack()
s2.pack()
s3.pack()
win.mainloop()
from pylab import plt
fig = plt.figure() #產生圖表
ax= fig.subplots() #產生座標軸
'''
x0 = 1
y0 = 1
x1 = 2
y1 = 3
x2 = 3
y2 = 2
x= [x0, x1, x2]
y=[y0, y1, y2]
'''
#畫圖
p, = ax.plot([1, 2, 3], [1, 3, 2], 's--')
p1, = ax.plot([1, 2, 3], [10, 30, 20], 'b')
plt.show()
Alias | Color |
---|---|
‘b’ | blue |
‘g’ | green |
‘r’ | red |
‘c’ | cyan |
‘m’ | magenta |
‘y’ | yellow |
‘k’ | black |
‘w’ | white |
np.arange(a, b, c)
:跟range(a, b, c)
的功能完全一樣,而且arange可以放浮點數np.linspace(start, stop, number)
:產生number數量的線性array,start<=i<=stop
f = open('test.txt','r') #用r的方法開啟檔案
f = f.readlines() #讀取f檔中的內容,存到List中,以行為單位
len(<str>):取得字串長度
list(<str>):將字串轉型成list, 每個字元為一個list成員
<list> = <str>.split(<分割字串>):依照<分割字串>分割<str>,結果存到一個list中
<str>[index]:讀取字串的第index個字元
list( List )
\\ 反斜線
\' 單引號 '
\" 雙引號 "
\0 空字元
\n 換行
\r 歸位
\t Tab
\ooo 以8 進位數指定字元,最多三位數
\xhh 以16進 位數指定字元,至少兩位數
\uhhhh 以Unicode 16位元編碼指定字元
\Uhhhh 以Unicode 32位元編碼指定字元
string.split( '分隔符號' , 分隔次數 )
#分隔符,默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等。
#實例
str = "Line1-abcdef \nLine2-abc \nLine4-abcd"; #字串str
print str.split( ); #split by default
print str.split(' ', 1 ); #split by space空格 且只分隔一次
#輸出結果
#['Line1-abcdef', 'Line2-abc', 'Line4-abcd'] 第五行
#['Line1-abcdef', '\nLine2-abc \nLine4-abcd'] 第六行
#ref:http://www.runoob.com/python/att-string-split.html
import pylab as plt #記得import pylab
f = open('Y5.csv', 'r')
f = f.readlines()
time=[]
v1=[]
v2=[]
for i in range(2, len(f)):
g = f[i]
g = g.split(',')
time.append(float(g[0]))
v1.append(float(g[1]))
v2.append(float(g[2]))
fig = plt.figure() #剛這邊有筆誤,請複製的人更正一下
ax = fig.subplots()
p,=ax.plot(time,v)
p2, = ax.plot(time,v2)
import pylab as plt
f = open('Y5.csv', 'r')
f = f.readlines()
time=[]
v1=[]
v2=[]
for i in range(2, len(f)):
g = f[i]
g = g.split(',')
time.append(float(g[0]))
v1.append(float(g[1]))
v2.append(float(g[2]))
fig = plt.figure()
ax1 = fig.add_subplot(2, 1, 1)
ax2 = fig.add_subplot(2, 1, 2)
ax1.set_title('I-V curve')
ax2.set_title('V-t curve')
fig.suptitle('LED curve')
p, = ax1.plot(v1, v2, '.')
p1, = ax2.plot(time, v1)
p2, = ax2.plot(time, v2)
http://www.itread01.com/content/1503829342.html
乘乘樂:阿中是個整數狂,看到數字都情不自禁的想把每個位數都乘在一起。例如看到 356 就會想要知道 3 * 5 * 6 的值為何。快寫個程式幫幫為了乘數字而快發瘋的阿中吧!
最大公因數 G.C.D & L.C.M
身分證字號檢驗
態度的重要性
二元一次聯立方程式機算機
a1*x+b1*y=c1
a2*x+b2*y=c2
delta = a1*b2-a2*b1
dx = c1*b2-c2*b1
dy = a1*c2-a2*c1
x = dx/delta
y = dy/delta
如果detlta=0,則無解或無限多解
猜數字遊戲1A2B