3='''
------info of {0}------
name:{0}
age:{1}
job:{2}
salary:{3}
'''.format(name,age,job,salary)
print (info3)
优点:
缺点:不直观,维护性较第一种方式好,但是不如第二种
5,python注释
#单行注释
''',"""多行注释,同时也是多行字符串设置方式 ''' ------info of {0}------
name:{0}
age:{1}
job:{2}
salary:{3}
'''
6,import 导入模块
它可以出现在程序中的任何位置。
import module
关键字 模块名
如果要同时导入多个模块,只需要在模块名之前用逗号进行分隔:
import module1,module2,module3.......
在用import语句导入模块时最好按照这样的顺序: 1、python 标准库模块
2、python 第三方模块
3、自定义模块
from module(模块名)import name(函数名)
导入指定模块的指定函数
from math import floor
7,if
_username = "cat"
_password = "a"
username = input("username:")
password = input("password:")
if username == _username and password == _password:
print("oye".format(name=username))
else:
print("fuck off baby!")
print(username, password)
注意:
==表示精确等于
!=表示不等于
python语言严格遵守缩进,如果缩进错误,则系统报错
对变量的直接引用通过{}内输入变量名实现,例如{name}
8,循环,while ,for
8.1 for循环
age_of_you=45
for i in range(3):
gass_age=int(input("gassage:"))
if gass_age==age_of_you:
print("oye")
break
elif gass_age<age_of_you:
print("too small")
else:
print("too big")
else:
print("try too many times.. fuck off")
注意:
break:退出当前的逻辑块,比如if,for,while
while,for的else语法是python独有的
count=count+1在python中等效与count+=1
range(10,X)X为步长,默认为1,可以不输入
8.2 while循环
age_of_you=45
count=0
while count<3:
gass_age=int(input("gassage:"))
if gass_age==age_of_you:
print("oye")
break
elif gass_age<age_of_you:
print("too small")
else:
print("too big")
count+=1
if count ==3:
countine_confirm=input("agein?Y|N:")
if countine_confirm != "n":
count=0
else:
print("thank you")
8.3,continue
for i in range(20):
if i <10:
print("loop:",i)
else:
continue
print(i)
注意:continue 跳过本次循环,直接进入下一次循环