7.笨办法学 Python-第七题
#!/usr/bin/python
#coding=utf-8
#笨办法学 Python-第七题
#复习前面的知识点
print '直接语句输出:'
print 'mary had a little lamb.'
print '格式化语句输出:'
print 'its fleece was white as %s' % 'snow'
print '字符串也是可以进行简单的加法和乘法运算:'
print '+'*10
word='p'
word1='y'
word2='t'
word3='h'
word4='o'
word5='n'
print word+word1+word2+word3+word4+word5
知识点:字符串的输出(1.直接输出,格式化输出,通过拼接方法输出)
运行结果:
直接语句输出:
mary had a little lamb.
格式化语句输出:
its fleece was white as snow
字符串也是可以进行简单的加法和乘法运算:
++++++++++
python
8.笨办法学 Python-第八题
#!/usr/bin/python
#coding=utf-8
#笨办法学 Python-第八题
#格式化输出,注意理解%r的作用,能够输出所有的值
print '输出的格式:'
formatter="%r %r %r %r"
print formatter
print formatter % (1,2,3,4)
print formatter % ('one','two','three','four')
print formatter % (True,False,True,False)
print formatter % (formatter,formatter,formatter,formatter)
print formatter % (
"i had this thing",
"that you could type up right",
"but it did't sing",
"so i said goodnight."
)
知识点:格式化输出,注意理解%r的作用,能够输出所有的值(按照原样输出),%r 打印出来的是你写在脚本里的内容,而 %s 打印的是你应该看到的内容
运行结果:
输出的格式:
%r %r %r %r
1 2 3 4
'one' 'two' 'three' 'four'
True False True False
'%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 did't sing" 'so i said goodnight.'
9.笨办法学 Python-第九题
#!/usr/bin/python
#coding=utf-8
#笨办法学 Python-第九题
#字符换打印过程中特殊符号
print '逗号和\\n的使用:'
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 month: ',months
print '三引号的使用:'
print '''
there is something going on here.
with the there double-quotes.
we will be able to type as much as we like.
even 4 lines if we want,or 5,or 6.
'''
知识点:逗号,\n(回车符).三引号(引号中写成什么样子输出就是什么样子)的使用.
运行结果:
逗号和\n的使用:
here are the days: Mon Tue Wed thu Fri Sat Sun
here are the month: Jan
Feb
Mar
Apr
May
Jun
jul
Aug
三引号的使用:
there is something going on here.
with the there double-quotes.
we will be able to type as much as we like.
even 4 lines if we want,or 5,or 6.
10.笨办法学 Python-第十题
针对上面的练习九,两种让字符串扩展到多行的方法:
1.第一种方法是用 \n (back-slash n )隔开,有一个特殊的转义序列,就是 双反斜杠 \\ 。这两个字符组合会打印出一个反斜杠来.单双引号的转义.
2.使用“三引号(triple-quotes)”,也就是 " " "或者是'''
#!/usr/bin/python
#coding=utf-8
#笨办法学 Python-第十题
#转义字符的使用
print '字符串中双引号的转义:'
print "i am 6'2\"tail"
print '字符串中但引号的转义:'
print 'i am 6\'2" tail'
print "\\t 代表tab,\\n 代表回车 \\ 代表 \\"
print '\ti am tabbed in'
print 'i\'m split\non a line'
print "i'm \\ a \\ cat"
print '转义序列和格式化字符串放到一起:'
print '\t this is a %s' % 'dog\n i like that dog'
print '''
i'll do list:
\t* Cat food
\t* friend
\t* Catnip\n\t* Grass
'''
'''
注释部分:
转移字符有那些:
\(在行尾时) 续行符
\\ 反斜杠符号
\' 单引号
\" 双引号
\a 响铃
\b 退格(Backspace)
\e 转义
\000 空
\n 换行
\v 纵向制表符
\t 横向制表符
\r 回车
\f 换页
\other 其它的字符以普通格式输出
'''
知识点:转义字符的使用,\t --->tab, \\----->\,\n----->回车,以及在存在单双引号的情况下使用单双引号
转移字符有那些:
\(在行尾时) 续行符
\\ 反斜杠符号
\' 单引号
\" 双引号
\a 响铃
\b 退格(Backspace)
\e 转义
\000 空
\n 换行
\v 纵向制表符
\t 横向制表符
\r 回车
\f 换页
\other 其它的字符以普通格式输出
运行结果:
字符串中双引号的转义:
i am 6'2"tail
字符串中但引号的转义:
i am 6'2" tail
\t 代表空格,\n 代表回车 \ 代表 \
i am tabbed in
i'm split
on a line
i'm \ a \ cat
i'll do list:
* Cat food
* friend
* Catnip
* Grass
11.笨办法学 Python-第十一题
#!/usr/bin/python
#coding=utf-8
#笨办法学 Python-第十一题
#从终端获取输入的值
print '在每行 print 后面加了个逗号,这样的话print就不会输出新行符而结束这一行跑到下一行去了.'
print 'how old are you?',
age=raw_input()
print 'how tall are you?',
height=raw_input()
print 'you are %s old,%s tall' %(age,height)
知识点:学习使用raw_input方法的使用,以及print在不希望其回车的情况下可以在后面加一个逗号
运行结果:
在每行 print 后面加了个逗号,这样的话print就不会输出新行符而结束这一行跑到下一行去了.
how old are you? 12
how tall are you? 6'12''
you are 12 old,6'12'' tall
12.笨办法学 Python-第十二题
#!/usr/bin/python
#coding=utf-8
#笨办法学 Python-第十二题
#raw_put的提示输入
age=raw_input("please input your ages:")
print 'age 的类型为:',type(age)
age1=int(raw_input("please input your ages:"))
print 'age1 的类型为:',type(age1)
height=raw_input("please input your tall:")
print '使用age和heignt:'
print 'you are %s years old,you are %s tall' % (age,height)
print '使用age1和heignt:'
print 'you are %d years old,you are %s tall' % (age1,height)
知识点:raw_input输出的类型和存在提示语句的输入语句,pydoc可以查看帮助信息:pydoc raw_input
运行结果:
please input your ages:12
age 的类型为:
please input your ages:12
age1 的类型为:
please input your tall:6'12''
使用age和heignt:
you are 12 years old,you are 6'12'' tall
使用age1和heignt:
you are 12 years old,you are 6'12'' tall
13.笨办法学 Python-第十三题
#!/usr/bin/python
#coding=utf-8
#笨办法学 Python-第十三题
#为脚本传递参数,解包,变量
#导入相应的模块argv是所谓的参数变量
from sys import argv
#argv “解包(unpack)”,与其将所有参数放到同一个变量下面,把 argv中的东西解包,将所有的参数依次赋予左边的变量名
script,frist,second,third=argv
print 'the script is called:',script
print 'the frist variable is: ',frist
print 'the second variable is ',second
print 'the third variable is ',third
知识点:在执行脚本的时候给出变量的值.
运行结果:
python 13.py a b c ---->执行脚本(当参数少于三个或者多于三个都会出现报错)
the script is called: 13.py
the frist variable is: a
the second variable is b
the third variable is c
14.笨办法学 Python-第十四题
#!/usr/bin/python
#coding=utf-8
#笨办法学 Python-第十四题
#raw_input & 传递参数
from sys import argv
script,user_name =argv
prompt='>'
print 'hi %s,i am the %s script' %(user_name,script)
print 'i would like to ask you a few questions'
print 'do you like me, %s' % user_name
likes=raw_input(prompt)
print 'where do you live, %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)
知识点:argv中的参数,在执行脚本的时候必须给出,通过使用prompt,和raw_input(prompt)实现交互式界面形式
这些可以用于安装脚本的编写.
执行结果:
python 14.py wz
hi wz,i am the 14.py script
i would like to ask you a few questions
do you like me, wz
>yes
where do you live, wz
>bj
what kind of computer do you have?
>hp
alright,so you said 'yes ' about liking me.
you live in 'bj '. not sure where that is.
and you have a 'hp' computer.nice
文件读取相关操作: https://segmentfault.com/n/1330000007008456(自己总结的)
15.笨办法学 Python-第十五题
#!/usr/bin/python
#coding=utf-8
#笨办法学 Python-第十五题
#通过变量获取读取文件名称,文件的相关操作
from sys import argv
script,file_name=argv
print 'the read of file name is %s' %file_name
file=open(file_name,'rb+')
print file.read()
file.close()
file_name1=raw_input("please input other file_name >")
file1=open(file_name1,'rb+')
print file1.readline()
file1.close()
知识点: 文件的打开和关闭,简单文件读取.读取文件名为传参得到的.
运行结果:
python 15.py 15_sample.txt
the read of file name is 15_sample.txt
this is stuff i typed into a file
it is really cool stuff
lots and lots of fun to have in here.
please input other file_name >2.py
#!/usr/bin/python
16.笨办法学 Python-第十六题
#!/usr/bin/python
#coding=utf-8
#笨办法学 Python-第十六题
#文件的相关操作
from sys import argv
script,file_name=argv
print 'the read of file name is %s' %file_name
#打开一个文件
file=open(file_name,'rw+')
print '''
文件操作的命令:
1.close – 关闭文件。跟你编辑器的 文件->保存.. 一个意思。
2.read – 读取文件内容。你可以把结果赋给一个变量。
3.readline – 读取文本文件中的一行。
4.truncate – 清空文件,请小心使用该命令。
5.write(stuff) – 将 stuff 写入文件。
'''
#打印文件内容
print file.read()
#由于文件在读取文件之后指针已经在文章的最后了,在清空文档是不成功的.
#移动指针位置
file.seek(0,0)
#清空文件内容
file.truncate()
#对该文件进行写操作
file.write('line1\n')
file.write('line2\n')
file.write('line3\n')
#返回文章当前位置
print file.tell()
#读取文件内容
file.seek(0,0)
#返回文章当前位置
print file.tell()
print file.read()
#关闭文件
file.close()
知识点: read(),write(),truncate(),seek(),tell(),close(),readline(),readlines()的使用(详细的讲解看上面的地址)
运行结果:
python 16.py 15_sample.txt
the read of file name is 15_sample.txt
文件操作的命令:
1.close – 关闭文件。跟你编辑器的 文件->保存.. 一个意思。
2.read – 读取文件内容。你可以把结果赋给一个变量。
3.readline – 读取文本文件中的一行。
4.truncate – 清空文件,请小心使用该命令。
5.write(stuff) – 将 stuff 写入文件。
this is stuff i typed into a file
it is really cool stuff
lots and lots of fun to have in here.
line1
line2
line3
line1
line2
line3
18
0
line1
line2
line3