Python基础一

2021-07-13 22:05

阅读:488

标签:format   输入   连接   分支   too   env   air   多行注释   inpu   

一、注释

 1 #!/usr/bin/env python
 2 #created by Baird
 3 
 4 #单行注释
 5 
 6 ‘‘‘ 多行注释‘‘‘
 7 
 8 """多行注释"""
 9 
10 name = "Baird"
11 name2= name
12 print(name,name2)
13 
14 name = "pencil"
15 print(name,name2)

 

二、分支和循环

 1 #!/usr/bin/env python
 2 #created by Baird
 3 
 4 for i in range(0,10,2):      #其中0位置为闭区间,10为开区间,2为增量
 5     print("loop ",i)
 6 
 7 age = 57
 8 count = 0
 9 
10 while count :
11     guess_age = int(input("guess age:"))
12     if guess_age==age:
13         print("yes,you got it!")
14         break
15     elif guess_age>age:
16         print("too big")
17     else:
18         print("too small")
19     count = count+1
20 else:                 #当循环正常结束时执行,即非break跳出
21     print("You‘ve tried too many times!")

 

三、输入和格式化输出

 1 #!/usr/bin/env python
 2 #created by Baird
 3 
 4 name = input("name:")
 5 age = int(input("age:"))      #强转成整型
 6 job = input("job:")
 7 salary = input("salary:")
 8 
 9 info = ‘‘‘
10 ------------info of ‘‘‘+name+‘‘‘------------
11 name:‘‘‘+name+‘‘‘
12 age:‘‘‘+str(age)+‘‘‘          #使用+号连接时需为字符串
13 job:‘‘‘+job+‘‘‘
14 salary:‘‘‘+salary
15 print(info)
16 
17 info2 = ‘‘‘
18 ------------info2 of %s------------
19 name:%s
20 age:%d
21 job:%s
22 salary:%s‘‘‘ %(name,name,age,job,salary)
23 print(info2)
24 
25 info3 = ‘‘‘
26 ------------info3 of {_name}------------
27 name:{_name}
28 age:{_age}
29 job:{_job}
30 salary:{_salary}‘‘‘.format(_name=name,_age=age,_job=job,_salary=salary)
31 
32 print(info3)
33 
34 info4 = ‘‘‘
35 ------------info4 of {0}------------
36 name:{0}
37 age:{1}
38 job:{2}
39 salary:{3}‘‘‘.format(name,age,job,salary)
40 
41 print(info4)

 

Python基础一

标签:format   输入   连接   分支   too   env   air   多行注释   inpu   

原文地址:https://www.cnblogs.com/baird/p/9541318.html


评论


亲,登录后才可以留言!