100 個 Python 小例子(練習(xí)題三)
前期練習(xí):
實例051:按位與
題目:學(xué)習(xí)使用按位與 & 。
程序分析:0&0=0; 0&1=0; 1&0=0; 1&1=1。
a=0o77 print(a) b=a&3 print(b) b=b&7 print(b)
實例052:按位或
題目:學(xué)習(xí)使用按位或 | 。
程序分析:0|0=0; 0|1=1; 1|0=1; 1|1=1
a=0o77 print(a|3) print(a|3|7)
實例053:按位異或
題目:學(xué)習(xí)使用按位異或 ^ 。
程序分析:0^0=0; 0^1=1; 1^0=1; 1^1=0
a=0o77 print(a^3) print(a^3^7)
實例054:位取反、位移動
題目:取一個整數(shù)a從右端開始的4?7位。
程序分析:可以這樣考慮: (1)先使a右移4位。 (2)設(shè)置一個低4位全為1,其余全為0的數(shù)??捎脋(~0<<4) (3)將上面二者進行&運算。
a=int(input('輸入一個數(shù)字: '))
b=0 ? ? ? ? ? ? ? ? # ? ? 0
b=~b ? ? ? ? ? ? ? ?# ? ? 1
b=b<<4 ? ? ? ? ? ? ?# 10000
b=~b ? ? ? ? ? ? ? ?# ?1111
c=a>>4
d=c&b
print('a:',bin(a))
print('b:',bin(b))
print('c:',bin(c))
print('d:',bin(d))實例055:按位取反
題目:學(xué)習(xí)使用按位取反~。
程序分析:~0=1; ~1=0;
print(~234) print(~~234)
實例056:畫圈
題目:畫圖,學(xué)用circle畫圓形?! ?/p>
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(26): ? ? canvas.create_oval(310-k,250-k,310+k,250+k,width=1) ? ? k+=j ? ? j+=0.3 mainloop()
實例057:畫線
題目:畫圖,學(xué)用line畫直線。
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()
實例058:畫矩形
題目:畫圖,學(xué)用rectangle畫方形。
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()實例059:畫圖(丑)
題目:畫圖,綜合例子?! ?/p>
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()
實例060:字符串長度
題目:計算字符串長度?! ?/p>
s='zhangguang101' print(len(s))
例061:楊輝三角
題目:打印出楊輝三角形前十行?! ?/p>
def generate(numRows): ? ? r = [[1]] ? ? for i in range(1,numRows): ? ? ? ? r.append(list(map(lambda x,y:x+y, [0]+r[-1],r[-1]+[0]))) ? ? return r[:numRows] a=generate(10) for i in a: ? ? print(i)
實例062:查找字符串
題目:查找字符串?! ?/p>
s1='aabbxuebixuebi' s2='ab' s3='xue' print(s1.find(s2)) print(s1.find(s3))
實例063:畫橢圓
題目:畫橢圓?!?/p>
程序分析:使用 tkinter。
if __name__ == '__main__': ? ? from tkinter import * ? ? x = 360 ? ? y = 160 ? ? top = y - 30 ? ? bottom = y - 30 ? ? canvas = Canvas(width = 400,height = 600,bg = 'white') ? ? for i in range(20): ? ? ? ? canvas.create_oval(250 - top,250 - bottom,250 + top,250 + bottom) ? ? ? ? top -= 5 ? ? ? ? bottom += 5 ? ? canvas.pack() ? ? mainloop()
實例64:畫橢圓、矩形
題目:利用ellipse 和 rectangle 畫圖。。
if __name__ == '__main__': ? ? from tkinter import * ? ? canvas = Canvas(width = 400,height = 600,bg = 'white') ? ? left = 20 ? ? right = 50 ? ? top = 50 ? ? num = 15 ? ? for i in range(num): ? ? ? ? canvas.create_oval(250 - right,250 - left,250 + right,250 + left) ? ? ? ? canvas.create_oval(250 - 20,250 - top,250 + 20,250 + top) ? ? ? ? canvas.create_rectangle(20 - 2 * i,20 - 2 * i,10 * (i + 2),10 * ( i + 2)) ? ? ? ? right += 5 ? ? ? ? left += 5 ? ? ? ? top += 10 ? ? canvas.pack() ? ? mainloop()
實例065:畫組合圖形
題目:一個最優(yōu)美的圖案?! ?/p>
import math from tkinter import * class PTS: ? ? def __init__(self): ? ? ? ? self.x = 0 ? ? ? ? self.y = 0 points = [] def LineToDemo(): ? ? screenx = 400 ? ? screeny = 400 ? ? canvas = Canvas(width = screenx,height = screeny,bg = 'white') ? ? AspectRatio = 0.85 ? ? MAXPTS = 15 ? ? h = screeny ? ? w = screenx ? ? xcenter = w / 2 ? ? ycenter = h / 2 ? ? radius = (h - 30) / (AspectRatio * 2) - 20 ? ? step = 360 / MAXPTS ? ? angle = 0.0 ? ? for i in range(MAXPTS): ? ? ? ? rads = angle * math.pi / 180.0 ? ? ? ? p = PTS() ? ? ? ? p.x = xcenter + int(math.cos(rads) * radius) ? ? ? ? p.y = ycenter - int(math.sin(rads) * radius * AspectRatio) ? ? ? ? angle += step ? ? ? ? points.append(p) ? ? canvas.create_oval(xcenter - radius,ycenter - radius, ? ? ? ? ? ? ? ? ? ? ? ?xcenter + radius,ycenter + radius) ? ? for i in range(MAXPTS): ? ? ? ? for j in range(i,MAXPTS): ? ? ? ? ? ? canvas.create_line(points[i].x,points[i].y,points[j].x,points[j].y) ? ? canvas.pack() ? ? mainloop() if __name__ == '__main__': ? ? LineToDemo()
實例066:三數(shù)排序
題目:輸入3個數(shù)a,b,c,按大小順序輸出。
程序分析:同實例005。
raw=[]
for i in range(3):
? ? x=int(input('int%d: '%(i)))
? ? raw.append(x)
for i in range(len(raw)):
? ? for j in range(i,len(raw)):
? ? ? ? if raw[i]>raw[j]:
? ? ? ? ? ? raw[i],raw[j]=raw[j],raw[i]
print(raw)
raw2=[]
for i in range(3):
? ? x=int(input('int%d: '%(i)))
? ? raw2.append(x)
print(sorted(raw2))實例067:交換位置
題目:輸入數(shù)組,最大的與第一個元素交換,最小的與最后一個元素交換,輸出數(shù)組。
li=[3,2,5,7,8,1,5] li[-1],li[li.index(min(li))]=li[li.index(min(li))],li[-1] m=li[0] ind=li.index(max(li)) li[0]=li[ind] li[ind]=m print(li)
實例068:旋轉(zhuǎn)數(shù)列
題目:有n個整數(shù),使其前面各數(shù)順序向后移m個位置,最后m個數(shù)變成最前面的m個數(shù)
from collections import *
li=[1,2,3,4,5,6,7,8,9]
deq=deque(li,maxlen=len(li))
print(li)
deq.rotate(int(input('rotate:')))
print(list(deq))實例069:報數(shù)
題目:有n個人圍成一圈,順序排號。從第一個人開始報數(shù)(從1到3報數(shù)),凡報到3的人退出圈子,問最后留下的是原來第幾號的那位。
if __name__ == '__main__':
? ? nmax = 50
? ? n = int(input('請輸入總?cè)藬?shù):'))
? ? num = []
? ? for i in range(n):
? ? ? ? num.append(i + 1)
? ? i = 0
? ? k = 0
? ? m = 0
? ? while m < n - 1:
? ? ? ? if num[i] != 0 : k += 1
? ? ? ? if k == 3:
? ? ? ? ? ? num[i] = 0
? ? ? ? ? ? k = 0
? ? ? ? ? ? m += 1
? ? ? ? i += 1
? ? ? ? if i == n : i = 0
? ? i = 0
? ? while num[i] == 0: i += 1
? ? print(num[i])實例070:字符串長度II
題目:寫一個函數(shù),求一個字符串的長度,在main函數(shù)中輸入字符串,并輸出其長度。
def lenofstr(s):
? ? return len(s)
print(lenofstr('tanxiaofengsheng'))實例071:輸入和輸出
題目:編寫input()和output()函數(shù)輸入,輸出5個學(xué)生的數(shù)據(jù)記錄。
N = 3
#stu
# num : string
# name : string
# score[4]: list
student = []
for i in range(5):
? ? student.append(['','',[]])
def input_stu(stu):
? ? for i in range(N):
? ? ? ? stu[i][0] = input('input student num:\n')
? ? ? ? stu[i][1] = input('input student name:\n')
? ? ? ? for j in range(3):
? ? ? ? ? ? stu[i][2].append(int(input('score:\n')))
def output_stu(stu):
? ? for i in range(N):
? ? ? ? print ('%-6s%-10s' % ( stu[i][0],stu[i][1] ))
? ? ? ? for j in range(3):
? ? ? ? ? ? print ('%-8d' % stu[i][2][j])
if __name__ == '__main__':
? ? input_stu(student)
? ? print (student)
? ? output_stu(student)實例072:創(chuàng)建鏈表
題目:創(chuàng)建一個鏈表。
class Node:
? ? def __init__(self, data):
? ? ? ? self.data = data
? ? ? ? self.next = None
? ? def get_data(self):
? ? ? ? return self.data
class List:
? ? def __init__(self, head):
? ? ? ? self.head = head
? ? def is_empty(self):?
? ? ? ? return self.get_len() == 0
? ? def get_len(self): ?
? ? ? ? length = 0
? ? ? ? temp = self.head
? ? ? ? while temp is not None:
? ? ? ? ? ? length += 1
? ? ? ? ? ? temp = temp.next
? ? ? ? return length
? ? def append(self, node):
? ? ? ? temp = self.head
? ? ? ? while temp.next is not None:
? ? ? ? ? ? temp = temp.next
? ? ? ? temp.next = node
? ? def delete(self, index):?
? ? ? ? if index < 1 or index > self.get_len():
? ? ? ? ? ? print("給定位置不合理")
? ? ? ? ? ? return
? ? ? ? if index == 1:
? ? ? ? ? ? self.head = self.head.next
? ? ? ? ? ? return
? ? ? ? temp = self.head
? ? ? ? cur_pos = 0
? ? ? ? while temp is not None:
? ? ? ? ? ? cur_pos += 1
? ? ? ? ? ? if cur_pos == index-1:
? ? ? ? ? ? ? ? temp.next = temp.next.next
? ? ? ? ? ? temp = temp.next
? ? def insert(self, pos, node):
? ? ? ? if pos < 1 or pos > self.get_len():
? ? ? ? ? ? print("插入結(jié)點位置不合理")
? ? ? ? ? ? return
? ? ? ? temp = self.head
? ? ? ? cur_pos = 0
? ? ? ? while temp is not Node:
? ? ? ? ? ? cur_pos += 1
? ? ? ? ? ? if cur_pos == pos-1:
? ? ? ? ? ? ? ? node.next = temp.next
? ? ? ? ? ? ? ? temp.next =node
? ? ? ? ? ? ? ? break
? ? ? ? ? ? temp = temp.next
? ? def reverse(self, head):
? ? ? ? if head is None and head.next is None:
? ? ? ? ? ? return head
? ? ? ? pre = head
? ? ? ? cur = head.next
? ? ? ? while cur is not None:
? ? ? ? ? ? temp = cur.next
? ? ? ? ? ? cur.next = pre
? ? ? ? ? ? pre = cur
? ? ? ? ? ? cur = temp
? ? ? ? head.next = None
? ? ? ? return pre
? ? def print_list(self, head):
? ? ? ? init_data = []
? ? ? ? while head is not None:
? ? ? ? ? ? init_data.append(head.get_data())
? ? ? ? ? ? head = head.next
? ? ? ? return init_data
if __name__=='__main__':
? ? head=Node('head')
? ? link=List(head)
? ? for i in range(10):
? ? ? ? node=Node(i)
? ? ? ? link.append(node)
? ? print(link.print_list(head))實例073:反向輸出鏈表
題目:反向輸出一個鏈表。
class Node:
? ? def __init__(self, data):
? ? ? ? self.data = data
? ? ? ? self.next = None
? ? def get_data(self):
? ? ? ? return self.data
class List:
? ? def __init__(self, head):
? ? ? ? self.head = head
? ? def is_empty(self):?
? ? ? ? return self.get_len() == 0
? ? def get_len(self): ?
? ? ? ? length = 0
? ? ? ? temp = self.head
? ? ? ? while temp is not None:
? ? ? ? ? ? length += 1
? ? ? ? ? ? temp = temp.next
? ? ? ? return length
? ? def append(self, node):
? ? ? ? temp = self.head
? ? ? ? while temp.next is not None:
? ? ? ? ? ? temp = temp.next
? ? ? ? temp.next = node
? ? def delete(self, index):?
? ? ? ? if index < 1 or index > self.get_len():
? ? ? ? ? ? print("給定位置不合理")
? ? ? ? ? ? return
? ? ? ? if index == 1:
? ? ? ? ? ? self.head = self.head.next
? ? ? ? ? ? return
? ? ? ? temp = self.head
? ? ? ? cur_pos = 0
? ? ? ? while temp is not None:
? ? ? ? ? ? cur_pos += 1
? ? ? ? ? ? if cur_pos == index-1:
? ? ? ? ? ? ? ? temp.next = temp.next.next
? ? ? ? ? ? temp = temp.next
? ? def insert(self, pos, node):
? ? ? ? if pos < 1 or pos > self.get_len():
? ? ? ? ? ? print("插入結(jié)點位置不合理")
? ? ? ? ? ? return
? ? ? ? temp = self.head
? ? ? ? cur_pos = 0
? ? ? ? while temp is not Node:
? ? ? ? ? ? cur_pos += 1
? ? ? ? ? ? if cur_pos == pos-1:
? ? ? ? ? ? ? ? node.next = temp.next
? ? ? ? ? ? ? ? temp.next =node
? ? ? ? ? ? ? ? break
? ? ? ? ? ? temp = temp.next
? ? def reverse(self, head):
? ? ? ? if head is None and head.next is None:
? ? ? ? ? ? return head
? ? ? ? pre = head
? ? ? ? cur = head.next
? ? ? ? while cur is not None:
? ? ? ? ? ? temp = cur.next
? ? ? ? ? ? cur.next = pre
? ? ? ? ? ? pre = cur
? ? ? ? ? ? cur = temp
? ? ? ? head.next = None
? ? ? ? return pre
? ? def print_list(self, head):
? ? ? ? init_data = []
? ? ? ? while head is not None:
? ? ? ? ? ? init_data.append(head.get_data())
? ? ? ? ? ? head = head.next
? ? ? ? return init_data
if __name__=='__main__':
? ? head=Node('head')
? ? link=List(head)
? ? for i in range(10):
? ? ? ? node=Node(i)
? ? ? ? link.append(node)
? ? print(link.print_list(head))
? ? print(link.print_list(link.reverse(head)))實例074:列表排序、連接
題目:列表排序及連接。
程序分析:排序可使用sort() 方法,連接可以使用 + 號或 extend() 方法。
a=[2,6,8] b=[7,0,4] a.extend(b) a.sort() print(a)
例075:不知所云
題目:放松一下,算一道簡單的題目。
if __name__ == '__main__': ? ? for i in range(5): ? ? ? ? n = 0 ? ? ? ? if i != 1: n += 1 ? ? ? ? if i == 3: n += 1 ? ? ? ? if i == 4: n += 1 ? ? ? ? if i != 4: n += 1 ? ? ? ? if n == 3: print (64 + i)
到此這篇關(guān)于100 個 Python 小例子(練習(xí)題三)的文章就介紹到這了,更多相關(guān) Python 練習(xí)題內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python趣味挑戰(zhàn)之用pygame實現(xiàn)簡單的金幣旋轉(zhuǎn)效果
今天教大家怎么用pygame實現(xiàn)簡單的金幣旋轉(zhuǎn)效果,文中有非常詳細的代碼示例,對正在學(xué)習(xí)python的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05
Python常見錯誤:IndexError:?list?index?out?of?range解決
最近在寫一個爬蟲程序,但是卻出現(xiàn)了錯誤提示IndexError:?list?index?out?of?range,所以下面這篇文章主要給大家介紹了關(guān)于Python常見錯誤:IndexError:?list?index?out?of?range的解決方法,需要的朋友可以參考下2023-01-01
python+jinja2實現(xiàn)接口數(shù)據(jù)批量生成工具
這篇文章主要介紹了python+jinja2實現(xiàn)接口數(shù)據(jù)批量生成工具的操作方法,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-08-08
ipython jupyter notebook中顯示圖像和數(shù)學(xué)公式實例
這篇文章主要介紹了ipython jupyter notebook中顯示圖像和數(shù)學(xué)公式實例,具有很好的參考價值,希望對有所幫助。一起跟隨小編過來看看吧2020-04-04

