1.字符串练习:

http://news.gzcc.cn/html/2017/xiaoyuanxinwen_1027/8443.html

取得校园新闻的编号

str="http://news.gzcc.cn/html/2017/xiaoyuanxinwen_1027/8443.html"
print(str[-14:-5])

 

https://docs.python.org/3/library/turtle.html

产生python文档的网址

addr1="https://docs.python.org/3/library/"
addr2=".html"
addr=addr1+"turtle"+addr2
print(addr)

 

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

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

i=1
for i in range(2,9):
    print('http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'.format(i))

 

练习字符串内建函数:strip,lstrip,rstrip,split,count
用函数得到校园新闻编号

str="http://news.gzcc.cn/html/2017/xiaoyuanxinwen_1027/8443.html"
print(str.rstrip('.html')[-9:])
str="http://news.gzcc.cn/html/2017/xiaoyuanxinwen_1027/8443.html"
print(str.rstrip('.html').split('_')[1])

 

用函数统计一歌词中单词出现的次数

songstr='''
Booty Music
Deepside
When the beat going like that (boom boom)
Girl, I wanna put you up in my room
I wanna put you up against that wall
Throw you on the bed and take your clothes off (clothes off)
Everybody don't like it slow (it slow)
Consider me one of them folk
Let's get to it (get to it)
Do it (do it)
Get to it (get to it)
Get to it
Kinda crunk and I'm off this scene
A look back and that's my shit
Once I had a shot of that good Petrone
I'm all in my boxers like Bobby Jones
Everybody don't like it slow (it slow)
Consider me one of them folk
And let's do it (do it)
Do it (do it)
Let's do it (do it)
Let's do it
Baby, that's the way I like it (hey)
That's the way you like it (hey)
That's the way we like it
Making love to booty music
Go Leo! It's your birthday (hey)
Go Virgo! It's your birthday (hey)
Go Pisces! It's your birthday
Making love to booty music
I feel it all in my bones
Tryna keep up with that tempo
Make it all night till your back gets sore
Till we just can take it no more
Look at that ass like "Oh my god"
We clap back till you give me applause
Sounded like (clap clap clap clap clap clap)Mhm… when I get in them drawers
When I get in them drawers
Get your hair in tangles
I wrap it in my ankles
I'm gripping on your handles
I'm getting on different angles
Like ten, five cent, ten cent, dollar
Ten, five cent, ten cent, dollar
Ten, five cent, ten cent, dollar
Let me see you pop it (pop it)
That's the way I like it (hey)
That's the way you like it (hey)
That's the way we like it
Making love to booty music
Go Aries! It's your birthday (hey)
Go Libra! It's your birthday (hey)
Go Scorpio! It's your birthday
Making love to booty music
Don't stop, get it, get it
Pop that coochie, let me hit it
I wanna rock, I wanna rock
Let me get a little bit of that bumpy ride
Don't stop, get it, get it
Let me put some stank up in it
I wanna rock, I wanna rock
Let me get a little bit of that bumpy ride
(~~~~~Music~~~~~)
Does anybody out there wanna
Let me get a little bit of that bumpy ride
Oooohh…
Oooohh…
Baby, that's the way I like it (hey)
That's the way you like it (hey)
That's the way we like it
Making love to booty music
Go Cancer! It's your birthday (hey)
Go Capricorn! It's your birthday (hey)
Go Aquarius! It's your birthday
Making love to booty music
That's the way I like it (hey)
That's the way you like it (hey)
That's the way we like it
Making love to booty music
Go Taurus! It's your birthday (hey)
Go Gemini! It's your birthday (hey)
Go Sag! It's your birthday
Making love to booty music
~~~END~~~
'''
print(songstr.count('booty'))

 

将字符串分解成一个个的单词。

str='Get your hair in tangles'
print(str.split())

 

2.组合数据类型练习

分别定义字符串,列表,元组,字典,集合,并进行遍历。

总结列表,元组,字典,集合的联系与区别。

#字符串遍历
string='hello,python'
print(string)
for str in string:
    print(str)


#列表遍历
course=['Mike','Jhon','Hello']
print(course)
for cou in course:
    print(cou)


#元组遍历
tuple=('A','apple','pink','java')
print(tuple)
for tup in range(len(tuple)):
    print(tuple[tup])


#字典遍历
dictionary={'python':1,'java':2,'C++':3}
for dic in dictionary.items():
    print(dic)


#集合遍历
s=set(['dic','cou','tup'])
for i in s:
    print(i)

总结:
列表是最常用的Python数据类型,创建列表只要把逗号分隔的不同的数据项使用方括号括起来就可以。
元组是存一组数据,一旦创建,便不能修改,也叫做只读列表。元组创建很简单,只需要在括号中添加元素,并使用逗号隔开。
字典是一种可变容器模型,它每个键值对小括号()用冒号‘:’分割,每个对之间用逗号‘,’分割,整个字典包括在花括号‘{}’里面。
集合是一个无序的,不重复的数据组合。