The following learning content is based on Python2
11. Questions
Print "How is old is?", age = Raw_input () print "So, you're%r old."% Agepython ex11.pyhow old is you? 35So, you're ' re ' old
Input () and raw_input () are Python built-in functions that interact with the user, but function differently.
Raw_input can represent arbitrary strings
Input "on the string to be added"
type int best uses input
12. Prompt others
For Raw_input, you can also let it show a hint that tells others what to enter. You can put a string between () that you want as a hint, as follows:
y = raw_input ("Name?")
Pydoc is a python built-in official document, similar to the man in Linux
13. Parameters, unpacking, variables
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 are: ", second print" Your third variable is: ", Thirdpython ex13.py first 2nd 3rdThe Script is called:ex13.py Your first variable is:first Your second variable is:2nd Your third variable is:3rd
14. Tips and Delivery
from sys import argvscript, user_name = argvprompt = ' > ' 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_namelikes = 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,&Nbsp;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. Read the file
$ VI ex15_sample.txtthis is stuff I typed to a file. It is really cool stuff. Lots and Lots of fun to has in here.
All we have to do is open the ex15_sample.txt.
From sys import argvscript, filename = argvtxt = open (filename) print "Here ' s your file%r:"% filenameprint txt.read () prin T "Type the filename agein:" File_again = Raw_input (">") txt_again = open (file_again) print txt_again.read ()
16. Read and Write files
from sys import argvscript, filename = argvprint "We ' re going to erase %r. " %filenameprint "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. More File operation
From Os.path Import exists
Print "%r"% exists (to_file) #查看to_file有有没用存在, returns a Boolean value
Print "d%"% len (indata) #查看有多少个字节
# Write data to To_file, write to Indata
Output = open (To_file, ' W ')
Output.write (Indata)
Output.close () #关闭
18. Commands, variables, codes, functions
#coding: Utf-8def Nan_nad_nv (nan_count,nv_count): print "Our It boys have%d. "% Nan_count print" We have it girls with%d. The number of "% nv_countprint" IT staff: >>> Nan_nad_nv (60,20) Results The number of IT staff is: we have 60 of it boys. We have 20 of it girls.
19. Functions and variables
def cheese_and_crackers (Cheese_count, boxes_of_crackers): print "You had%d cheeses!"% cheese_count print "you h Ave%d boxes of crackers! "% boxes_of_crackerscheese_and_crackers (20, 30) results you have a total of cheeses! You have a boxes of Crackers!amount_of_cheese = Amount_of_crackers = Cheese_and_crackers (Amount_of_cheese, Amount_o F_crackers) Results you have cheeses! You have boxes of crackers!
20. Functions and Files
Grasp the following parameter meanings
F.seek (0)
F.readline ()
Stupid way to learn Python (11-20)