Python基础综合练习
Pycharm开发环境设置与熟悉。
练习基本输入输出:
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 drawstar(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(-240, 90) drawstar(100) mygoto(-100, 120) turtle.left(50) drawstar(30) mygoto(-50, 85) turtle.left(-10) drawstar(30) mygoto(-50, 45) turtle.left(-40) drawstar(30) mygoto(-100,10) turtle.left(-20) drawstar(30) turtle.hideturtle() turtle.done()
字符串练习:
http://news.gzcc.cn/html/2017/xiaoyuanxinwen_1027/8443.html
取得校园新闻的编号
>>> url1 = 'http://news.gzcc.cn/html/2017/xiaoyuanxinwen_1027/8443.html' >>> url1[-14:-5]
https://docs.python.org/3/library/turtle.html
产生python文档的网址
>>> addr1 = 'https://docs.python.org/3/library/'
>>> addr2 = '.html'
>>> print(addr1 + 'turtle' + addr2)
http://news.gzcc.cn/html/xiaoyuanxinwen/4.html
产生校园新闻的一系列新闻页网址
>>> addr1 = 'http://news.gzcc.cn/html/xiaoyuanxinwen/' >>> addr2 = '.html' >>> for i in range(2,10): print(addr1 + str(i) + addr2)
练习字符串内建函数:strip,lstrip,rstrip,split,count,replace
用函数得到校园新闻编号
>>> url = 'http://news.gzcc.cn/html/2017/xiaoyuanxinwen_1027/8443.html' >>> print(url.rstrip('.html').split('_')[1])
用函数统计一歌词(文章、小说)中单词出现的次数,替换标点符号为空格,用空格进行分词。
>>> song = ''' Camila Cabello、Young Thug - Havana Pharrell Williams:Hey Camila Cabello & Pharrell Williams:Havana ooh nana ayy Half of my heart is in Havana oohnana ayy ayy He took me back to East Atlanta nanana All of my heart is in Havana ayy There's somethin' 'bout his manners uh huh Havana ooh nana Camila Cabello:He didn't walk up with that how you doin' When he came in the room He said there's a lot of girls I can do with But I can't without you I'm doin' forever in a minute That summer night in June And papa says he got malo in him He got me feelin' like Ooohooohooh I knew it when I met him I loved him when I left him Got me feelin' like Ooohooohooh and then I had to tell him I had to go oh nanananana Camila Cabello & Pharrell Williams:Havana ooh nana ayy Half of my heart is in Havana oohnana ayy ayy He took me back to East Atlanta nanana All of my heart is in Havana ayy My heart is in Havana Havana ooh nana Young Thug:Jeffery Just graduated fresh on campus mmm Fresh out East Atlanta with no manners damn Fresh out East Atlanta Bump on her bumper like a traffic jam jam Hey I was quick to pay that girl like Uncle Sam here you go ayy Back it on me shawty cravin' on me Get to diggin' on me on me She waited on me then what Shawty cakin' on me got the bacon on me wait up This is history in the makin' on me on me Point blank close range that be If it cost a million that's me that's me I was gettin' mula man they feel me Camila Cabello & Pharrell Williams:Havana ooh nana ayy ayy Half of my heart is in Havana oohnana oh ayy ayy He took me back to East Atlanta nanana oh no All of my heart is in Havana ayy My heart is in Havana ayy Havana ooh nana Starrah & Camila Cabello:Ooh nana oh nanana Take me back back back like Ooh nana oh nanana Take me back back back like Ooh nana oh nanana Take me back back back like Ooh nana oh nanana Take me back back back Yeah ayy Ooohooohooh Ooohooohooh Take me back to my Havana Camila Cabello & Pharrell Williams:Havana ooh nana ayy ayy Half of my heart is in Havana oohnana oh yeah He took me back to East Atlanta nanana All of my heart is in Havana My heart is in Havana ayy Havana ooh nana Camila Cabello & Starrah:Uh huh Oh nanana Oh nanana hey Oh nanana No no no take me back Oh nanana Havana ooh nana ''' >>> song.count('back') 18
>>> test=''' 人们都说:“桂林山水甲天下。”我们乘着木船,荡漾在漓江上,来观赏桂林的山水。 我看见过波澜壮阔的大海,玩赏过水平如镜的西湖,却从没看见过漓江这样的水。漓江的水真静啊,静得让你感觉不到它在流动;漓江的水真清啊,清得可以看见江底的沙石;漓江的水真绿啊,绿得仿佛那是一块无瑕的翡翠。船桨激起的微波扩散出一道道水纹,才让你感觉到船在前进,岸在后移。 我攀登过峰峦雄伟的泰山,游览过红叶似火的香山,却从没看见过桂林这一带的山,桂林的山真奇啊,一座座拔地而起,各不相连,像老人,像巨象,像骆驼,奇峰罗列,形态万千;桂林的山真秀啊,像翠绿的屏障,像新生的竹笋,色彩明丽,倒映水中;桂林的山真险啊,危峰兀立,怪石嶙峋,好像一不小心就会栽倒下来。 这样的山围绕着这样的水,这样的水倒映着这样的山,再加上空中云雾迷蒙,山间绿树红花,江上竹筏小舟,让你感到像是走进了连绵不断的画卷,真是“舟行碧波上,人在画中游”。''' >>> test.replace(',',' ').replace('。',' ').replace('!',' ') '\n人们都说:“桂林山水甲天下 ”我们乘着木船 荡漾在漓江上 来观赏桂林的山水 \n\u3000\u3000我看见过波澜壮阔的大海 玩赏过水平如镜的西湖 却从没看见过漓江这样的水 漓江的水真静啊 静得让你感觉不到它在流动;漓江的水真清啊 清得可以看见江底的沙石;漓江的水真绿啊 绿得仿佛那是一块无瑕的翡翠 船桨激起的微波扩散出一道道水纹 才让你感觉到船在前进 岸在后移 \n\u3000\u3000我攀登过峰峦雄伟的泰山 游览过红叶似火的香山 却从没看见过桂林这一带的山 桂林的山真奇啊 一座座拔地而起 各不相连 像老人 像巨象 像骆驼 奇峰罗列 形态万千;桂林的山真秀啊 像翠绿的屏障 像新生的竹笋 色彩明丽 倒映水中;桂林的山真险啊 危峰兀立 怪石嶙峋 好像一不小心就会栽倒下来 \n\u3000\u3000这样的山围绕着这样的水 这样的水倒映着这样的山 再加上空中云雾迷蒙 山间绿树红花 江上竹筏小舟 让你感到像是走进了连绵不断的画卷 真是“舟行碧波上 人在画中游” ' >>>
>>> test1 = test.split('str = ' '') >>> print(test1) ['\n人们都说:“桂林山水甲天下。”我们乘着木船,荡漾在漓江上,来观赏桂林的山水。\n\u3000\u3000我看见过波澜壮阔的大海,玩赏过水平如镜的西湖,却从没看见过漓江这样的水。漓江的水真静啊,静得让你感觉不到它在流动;漓江的水真清啊,清得可以看见江底的沙石;漓江的水真绿啊,绿得仿佛那是一块无瑕的翡翠。船桨激起的微波扩散出一道道水纹,才让你感觉到船在前进,岸在后移。\n\u3000\u3000我攀登过峰峦雄伟的泰山,游览过红叶似火的香山,却从没看见过桂林这一带的山,桂林的山真奇啊,一座座拔地而起,各不相连,像老人,像巨象,像骆驼,奇峰罗列,形态万千;桂林的山真秀啊,像翠绿的屏障,像新生的竹笋,色彩明丽,倒映水中;桂林的山真险啊,危峰兀立,怪石嶙峋,好像一不小心就会栽倒下来。\n\u3000\u3000这样的山围绕着这样的水,这样的水倒映着这样的山,再加上空中云雾迷蒙,山间绿树红花,江上竹筏小舟,让你感到像是走进了连绵不断的画卷,真是“舟行碧波上,人在画中游”。']