以下学习内容以python2为基准
11、提问print "How old are you?",
age = raw_input()
print "So, you're %r old." % age
python ex11.py
How old are you? 35
So, you're '35' old
input()与raw_input()都是Python的内建函数,实现与用户的交互,但是功能不同。
raw_input可代表任意字符串
input在字符串上要加‘ ’
int类型最好使用input
12、提示别人
对于 raw_input 而言,你还可以让它显示出一个提示,从而告诉别人应该输入什么东西。你可以在 () 之间放入一个你想要作为提示的字符串,如下所示:
y = raw_input("Name? ")
pydoc是python内置的官方文档,类似于linux中的man
13、参数、解包、变量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 first 2nd 3rd
The script is called: ex13.py
Your first variable is: first
Your second variable is: 2nd
Your third variable is: 3rd
14、提示和传递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)
$ python ex14.py Zed
Hi Zed, I'm the ex14.py script.
I'd like to ask you a few questions.
Do you like me Zed?
> yes Where do you live Zed?
> America What kind of computer do you have?
> Tandy
Alright, so you said 'yes' about liking me.
You live in 'America'. Not sure where that is.
And you have a 'Tandy' computer. Nice.
15、读取文件$ vi 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.
我们要做的是打开 ex15_sample.txtfrom sys import argv
script, filename = argv
txt = open(filename)
print "Here's your file %r:" % filename
print txt.read()
print "Type the filename agein:"
file_again = raw_input(">")
txt_again = open(file_again)
print txt_again.read()
16、读写文件from sys import argv
script, filename = argv
print "We're going to erase %r." %filename
print "If you don't want that, hit CRIL-C(^C)."
print "If you do want that, hit RETURN."
raw_input("?")
print "Opening the file..."
target = open(filename, 'w')
print "Truncating the file. Goodbye!"
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 whese 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()
17、更多文件操作
from os.path import exists
print "%r" % exists(to_file) #查看to_file有有没用存在,会返回布尔值
print "d%" % len(indata) #查看有多少个字节
# 朝to_file里写入数据,写入indata
output = open(to_file, 'w')
output.write(indata)
output.close() #关闭
18、命令、变量、代码、函数#coding:utf-8
def nan_nad_nv(nan_count,nv_count):
print "我们IT男生有%d。" % nan_count
print "我们IT女生有%d。" % nv_count
print "IT人员数目为:"
>>> nan_nad_nv(60,20)
结果
IT人员数目为:
我们IT男生有60。
我们IT女生有20。
19、函数和变量def cheese_and_crackers(cheese_count, boxes_of_crackers):
print "You have %d cheeses!" % cheese_count
print "You have %d boxes of crackers!" % boxes_of_crackers
cheese_and_crackers(20, 30)
结果
You have 20 cheeses!
You have 30 boxes of crackers!
amount_of_cheese = 10
amount_of_crackers = 50
cheese_and_crackers(amount_of_cheese, amount_of_crackers)
结果
You have 10 cheeses!
You have 50 boxes of crackers!
20、函数和文件
掌握以下参数意义
f.seek(0)
f.readline()
原文地址:http://blog.51cto.com/8672771/2073551