Python学习12-21

1、

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

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

2、

from sys import argv

script,first,second,third=argv

print "The script is called:",script 
print "Your first variable is:",first 
print "Your second variable is:",second
print "Your third variable is:",third 

运行时:python ex13.py  red green blue

练习:将raw_input和argv一起使用,让你的脚本从用户手上得到更多的输入

rom sys import argv

script,first,second,third=argv

age=raw_input("How old are you ?")
height=raw_input("How much do you height?")
weight=raw_input("How much do you weight?")

print "The script is called: ",script 
print "Your first variable is: %s new value is %s" %(first,age)
print "Your second variable is: %s new value is %s " %(second,height)
print "Your third variable is: %s new value is %s" %(third,weight)

3、

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 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)

这样每次用raw_input可以看到输入的提示用户的字符

4、读取文件

<pre name="code" class="python">rom sys import argv

script ,filename=argv

txt=open(filename)

print "Here's your file %r:" %filename
print txt.read()
txt.close();


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

txt_again=open(file_again)

print txt_again.read()
txt_again.close()


 
在window 下使用python -m pydoc  file 可以查看read()命令 

5、文件操作

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 "Opening the file..."
target=open(filename,'w')

print "Truncating the file..."
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()

open(filename)就是使用‘r’模式,这是open()函数默认的工作方式

6、将一个文件中的的内容拷贝到你个文件中

from sys import argv
from os.path import exists

script,from_file,to_file=argv
print "Copying from %s to %s " %(from_file,to_file)

in_file=open(from_file)
indata=in_file.read()

print "The input file is %d bytes long"% len(indata)

print  "Does the output file exist? %r" %exists(to_file)
print  "Ready,hit RETURN to continue,CTRL-C to abort."
raw_input()

out_file=open(to_file,'w')
out_file.write(indata)

print "Alright,all done."

out_file.close()
in_file.close()
cat这个命令,它只能在Linux和OSX下使用

7、函数

# this one is like your script with argv
def print_two(*args):
 arg1,arg2=args
 print "arg1: %r,arg2:%r" %(arg1,arg2)

#ok,that *args is actually pointless,we can just do this
def print_two_again(arg1,arg2):
 print "arg1: %r,arg2:%r" %(arg1,arg2)

#this just takes one argument
def print_one(arg1):
 print "arg1: %r" %arg1

#this one takes no arguments
def print_none():
 print "I got nothin'."

print_two("mouse1","mouse2")
print_two_again("mouse1","mouse2")
print_one("First")
print_none()

1、函数和文件

from sys import argv

script,input_file=argv
def print_all(f):
 print f.read()
 
def rewind(f):
 f.seek(0)

def print_a_line(line_count,f):
 print line_count,f.readline()
 
current_file=open(input_file)

print "First let's print the whole file:\n"

print_all(current_file)

print "Now let's rewind,kind of like a tape.\n"

rewind(current_file)

print "Let's print three lines:"

current_line=1
print_a_line(current_line,current_file)

current_line=current_line+1
print_a_line(current_line,current_file)

current_line=current_line+1
print_a_line(current_line,current_file)

在window 下使用python -m pydoc file 就可以查看seek函数的用法和介绍

当offset=0代表从文件开始算起,1代表从当前的位置算起,2代表从文件末尾算起。

故f.seek(0) 就回到了文件的开始也就是说 f.seek(0)将f的文件指针恢复到文件开头

EOF指的是end of file ,f.readline() 读取文件的一行。readline()函数返回的内容中包含文件本来就有\n,print 打印优惠添加一个\n。解决办法是在f.readline()的后面添加一个逗号。


21、函数可以返回东西

def add(a,b):
 print "ADDING %d + %d" %(a,b)
 return a+b
 
def subtract(a,b):
 print "SUBTRACTING %d - %d" %(a,b)
 return a-b 
 
def multiply(a,b):
 print "MULTIPLYING %d * %d" %(a,b)
 return a*b 
 
def divide(a,b):
 print "DIVIDEING %d / %d" %(a,b)
 return a/b
 

age=add(30,5)
height=subtract(78,4)
weight=multiply(90,2);
iq=divide(100,2)

print "Age: %d,Height: %d, Weight: %d, IQ: %d "%(age,height,weight,iq) 

what=add(age,subtract(height,multiply(weight,divide(iq,2))))
print "That bocomes:",what,"can you do it by hand?"
-------------------------------------------------------------------------------------------------------------------------------------------

(1)如果一个字符串包含很多需要转义的字符,对每一个字符都进行转义会很麻烦。为了避免这种情况,我们可以在字符串前面加个前缀 r ,表示这是一个 raw 字符串,里面的字符就不需要转义了。

(2)如果要表示多行字符串,可以用'''...'''表示:

'''Line 1
Line 2
Line 3'''
和'Line 1\nLine 2\nLine 3'效果一致

(3)字符串的编码问题

Python在后来添加了对Unicode的支持,以Unicode表示的字符串用u'...'表示,比如:

print u'中文'  
注意: 不加 u ,中文就不能正常显示。

(4)如果中文字符串在Python环境下遇到 UnicodeDecodeError,这是因为.py文件保存的格式有问题。可以在第一行添加注释

# -*- coding: utf-8 -*-

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值