相信有很多人都在努力地自學着Python,但自學過程中總是看懂了卻不會做。這就像學生時期老師講過了,覺得聽懂了,但一看題就懵了。
在自學任何一門軟件或是編程語言的過程中,最重要的是結合教程去進行實踐訓練,畢竟“實踐才是檢驗真理的唯一标準”。
今天給大家帶來Python從初級入門到精進的練習實例共100例,每篇五道題保持篇幅不會過長,大家練習也循序漸進、松弛有度。并且還會附上題目程序的的源代碼,供大家參考,大家初期可以“照貓畫虎”去發現其編程的目的及規律,從而更好地理解。
題目:畫圖,學用circle畫圓形。
程序分析:無。
程序源代碼:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
if __name__ == '__main__':
from Tkinter import *
canvas = Canvas(width=800, height=600, bg='yellow')
canvas.pack(expand=YES, fill=BOTH)
k = 1
j = 1
for i in range(0,26):
canvas.create_oval(310 - k,250 - k,310 k,250 k, width=1)
k = j
j = 0.3
mainloop()
題目:畫圖,學用line畫直線。
程序分析:無。
程序源代碼:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
if __name__ == '__main__':
from Tkinter import *
canvas = Canvas(width=300, height=300, bg='green')
canvas.pack(expand=YES, fill=BOTH)
x0 = 263
y0 = 263
y1 = 275
x1 = 275
for i in range(19):
canvas.create_line(x0,y0,x0,y1, width=1, fill='red')
x0 = x0 - 5
y0 = y0 - 5
x1 = x1 5
y1 = y1 5
x0 = 263
y1 = 275
y0 = 263
for i in range(21):
canvas.create_line(x0,y0,x0,y1,fill = 'red')
x0 = 5
y0 = 5
y1 = 5
mainloop()
題目:畫圖,學用rectangle畫方形。
程序分析:參數說明:(left ,top )為矩形的左上坐标,(right,bottom)為矩形的右下坐标,兩者可确定一個矩形的大小
程序源代碼:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
if __name__ == '__main__':
from Tkinter import *
root = Tk()
root.title('Canvas')
canvas = Canvas(root,width = 400,height = 400,bg = 'yellow')
x0 = 263
y0 = 263
y1 = 275
x1 = 275
for i in range(19):
canvas.create_rectangle(x0,y0,x1,y1)
x0 -= 5
y0 -= 5
x1 = 5
y1 = 5
canvas.pack()
root.mainloop()
題目:畫圖,綜合例子。
程序分析:利用for循環控制100-999個數,每個數分解出個位,十位,百位。。
程序源代碼:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
if __name__ == '__main__':
from Tkinter import *
canvas = Canvas(width = 300,height = 300,bg = 'green')
canvas.pack(expand = YES,fill = BOTH)
x0 = 150
y0 = 100
canvas.create_oval(x0 - 10,y0 - 10,x0 10,y0 10)
canvas.create_oval(x0 - 20,y0 - 20,x0 20,y0 20)
canvas.create_oval(x0 - 50,y0 - 50,x0 50,y0 50)
import math
B = 0.809
for i in range(16):
a = 2 * math.pi / 16 * i
x = math.ceil(x0 48 * math.cos(a))
y = math.ceil(y0 48 * math.sin(a) * B)
canvas.create_line(x0,y0,x,y,fill = 'red')
canvas.create_oval(x0 - 60,y0 - 60,x0 60,y0 60)
for k in range(501):
for i in range(17):
a = (2 * math.pi / 16) * i (2 * math.pi / 180) * k
x = math.ceil(x0 48 * math.cos(a))
y = math.ceil(y0 48 math.sin(a) * B)
canvas.create_line(x0,y0,x,y,fill = 'red')
for j in range(51):
a = (2 * math.pi / 16) * i (2* math.pi / 180) * k - 1
x = math.ceil(x0 48 * math.cos(a))
y = math.ceil(y0 48 * math.sin(a) * B)
canvas.create_line(x0,y0,x,y,fill = 'red')
mainloop()
題目:計算字符串長度。
程序分析:無。
程序源代碼:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
sStr1 = 'strlen'
print len(sStr1)
祝大家Python學習順利!沒有 B U G~
,更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!