python基础之二:占位符、格式化输出、while else 、逻辑运算

1、占位符和格式化输出

示例代码

 1 #格式化输出
 2 # % s d
 3 # name = input('请输入姓名')
 4 # age = input('请输入年龄')
 5 # height = input('请输入身高')
 6 # msg = "我叫%s,今年%s 身高 %s" %(name,age,height)
 7 # print(msg)
 8 """
 9 name = input('请输入姓名:')
10 age = input('请输入年龄:')
11 job = input('请输入工作:')
12 hobbie = input('你的爱好:')
13 
14 msg = '''------------ info of %s -----------
15 Name  : %s
16 Age   : %d
17 job   : %s
18 Hobbie: %s
19 ------------- end -----------------''' %(name,name,int(age),job,hobbie)
20 print(msg)
21 """
22 name = input('请输入姓名')
23 age = input('请输入年龄')
24 height = input('请输入身高')
25 msg = "我叫%s,今年%s 身高 %s 学习进度为3%%s" %(name,age,height)
26 print(msg)
View Code
 

 2、while else语句

1 count = 0
2 while count <= 5 :
3     count += 1
4     if count == 3:break
5     print("Loop",count)
6 
7 else:
8     print("循环正常执行完啦")
9 print("-----out of while loop ------")
View Code

3、逻辑运算

 1 #and or not
 2 #优先级,()> not > and > or
 3 # print(2 > 1 and 1 < 4)
 4 # print(2 > 1 and 1 < 4 or 2 < 3 and 9 > 6 or 2 < 4 and 3 < 2)
 5 # T or T or F
 6 #T or F
 7 # print(3>4 or 4<3 and 1==1)  # F
 8 # print(1 < 2 and 3 < 4 or 1>2)  # T
 9 # print(2 > 1 and 3 < 4 or 4 > 5 and 2 < 1)  # T
10 # print(1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8)  # F
11 # print(1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6)  # F
12 # print(not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6) # F
13 
14 #ps  int  ----> bool   非零转换成bool True   0 转换成bool 是False
15 # print(bool(2))
16 # print(bool(-2))
17 # print(bool(0))
18 # #bool --->int
19 # print(int(True))   # 1
20 # print(int(False))  # 0
21 
22 
23 '''x or y x True,则返回x'''
24 # print(1 or 2)  # 1
25 # print(3 or 2)  # 3
26 # print(0 or 2)  # 2
27 # print(0 or 100)  # 100
28 
29 
30 # print(2 or 100 or 3 or 4)  # 2
31 
32 # print(0 or 4 and 3 or 2)
33 '''x and y x True,则返回y'''
34 # print(1 and 2)
35 # print(0 and 2)
36 print(2 or 1 < 3)
37 print(3 > 1 or 2 and 2)
View Code

猜你喜欢

转载自www.cnblogs.com/funyou/p/11884278.html