python集合例题_python练习题集合-1

author:headsen chen

date : 2018-05-31  17:59:04

notice:本文素材来自于:<< 笨方法学python >> 这本书,由本人重新整理如下:

1.print的用法:

[root@localhost py]#cat print1.py#!/usr/bin/env python

print "Hellow world"

print "Hello Again"

print "I like typing this."

print "This is fun."

print "Yay! Printing."

print "I\'d much rather you 'not'."

print "I 'said' do not touch this."[root@localhost py]#/py/print1.py

Hellow world

Hello Again

I like typing this.

Thisisfun.

Yay! Printing.

I'd much rather you'not'.

I 'said' do not touch this.

习题 2: 注释和井号

[root@localhost py]#cat print2.py#!/usr/bin/env python#ou bbbbbbbbbb dddddd

print "This is will run."

#print 4444444444

[root@localhost py]#/py/print2.py

This is will run.

习题 3: 数字和数学计算

名字如下:

+ plus 加号

- minus 减号

/ slash 斜杠

* asterisk 星号

% percent 百分号

< less-than 小于号

> greater-than 大于号

<= less-than-equal 小于等于号

>= greater-than-equal 大于等于号

[root@localhost py]#cat cal.py#!/usr/bin/env python

print "I will now count my chickens:"

print "Hens",25 + 30 /6

print 'Roosters',100-25*3/4

print 'Now I will count the eggs:'

print 2+4+1 -4+6%2-1/4 +6

print "Is it true that 3+2 < 5-7?"

print 3+2 < 5-7

print "What is 3+2?",3+2

print "what is 5-7?",5-7

print "oh,that is why it\'s False"

print "How about some more."

print "Is it greater?",5> -2

print "Is it greater or equal?",5>=-2

print "Is it less or equal?",5 <= -2

[root@localhost py]#python cal.py

I will now count my chickens:

Hens30Roosters82Now I will count the eggs:9Is it true that3+2 < 5-7?

False

Whatis 3+2? 5whatis 5-7? -2oh,thatis why it's False

How about some more.

Is it greater? True

Is it greaterorequal? True

Is it lessor equal? False

习题 4: 变量(variable)和命名

[root@localhost py]#cat var.py#!/usr/bin/env python

cars = 100space_in_a_car= 4.0drivers= 30passengers= 90cars_not_driven= cars -drivers

cars_driven=drivers

carpool_capacity= cars_driven *space_in_a_car

average_passengers_per_car= passengers /cars_drivenprint "There are",cars,"cars available."

print "There are only",cars_not_driven,"empty cars today."

print "We can transport",carpool_capacity,"people today."

print "We have",passengers,"to carpool today."

print "We need to put about",average_passengers_per_car,"in each car."[root@localhost py]#python var.py

There are 100cars available.

There are only70empty cars today.

We can transport120.0people today.

We have90to carpool today.

We need to put about3 in each car.

习题 5: 更多的变量

[root@localhost py]#cat var2.py#!/usr/bin/env python

my_name = 'Zed A. Shaw'my_age= 35 #not a lie

my_weight = 174 #inches

my_height = 75 #lbs

my_eyes = 'blue'my_teech= 'white'my_hair= 'brown'

print "Let\'s talk about %s." %my_nameprint "He\'s %d inches tall." %my_heightprint "He\'s %d pounds heavy." %my_weightprint "Actually that\'s not too heavy."

print "He\'s got %s eyes and %s hair." %(my_eyes,my_hair)print "His teeth are usually %s depending on the coffee." %my_teech

[root@localhost py]#python var2.py

Let's talk about Zed A. Shaw.

He's 75 inches tall.

He's 174 pounds heavy.

Actually that's not too heavy.

He's got blue eyes and brown hair.

His teeth are usually white depending on the coffee.

习题 6: 字符串(string)和文本

[root@localhost py]# cat string.py

#!/usr/bin/env python

x= "There are %d types of people." %10binary= 'binary'do_not= "don\'t"y= "Those who know %s and those who %s." %(binary,do_not)

print x

print y

print"I said: %r." %x

print"I said: '%s'." %y

joke_evaluation= "Isn\'t that joke so funny?! %r"hilarious=False

