笨方法學習Python(11-20)
阿新 • • 發佈:2018-02-27
python以下學習內容以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.txt
from 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()
笨方法學習Python(11-20)