當前位置:
首頁 >
Python基础综合练习
發布時間:2025/4/14
32
豆豆
生活随笔
收集整理的這篇文章主要介紹了
Python基础综合练习
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
# coding=utf-8
import turtle# 畫五角星的方法
def drawPentagram(x):turtle.begin_fill()turtle.color('yellow')for i in range(5):turtle.forward(x)turtle.right(144)turtle.end_fill()def gotoPoint(x,y,z):turtle.penup()turtle.setheading(0)turtle.goto(x,y)turtle.right(z)turtle.pendown()#length = 540
length = int(input('請輸入國旗長度:'))
width=length/3*2
bigDiameter=width*0.3
smallDiameter=width*0.1
turtle.hideturtle()turtle.penup()
turtle.goto(-length/2,width/2)
turtle.pendown()
turtle.color('red')
turtle.begin_fill()
for i in range(2):turtle.forward(length)turtle.right(90)turtle.forward(width)turtle.right(90)
turtle.end_fill()# 大的五角星
gotoPoint(-width*0.65, width*0.3, 0)
drawPentagram(bigDiameter)# 第一個小五角星
gotoPoint(-width*0.3, width*9/20, 12)
drawPentagram(smallDiameter)# 第二個小五角星
gotoPoint(-width*0.18,width*0.32, -18)
drawPentagram(smallDiameter)# 第三個小五角星
gotoPoint(-width*0.18,width*0.19, 0)
drawPentagram(smallDiameter)# 第四個小五角星
gotoPoint(-width*0.3, width*0.1, 12)
drawPentagram(smallDiameter)turtle.done()
運行結果圖:
?
字符串練習:
取得校園新聞的編號
url = 'http://news.gzcc.cn/html/2017/xiaoyuanxinwen_1027/8443.html' print(url[-14:-5])運行結果圖
?
產生python文檔的網址
def generatePythonUrl(className):addr1 = 'https://docs.python.org/3/library/'addr2 = '.html'return addr1 + className + addr2 print(generatePythonUrl('turtle'))運行結果圖
?
產生校園新聞的一系列新聞頁網址
for i in range(0,20):print('http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'.format(i))運行結果圖
?
練習字符串內建函數:strip,lstrip,rstrip,split,count,replace
用函數得到校園新聞編號
url = 'http://news.gzcc.cn/html/2017/xiaoyuanxinwen_1027/8443.html' print(url.rstrip('.html').split('_')[1])運行結果
?
?
用函數統計一歌詞(文章、小說)中單詞出現的次數,替換標點符號為空格,用空格進行分詞。
lyric = ''' Waking up I see that everything is ok The first time in my life and now it's so great Slowing down I look around and I am so amazed I think about the little things that make life great I wouldn't change a thing about it This is the best feeling This innocence is brilliant, I hope that it will stay This moment is perfect, please don't go away, I need you now And I'll hold on to it, don't you let it pass you by I found a place so safe, not a single tear The first time in my life and now it's so clear Feel calm I belong, I'm so happy here It's so strong and now I let myself be sincere I wouldn't change a thing about it This is the best feeling This innocence is brilliant, I hope that it will stay This moment is perfect, please don't go away, I need you now And I'll hold on to it, don't you let it pass you by It's the state of bliss you think you're dreaming It's the happiness inside that you're feeling It's so beautiful it makes you wanna cry It's the state of bliss you think you're dreaming It's the happiness inside that you're feeling It's so beautiful it makes you wanna cry It's so beautiful it makes you want to cry This innocence is brilliant, it makes you want to cry This innocence is brilliance, please don't go away Cause I need you now And I'll hold on to it, don't you let it pass you by This innocence is brilliant, I hope that it will stay This moment is perfect, please don't go away, I need you now And I'll hold on to it, don't you let it pass you by ''' result = lyric.replace(',', ' ').lower().lstrip().rstrip() tempwords = result.split() words = list(set(tempwords)) print(words) print(result) for i in range(0,len(words)):print('單詞 '+ words[i] + ' 的出現次數為:'+str(result.count(str(words[i]))))運行結果圖
?
轉載于:https://www.cnblogs.com/zzrf/p/8609863.html
總結
以上是生活随笔為你收集整理的Python基础综合练习的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Replicate(网络复制),Acto
- 下一篇: shell基础:多命令顺序执行与管道符