First, comments
1 #!/usr/bin/env python2 #created by Baird3 4 #single-line comment5 6 " "Multi-line comments" "7 8 """Multi-line comments"""9 TenName ="Baird" OneName2=name A Print(name,name2) - -Name ="Pencil" the Print(name,name2)
Ii. Branches and loops
1 #!/usr/bin/env python2 #created by Baird3 4 forIinchRange (0,10,2):#where 0 positions are closed, 10 is open and 2 is incremental5 Print("Loop", i)6 7Age = 578Count =09 Ten whileCount < 3: Oneguess_age = Int (input ("Guess Age:")) A ifguess_age==Age : - Print("yes,you got it!") - Break the elifGuess_age>Age : - Print("too Big") - Else: - Print("too small") +Count = count+1 - Else:#executes when the loop ends normally, i.e. non-break jumps + Print("You ' ve tried too many times!")
Iii. input and format output
1 #!/usr/bin/env python2 #created by Baird3 4Name = input ("Name:")5age = Int (input ("Age :"))#strong turn into integral type6Job = input ("Job:")7Salary = input ("Salary:")8 9info =" "Ten ------------Info of" "+name+" "------------ One Name:" "+name+" " A Age :" "+STR (age) +" "A string is required to connect #使用 + number - Job:" "+job+" " - Salary:" "+Salary the Print(Info) - -Info2 =" " - ------------Info2 of%s------------ + name:%s - age:%d + job:%s A salary:%s" "%(name,name,age,job,salary) at Print(Info2) - -Info3 =" " - ------------Info3 of {_name}------------ - Name:{_name} - Age:{_age} in Job:{_job} - Salary:{_salary}" ". Format (_name=name,_age=age,_job=job,_salary=salary) to + Print(INFO3) - theInfo4 =" " * ------------Info4 of {0}------------ $ name:{0}Panax Notoginseng Age:{1} - job:{2} the Salary:{3}" ". Format (name,age,job,salary) + A Print(Info4)
Python Foundation One