一、 __future__ 模块
Guido van Rossum (Python之父)拥有一架时光机,因为在人们通常要求增加语言新特性的时候,这个特性通常都已经实现了。当然我们一般人是不允许进入这架时光机的。
Guido将时光机的一部分以__future__这个模块的形式融入了python。通过它可以导入那些在未来会成为标准Python组成部分的新特性。
二、 逻辑运算
‘‘‘x or y x True,则返回x‘‘‘
>>> print(1 or 2) 1 >>> print(2 or 1) 2 >>> print(0 or 1) 1 >>> print(2 or 0) 2
‘‘‘x and y x True,则返回y‘‘‘
>>> print(0 and 2) 0 >>> print(2 and 0) 0 >>> print(1 and 2) 2 >>> print(2 and 1) 1
# “>","<"的优先级高于not > and > or ,True 和 False 不会转化为1或0 >>> print(2 or 1<3) 2 >>> print(1<3 or 2) True >>> print(1>3 and 2) False >>> print(1<3 and 2) 2 >>> print(True or 0) True >>> print(True and 0) 0 >>> print(False or 2) 2 >>> print(False and 2) False
三、 格式化输出
格式一:
1 name = input("请输入姓名:") 2 age = input("请输入年龄:") 3 height = input("请输入身高:") 4 msg = "my name is %s, my age is %s, my height is %s, my process is 80%%" %(name,age,height) 5 print(msg) 6 # 运行结果: 7 # 请输入姓名:liuyankui 8 # 请输入年龄:25 9 # 请输入身高:175 10 # my name is liuyankui, my age is 25, my height is 175, my process is 80%
格式二:
1 name = input("请输入姓名:") 2 age = input("请输入年龄:") 3 job = input(‘请输入工作:‘) 4 hobbie = input(‘请输入爱好:‘) 5 msg = ‘‘‘------------ info of %s ----------- 6 Name : %s 7 Age : %d 8 job : %s 9 Hobbie: %s 10 ------------- end -----------------‘‘‘ %(name,name,int(age),job,hobbie) 11 print(msg) 12 # 输出结果 13 # 请输入姓名:liuyankui 14 # 请输入年龄:25 15 # 请输入工作:IT 16 # 请输入爱好:PingPong 17 # ------------ info of liuyankui ----------- 18 # Name : liuyankui 19 # Age : 25 20 # job : IT 21 # Hobbie: PingPong 22 # ------------- end -----------------
格式三:
1 name = input("请输入姓名:") 2 age = input("请输入年龄:") 3 job = input(‘请输入工作:‘) 4 hobbie = input(‘请输入爱好:‘) 5 msg = ‘‘‘------------ info of {0} ----------- 6 Name :{0} 7 Age : {1} 8 job : {2} 9 Hobbie: {3} 10 ------------- end -----------------‘‘‘ .format(name,age,job,hobbie) 11 print(msg) 12 13 # 运行结果: 14 # 请输入姓名:liuyankui 15 # 请输入年龄:25 16 # 请输入工作:IT 17 # 请输入爱好:PingPong 18 # ------------ info of liuyankui ----------- 19 # Name :liuyankui 20 # Age : 25 21 # job : IT 22 # Hobbie: PingPong 23 # ------------- end -----------------
四、 int 和bool值互相转换
int ----> bool 非零转换成bool True 0 转换成bool 是False
1 >>> print(bool(2)) 2 True 3 >>> print(bool(1)) 4 True 5 >>> print(bool(0)) 6 False 7 >>> print(bool(-2)) 8 True 9 # bool ----> int False ----> 0; True ----> 1 10 >>> print(int(False)) 11 0 12 >>> print(int(True)) 13 1
五、 while...else...语句
count = 0 while count <= 5: count += 1 if count == 3:break print("Loop",count) else: # 当break打断while循环,else的内容就不再执行 print("循环正常执行了") print("------------------")