笨方法学习Python(11-20),,以下学习内容以pyt


以下学习内容以python2为基准

11、提问

print"Howoldareyou?",age=raw_input()print"So,you're%rold."%agepythonex11.pyHowoldareyou?35So,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、参数、解包、变量

fromsysimportargvscript,first,second,third=argvprint"Thescriptiscalled:",scriptprint"Yourfirstvariableis:",firstprint"Yoursecondvariableis:",secondprint"Yourthirdvariableis:",thirdpythonex13.pyfirst2nd3rdThescriptiscalled:ex13.pyYourfirstvariableis:firstYoursecondvariableis:2ndYourthirdvariableis:3rd


14、提示和传递

fromsysimportargvscript,user_name=argvprompt='>'print"Hi%s,I'mthe%sscript."%(user_name,script)print"I'dliketoaskyouafewquestions."print"Doyoulikeme%s?"%user_namelikes=raw_input(prompt)print"Wheredoyoulive%s?"%user_namelives=raw_input(prompt)print"Whatkindofcomputerdoyouhave?"computer=raw_input(prompt)print"""Alright,soyousaid%raboutlikingme.Youlivein%r.Notsurewherethatis.Andyouhavea%rcomputer.Nice."""%(likes,lives,computer)$pythonex14.pyZedHiZed,I'mtheex14.pyscript.I'dliketoaskyouafewquestions.DoyoulikemeZed?>yesWheredoyouliveZed?>AmericaWhatkindofcomputerdoyouhave?>TandyAlright,soyousaid'yes'aboutlikingme.Youlivein'America'.Notsurewherethatis.Andyouhavea'Tandy'computer.Nice.


15、读取文件

$viex15_sample.txtThisisstuffItypedintoafile.Itisreallycoolstuff.Lotsandlotsoffuntohaveinhere.

我们要做的是打开ex15_sample.txt

fromsysimportargvscript,filename=argvtxt=open(filename)print"Here'syourfile%r:"%filenameprinttxt.read()print"Typethefilenameagein:"file_again=raw_input(">")txt_again=open(file_again)printtxt_again.read()


16、读写文件

fromsysimportargvscript,filename=argvprint"We'regoingtoerase%r."%filenameprint"Ifyoudon'twantthat,hitCRIL-C(^C)."print"Ifyoudowantthat,hitRETURN."raw_input("?")print"Openingthefile..."target=open(filename,'w')print"Truncatingthefile.Goodbye!"target.truncate()print"NowI'mgoingtoaskyouforthreelines."line1=raw_input("line1:")line2=raw_input("line2:")line3=raw_input("line3:")print"I'mgoingtowritewhesetothefile."target.write(line1)target.write("\n")target.write(line2)target.write("\n")target.write(line3)target.write("\n")print"Andfinally,wecloseit."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-8defnan_nad_nv(nan_count,nv_count):print"我们IT男生有%d。"%nan_countprint"我们IT女生有%d。"%nv_countprint"IT人员数目为:">>>nan_nad_nv(60,20)结果IT人员数目为:我们IT男生有60。我们IT女生有20。


19、函数和变量

defcheese_and_crackers(cheese_count,boxes_of_crackers):print"Youhave%dcheeses!"%cheese_countprint"Youhave%dboxesofcrackers!"%boxes_of_crackerscheese_and_crackers(20,30)结果Youhave20cheeses!Youhave30boxesofcrackers!amount_of_cheese=10amount_of_crackers=50cheese_and_crackers(amount_of_cheese,amount_of_crackers)结果Youhave10cheeses!Youhave50boxesofcrackers!


20、函数和文件

掌握以下参数意义

f.seek(0)

f.readline()

笨方法学习Python(11-20)

评论关闭