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 drawwjx(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, 75) drawwjx(100) for i in range(4): x = 1 if i in [0, 3]: x = 0 mygoto(-100 + x * 50, 150 - i * 40) turtle.left(15 - i * 15) drawwjx(30) mygoto(0, 0) turtle.hideturtle() turtle.done()
http://news.gzcc.cn/html/2017/xiaoyuanxinwe 
n_1027/8443.html

取得校园新闻的编号

str1='http://news.gzcc.cn/html/2017/xiaoyuanxinwen_1027/8443.html'
print(str1[-9:-5])
结果:



https://docs.python.org/3/library/turtle.ht 
ml

产生python文档的网址

str2='https://docs.python.org/3/library/turtle.html'
print(str2.replace("turtle","python"))
结果:



http://news.gzcc.cn/html/xiaoyuanxinwen/4.h 
tml

产生校园新闻的一系列新闻页网址

add3='http://news.gzcc.cn/html/xiaoyuanxinwen/'
add4='.html'
for i  in range(4):
    result=add3+str(i)+add4
    print(result)


练习字符串内建函数: 
strip,lstrip,rstrip,split,count

用函数得到校园新闻编号

str5='http://news.gzcc.cn/html/2017/xiaoyuanxinwen_1027/8443.html'
print(str5.split('_',2)[1].rstrip('.html'))

  

用函数统计一歌词中单词出现的次数
>>> song = '''
There For You - Martin Garrix/Troye Sivan
Written by:Bea Miller/Brady Riley/Brett McLaughlin/Martin Garrix/Troye Sivan
I woke up pissed off today
And lately everyone feels fake
Somewhere I lost a piece of me
Smoking cigarettes on balconies
But I can't do this alone
Sometimes I just need a light
If I call you on the phone
Need you on the other side
So when your tears roll down your pillow like a river
I'll be there for you
I'll be there for you
When you're screaming but they only hear you whisper
I'll be loud for you
But you gotta be there for me too
But you gotta be there for me too
Last year took a toll on me
But I made it with you next to me
Around the world and back again
I hope you're waiting at the end
But I can't do this alone
Sometimes I just need a light
If I call you on the phone
Need you on the other side
So when your tears roll down your pillow like a river
I'll be there for you
I'll be there for you
When you're screaming but they only hear you whisper
I'll be loud for you
I'll be loud for you
I got you I promise
Let me be honest
Love is a road that goes both ways
When your tears roll down your pillow like a river
I'll be there for you
But you gotta be there for me too
But you gotta be there for me too
Boy I'm holding on to something
Won't let go of you for nothing
I'm running running just to keep my hands on you
There was a time that I was so blue
What I got to do to show you
I'm running running just to keep my hands on you
Running running just to keep my hands on you
Running running just to keep my hands on you
So I'm running running just to keep my hands on you
But you gotta be there for me too
But you gotta be there for me too
'''
>>> song.count('you')
38

 



  

posted @ 2018-03-20 15:54  炸了吗兄弟  阅读(207)  评论(0编辑  收藏  举报