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.
This is fun.
Yay! Printing.
I'd much rather you 'not'.
I 'said' do not touch this.```

```[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.```

+ 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 "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:
Hens 30
Roosters 82
Now I will count the eggs:
9
Is it true that 3+2 < 5-7?
False
What is 3+2? 5
what is 5-7? -2
oh,that is why it's False
Is it greater? True
Is it greater or equal? True
Is it less or equal? False```

```[root@localhost py]# cat var.py
#!/usr/bin/env python
cars = 100
space_in_a_car = 4.0
drivers = 30
passengers = 90
cars_not_driven = cars - drivers
cars_driven = drivers
carpool_capacity = cars_driven * space_in_a_car
average_passengers_per_car = passengers / cars_driven

print "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 100 cars available.
There are only 70 empty cars today.
We can transport 120.0 people today.
We have 90 to carpool today.
We need to put about 3 in each car.```

```[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_name
print "He\'s %d inches tall." % my_height
print "He\'s %d pounds heavy." % my_weight
print "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.```

```[root@localhost py]# cat string.py
#!/usr/bin/env python
x = "There are %d types of people." %
binary = '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 are  types 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.```

```[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 "." *  # 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,try removing 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
Its fleece was white as snow.
..........
cbeese
Burger```

```[root@localhost py]# cat print4.py
#!/usr/bin/env python
formatter = "%r %r %r %r"
print formatter %(,,,)
print formatter %('one','two','three','four')

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

print formatter %(formatter,formatter,formatter,formatter)
print formatter %(
"That you could type up right.",
"But it didn\'t sing.",
"So I said goodnight."
)```
```[root@localhost py]# python print4.py

'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.'```

```[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  lines if we want,or ,or .
"""```
```[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  lines if we want,or ,or .```

"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
```

```[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?
How tall are you?
How much do you weight?
So,you're '' old,'' tall and '' heavy.```

```[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?
How tall are you?
How much do you weight?
So,you're '' old,'' tall and '' heavy.```

```[root@localhost py]# cat argv.py
#!/usr/bin/env python
from sys 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
The script is called: argv.py

[root@localhost py]# python argv.py cheese apples bread
The script is called: argv.py

```[root@localhost py]# cat argv2.py
#!/usr/bin/env python
from sys 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 live in %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
Where do you like mary?
>bike
What kind of computer do you have?
>aus

Alright,so you said 'no' about liking me.
You live in 'bike'. Not sure where that is.
And you have a 'aus' computer. Nice.```

```[root@localhost py]# cat file.py
#!/usr/bin/env python
from sys import argv
script,filename = argv
txt = open(filename)

print "Here\' your file %r: " %filename
print "Type the file name again: "
file_again = raw_input(">")
txt_again = open(file_again)
```[root@localhost py]# python file.py ex15_sample.txt
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.

Type the file name again:
>ex15_sample.txt
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.

[root@localhost py]#```

close – 关闭文件。跟你编辑器的 文件->保存.. 一个意思。
truncate – 清空文件，请小心使用该命令。
write(stuff) – 将 stuff 写入文件。

```[root@localhost py]# cat file2.py
#!/usr/bin/env python
from sys 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()```

```[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 do want that,hit RETURN.
?
Openning the file ...
Truncating the file.Goodby!
Now I'm going to ask you for three lines.
line :
line :
line :
I'm going to write these to the file.
And finally,we close it.```

```[root@localhost py]# cat bb.txt
```

python练习题集合-1的更多相关文章

1. python练习题集合-2

author:headsen chen date:2018-06-01 15:39:26  习题17,文件的更多操作 [root@localhost py]# echo > cc.txt [ro ...

2. Python练习题 001：4个数字求不重复的3位数

听说做练习是掌握一门编程语言的最佳途径,那就争取先做满100道题吧. ----------------------------------------------------------------- ...

3. Python练习题 028：求3*3矩阵对角线数字之和

[Python练习题 028] 求一个3*3矩阵对角线元素之和 ----------------------------------------------------- 这题解倒是解出来了,但总觉得 ...

4. Python练习题 027：对10个数字进行排序

[Python练习题 027] 对10个数字进行排序 --------------------------------------------- 这题没什么好说的,用 str.split(' ') 获 ...

5. Python练习题 026：求100以内的素数

[Python练习题 026] 求100以内的素数. ------------------------------------------------- 奇怪,求解素数的题,之前不是做过了吗?难道是想 ...

6. Python练习题 025：判断回文数

[Python练习题 025] 一个5位数,判断它是不是回文数.即12321是回文数,个位与万位相同,十位与千位相同. ---------------------------------------- ...

7. Python练习题 024：求位数及逆序打印

[Python练习题 024] 给一个不多于5位的正整数,要求:一.求它是几位数,二.逆序打印出各位数字. ---------------------------------------------- ...

8. Python练习题 004：判断某日期是该年的第几天

[Python练习题 004]输入某年某月某日,判断这一天是这一年的第几天? ---------------------------------------------- 这题竟然写了 28 行代码! ...

9. Python 3 集合基础和概念！

Python 3 集合基础和概念! Python 3中,集合是无序的,所以不能进行切片和索引操作. 创建集合有两个方法:set()方法创建的集合是可变的,可被迭代的:frozenset()方法创建的集 ...

随机推荐

1. # 20145334赵文豪 《Java程序设计》第5周学习总结

20145334赵文豪 <Java程序设计>第5周学习总结 教材学习内容总结 第八章 1.使用try.catch打包System.in.read(),声明throws java.io.IO ...

2. HDU 4334 Trouble （暴力）

Trouble Time Limit: 5000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Statu ...

3. js正则函数match、exec、test、search、replace、split使用介绍

match() 使用正则表达式模式对字符串执行查找,并将包含查找的结果作为数组返回. stringObj.match(rgExp) stringObj 必选项.对其进行查找的 String 对象或字符 ...

4. VC多线程编程

5. transition与animation

以前,一直都知道,transition是animation的一个简化版,甚至不算是动画,而是一种过渡. transition的用法 早两天用transition写了一个按钮滑动的效果,类似于IOS的设 ...

6. erlang-string

string:len("abcdef"). 结果为 6 求字符串的长度 string:equal("abc","abc"). 结果为 tru ...

7. bzoj 4012: [HNOI2015]开店

Description 风见幽香有一个好朋友叫八云紫,她们经常一起看星星看月亮从诗词歌赋谈到 人生哲学.最近她们灵机一动,打算在幻想乡开一家小店来做生意赚点钱.这样的 想法当然非常好啦,但是她们也发现 ...

8. BZOJ 1012: [JSOI2008]最大数maxnumber【线段树单点更新求最值，单调队列，多解】

1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 10374  Solved: 4535[Subm ...

9. intellij idea解除svn关联

有时候项目需要解除svn关联,百度谷歌几篇,以下方式是最简单快捷的. 从.idea文件夹下手,找到了cvs.xml,其内容如下: <?xml version="1.0" en ...

10. Axure 全局变量公式的使用和局部变量

全局变量和全部变量的公式: 1.全局变量在当前用例所有的动作里面都有,都可以设置 2.全局变量的公式在中括号里面的变量可以运算,但是在中括号外面的变量只是起到连接的作用 局部变量: 局部变量只可以在当 ...