9.129-26节

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

1.反斜杠以及转义字符的用法 多上网查资料

2.输入

PRint "how old are you?"

age = raw_input()

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

结果:

how old are you?

_ (输入)

how old are you?_ (输入)

print "how old are you?"

raw_input(">>")

结果:

how old are you?

>>_

3.参数,解包,变量

from sys import argv

a,b,c = argv

print a,b,c

结果(文件名为a.py 后面参数为 1,2)

a.py 1 2

注意参数变量个数要和解包数量想对应

4.文件处理

from sys import argv

file1,file2 = argv

print "opening the file "

a = open (file2,"w")

print "truncating the file "

a.truncate()

print "two lines:"

a.write("abcdef")

a.write("\n")

a.write('ghijk')

a.write(\n)

结果:

opening the file

truncating the file

two lines:

abcdef

ghijk

还有更多的文件操作 各种命令

多看资料

5.函数

def print_two(*args):

arg1,arg2 = args

print "arg1: %r,arg2: %r")%(arg1,arg2)

def print_one(arg1):

print "arg1: %r"%arg1

print_two("fan","year")

print_one("cool")

结果:

'fan''year'

'cool'

def add(a,b):

return a-b

print add(6,2)

结果:

4

注意函数有严格的缩进要求

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航