print joke_evaluation%hilarious

w= "This is the left side of ..."e= 'a string with right side.'print w+ e

[root@localhost py]# python string.py

There are10types of people.

Those who know binary and those who don't.

I said: 'There are 10 types of people.'.

I said:'Those who know binary and those who don't.'.

Isn't that joke so funny?! False

This is the left side of ...a string with right side.

习题 7: 更多打印

[root@localhost py]# cat print3.py

#!/usr/bin/env python

print"Mary had a liitle lamb."print"Its fleece was white as %s." % 'snow'print"Add everyone that Mary went."print"." * 10 # what'd that do?

end1= 'c'end2= 'b'end3= 'e'end4= 'e'end5= 's'end6= 'e'end7= 'B'end8= 'u'end9= 'r'end10= 'g'end11= 'e'end12= 'r'# what that comma at the end,tryremoving it to see what happens.

print end1+ end2 + end3+ end4 + end5 +end6

print end7+end8 +end9 +end10 + end11 +end12

[root@localhost py]# python print3.py

Mary had a liitle lamb.

Its fleece was whiteassnow.

Add everyone that Mary went.

..........

cbeese

Burger

习题 8: 打印

[root@localhost py]# cat print4.py

#!/usr/bin/env python

formatter= "%r %r %r %r"print formatter%(1,2,3,4)

print formatter%('one','two','three','four')

print formatter%(True,False,False,True)

print formatter%(formatter,formatter,formatter,formatter)

print formatter%("I had this thing.","That you could type up right.","But it didn\'t sing.","So I said goodnight.")

[root@localhost py]# python print4.py1 2 3 4

'one' 'two' 'three' 'four'True False False True'%r %r %r %r' '%r %r %r %r' '%r %r %r %r' '%r %r %r %r'

'I had this thing.' 'That you could type up right.' "But it didn't sing." 'So I said goodnight.'

习题 9: 打印

[root@localhost py]# cat print5.py

#!/usr/bin/env python

days= "Mon Tue Wed Thu Fri Sat Sun"months= "Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug"print"Here are the days:",days

print"Here are the months:",months

print"""There\'s something going on here.

With the three double-quotes.

We\'ll be able to type as much as we like.

Even 4 lines if we want,or 5,or 6."""

[root@localhost py]# python print5.py

Here are the days: Mon Tue Wed Thu Fri Sat Sun

Here are the months: Jan

Feb

Mar

Apr

May

Jun

Jul

Aug

There's something going on here.

With the three double-quotes.

We'll be able to type as much as we like.

Even 4 lines if we want,or 5,or 6.

习题10:转义和三引号用法

"I am 6'2\" tall." # 将字符串中的双引号转义

'I am 6\'2" tall.'# 将字符串种的单引号转义

[root@localhost py]# cat print6.py

#!/usr/bin/env python

tabby_cat= "\tI\'m tabbed in."persian_cat= "I\'m split\non a line."backslash_cat= "I\'m \\ a \\ cat."fat_cat= """I\'ll do a list:

\t*Cat food

\t*Fishies"""print tabby_cat

print persian_cat

print backslash_cat

print fat_cat

[root@localhost py]# python print6.py

I'm tabbed in.

I'm split

on a line.

I'm \ a \ cat.

I'll do a list:

* Cat food

* Fishies

习题 11: 提问和交互式输入

[root@localhost py]# cat input.py

#!/usr/bin/env python

print"How old are you?",

age=raw_input()

print"How tall are you?",

height=raw_input()

print"How much do you weight?",

weight=raw_input()

print"So,you\'re %r old,%r tall and %r heavy."%(age,height,weight)

[root@localhost py]# python input.py

How old are you? 35How tall are you? 455How muchdo you weight? 233So,you're'35'old,'455'tall and'233'heavy.

习题 12: 带提示的raw_input()

[root@localhost py]# cat input2.py

#!/usr/bin/env python

age= raw_input("How old are you?")

height= raw_input("How tall are you?")

weight= raw_input("How much do you weight?")

print"So,you\'re %r old,%r tall and %r heavy."%(age,height,weight)

[root@localhost py]# python input2.py

