在python中,变量赋值或者其他语句后不需要加”;“
python中,缩进特别重要!
条件语句和循环语句,要加”:“
一、变量赋值
name = "cecelia"name2 = nameprint("My name is ",name,name2)
二、注释符号的使用
1.使用#,#与注释内容之间要有空格
# first trying
2.使用‘’‘或“”“
'''for i in range(0,10,2): if i<3: print("loop",i) else: continue print("hhe")''' '''除了用于注释外,还可以用于将多行内容赋值给一个变量
msg='''gf_of_oldboy = "chen rong hua" print("hello ,",gf_of_oldboy)'''print(msg) 三、用户输入(input的使用)
name = input("name")age = input("age")
2.密文效果# 开头需要引入python 标准库
import getpass
username = input("username:") password = getpass.getpass("password")
# 要在cmd下才能运行出结果
3.在输出中连接输入变量的值
3.1 %s 或 %dmsg = '''---information of %s---name:%sage:%djob:%s''' % (name, name, int(age), job) print(msg)
3.2 .format(),{变量名}
info2 = '''---information2 of {_name}---name:{_name}age:{_age}job:{Job}'''.format(_name=name, _age=age, Job=job) print(info2)
3.3 .format(),{输入序号}
info3 = '''---information3 of {0}---name:{0}age:{1}job:{2}'''.format(name, age, job) print(info3)
四、if...(elif...)else...条件语句(注意,if语句后要加:)4.1
if _username == username and password == _passwd: print("welcome user {name} login ...".format(name=username))else: print("invalid username or password")
4.2
if age == age_of_oldboy: print("yes,you got it.")elif age>age_of_oldboy: print("think smaller")else: print("think older")
五、循环语句5.1 while 循环(while 语句后面要加:)count = 0 while count <3:
age = int(input("guess age:")) if age == age_of_oldboy: print("yes,you got it.") break elif age>age_of_oldboy: print("think smaller") else: print("think older") count=count+1else: print("you have tried so many times")
5.2 for 循环(for 语句后面要加:)
for i in range(0,10,2): if i<3: print("loop",i) else: continue print("hhe")
for 语句,range(arg1,arg2,arg3)中
arg1:初始值(包括)
arg2:终值(不包括)
arg3:步长