Bootstrap

python六大原则生活中例子_Python练习实例100例(从简入难)56-60

相信有很多人都在努力地自学着Python,但自学过程中总是看懂了却不会做。这就像学生时期老师讲过了,觉得听懂了,但一看题就懵了。

在自学任何一门软件或是编程语言的过程中,最重要的是结合教程去进行实践训练,毕竟“实践才是检验真理的唯一标准”。

今天给大家带来Python从初级入门到精进的练习实例共100例,每篇五道题保持篇幅不会过长,大家练习也循序渐进、松弛有度。并且还会附上题目程序的的源代码,供大家参考,大家初期可以“照猫画虎”去发现其编程的目的及规律,从而更好地理解。

Python 练习实例56

题目:画图,学用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()

Python 练习实例57

题目:画图,学用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()

Python 练习实例58

题目:画图,学用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()

Python 练习实例59

题目:画图,综合例子。

程序分析:利用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()

Python 练习实例60

题目:计算字符串长度。

程序分析:无。

程序源代码:

#!/usr/bin/python

# -*- coding: UTF-8 -*-

sStr1 = 'strlen'

print len(sStr1)

祝大家Python学习顺利!没有 B U G~

收藏

;