How old are you? 34How tall are you? 544How muchdo you weight? 2342So,you're'34'old,'544'tall and'2342'heavy.

习题 13: 带参数的python文件和导入模组 :每个参数赋予一个变量名: script, first, second

[root@localhost py]# cat argv.py

#!/usr/bin/env pythonfromsys import argv

script,first,second,third=argv

print"The script is called:",script

print"Your first variable is:",first

print"Your second variavle is:",second

print"Your third variavle is:",third

[root@localhost py]# python argv.py 1 2 3The scriptiscalled: argv.py

Your first variableis: 1Your second variavleis: 2Your third variavleis: 3[root@localhost py]# python argv.py cheese apples bread

The scriptiscalled: argv.py

Your first variableis: cheese

Your second variavleis: apples

Your third variavleis: bread

习题14 带参数的python文件2

[root@localhost py]# cat argv2.py

#!/usr/bin/env pythonfromsys import argv

script,user_name=argv

prompt= '>'print"Hi %s,I\'m the %s script." %(user_name,script)

print"I\'d like to ask you a few questions."print"Do you like me %s" %user_name

likes=raw_input(prompt)

print"Where do you like %s?" %user_name

lives=raw_input(prompt)

print"What kind of computer do you have?"computer=raw_input(prompt)

print"""Alright,so you said %r about liking me.

You livein %r. Not sure where that is.

And you have a%r computer. Nice."""%(likes,lives,computer)

[root@localhost py]# python argv2.py mary

Hi mary,I'm the argv2.py script.

I'd like to ask you a few questions.

Do you like me mary>no

Wheredo you like mary?

>bike

What kind of computerdo you have?

>aus

Alright,so you said'no'about liking me.

You livein 'bike'. Not sure where that is.

And you have a'aus' computer. Nice.

习题15: 文件的打开和读取

[root@localhost py]# cat file.py

#!/usr/bin/env pythonfromsys import argv

script,filename=argv

txt=open(filename)

print"Here\' your file %r:" %filename

print txt.read()

print"Type the file name again:"file_again= raw_input(">")

txt_again=open(file_again)

print txt_again.read()

[root@localhost py]# python file.py ex15_sample.txt

Here'your file'ex15_sample.txt':

This isstuff I typed into a file.

Itisreally cool stuff.

Lots and lots of fun to haveinhere.

Type the file name again:>ex15_sample.txt

Thisisstuff I typed into a file.

Itisreally cool stuff.

Lots and lots of fun to haveinhere.

[root@localhost py]#

习题 16: 读写文件

应该记住的命令如下:

close – 关闭文件。跟你编辑器的 文件->保存.. 一个意思。

read – 读取文件内容。你可以把结果赋给一个变量。

readline – 读取文本文件中的一行。

truncate – 清空文件,请小心使用该命令。

write(stuff) – 将 stuff 写入文件。

[root@localhost py]# cat file2.py

#!/usr/bin/env pythonfromsys import argv

script,filename=argv

print"We\'re going to erase %r." %filename

print"If you don\'t want that,hit CTRL-C(^C)."print"If you do want that,hit RETURN."raw_input("?")

print"Openning the file ..."target= open(filename,'w')

print"Truncating the file.Goodby!"target.truncate()

print"Now I\'m going to ask you for three lines."line1= raw_input("line 1:")

line2= raw_input("line 2:")

line3= raw_input("line 3:")

print"I\'m going to write these to the file."target.write(line1)

target.write("\n")

target.write(line2)

target.write("\n")

target.write(line3)

target.write("\n")

print"And finally,we close it."target.close()

运行该脚本:随便取个文件名:bb.txt

[root@localhost py]# python file2.py bb.txt

We're going to erase'bb.txt'.

If you don't want that,hit CTRL-C(^C).

If you dowant that,hit RETURN.?Openning the file ...

Truncating the file.Goodby!Now I'm going to ask you for three lines.

line 1: 1111111111line2: 2222222222line3: 3333333333I'm going to write these to the file.

And finally,we close it.

检验:新生成的bb.txt的文件内容是否为写入的内容

[root@localhost py]# cat bb.txt1111111111

2222222222

3333333333

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值