Python基础综合练习
练习基本输入输出:
print('你好,{}.'.format(name))
print(sys.argv)
库的使用方法:
import ...
from ... import ...
条件语句:
if (abs(pos()))<1:
break
循环语句:
for i in range(5):
while True:
函数定义:
def mygoto(x,y):
def drawjx(r):
综合练习:画一面五星红旗,将代码与运行截图发布博客交作业。
import turtle # 画线 def mygoto(x, y): turtle.up() turtle.goto(x, y) turtle.down() # 五角星形 def drawjx(x): turtle.begin_fill() for i in range(5): turtle.forward(x) turtle.right(144) turtle.end_fill() turtle.setup (600, 400, 0, 0) turtle.color("yellow") turtle.bgcolor("red") turtle.fillcolor("yellow") mygoto(-250, 95) drawjx(100) for i in range(4): x = 1 if i in [0, 3]: x = 0 mygoto(-120 + x * 30, 150 - i * 40) turtle.left(15 - i * 15) drawjx(20) turtle.done()
字符串练习:
http://news.gzcc.cn/html/2017/xiaoyuanxinwen_1027/8443.html
取得校园新闻的编号
asd = "http://news.gzcc.cn/html/2017/xiaoyuanxinwen_1027/8443.html" num = asd.rstrip(".html").split("_")[1] print(num)
https://docs.python.org/3/library/turtle.html
产生python文档的网址
addr1 = "https://docs.python.org/3/library/" addr2 = ".html" print(addr1+"index"+addr2)
http://news.gzcc.cn/html/xiaoyuanxinwen/4.html
产生校园新闻的一系列新闻页网址
方法1
addr3 = "http://news.gzcc.cn/html/xiaoyuanxinwen/" addr4 = ".html" for i in range(2, 21): print(addr3+str(i)+addr4)
方法2
addr5 = "http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html" for i in range(2, 30): addr6 = addr5.format(i) print(addr6)
练习字符串内建函数:strip,lstrip,rstrip,split,count,replace
用函数得到校园新闻编号
addr5 = "http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html" for i in range(2, 30): addr6 = addr5.format(i) addr7 = addr6.rstrip(".html").split("/")[-1] print(addr7)
用函数统计一歌词(文章、小说)中单词出现的次数,替换标点符号为空格,用空格进行分词。
zxc = '''In my dual profession as an educator and health care provider, I have worked with numerous children infected with the virus that causes AIDS. The relationships that I have had with these special kids have been gifts in my life. They have taught me so many things, but I have especially learned that great courage can be found in the smallest of packages. Let me tell you about Tyler. Tyler was born infected with HIV: his mother was also infected. From the very beginning of his life, he was dependent on medications to enable him to survive. When he was five, he had a tube surgically inserted in a vein in his chest. This tube was connected to a pump, which he carried in a small backpack on his back. Medications were hooked up to this pump and were continuously supplied through this tube to his bloodstream. At times, he also needed supplemented oxygen to support his breathing. Tyler wasn’t willing to give up one single moment of his childhood to this deadly disease. It was not unusual to find him playing and racing around his backyard, wearing his medicine-laden backpack and dragging his tank of oxygen behind him in his little wagon. All of us who knew Tyler marveled at his pure joy in being alive and the energy it gave him. Tyler’s mom often teased him by telling him that he moved so fast she needed to dress him in red. That way, when she peered through the window to check on him playing in the yard, she could quickly spot him. This dreaded disease eventually wore down even the likes of a little dynamo like Tyler. He grew quite ill and, unfortunately, so did his HIV-infected mother. When it became apparent that he wasn’t going to survive, Tyler’s mom talked to him about death. She comforted him by telling Tyler that she was dying too, and that she would be with him soon in heaven. A few days before his death, Tyler beckoned me over to his hospital bed and whispered, “I might die soon. I’m not scared. When I die, please dress me in red. Mom promised she’s coming to heaven, too. I’ll be playing when she gets there, and I want to make sure she can find me.”''' zxc.count('few') zxc.count('days') zxc.count('before') zxc.count('his') zxc.count('death') zxc.count('Tyler') zxc.count('beckoned') zxc.count('me') zxc.count('over') zxc.count('to') zxc.count('hospital') fgh = zxc.replace(',',' ').replace('.',' ').replace('?',' ').replace('!',' ').replace(':',' ').replace(';',' ').replace('\'',' ').replace('\"',' ') print(fgh) dfg = fgh.split() print(dfg)