赋值打印,注释,各种循环语句
name = "您好,世界"
msg = "i'm alex li"
print(name)
print(msg)
#单行注释
"""
多行注释
"""
---------------------------------------------------------------------
三种输出方式:
一:
拼接方式,不推荐使用,每次都要开辟新内存
二:
info2 = '''
---- info of {_name}----
name:{_name}
age:{_age}
job:{_job}
salary:{_salary}
'''.format(_name = name,
_age = age,
_job = job,
_salary = salary)
print(info2)
三:
info3 = '''
---- info of {0} ----
name:{0}
age:{1}
job:{2}
salary:{3}
'''.format(name, age, job, salary)
print(info3)
---------------------------------------------------------------------
条件判断:
import getpass
#密文导入框架(终端显示)
#存密码
_username = 'zhou'
_password = 'abc123'
username = input("username:")
#password = getpass.getpass("password:")
password = input("password:")
#if else逻辑
#else强制缩进
if _username == username and _password == password:
#if的子代码
print("welcome to user {name} python".format(name=username))
else:
#else的子代码
print("Invalid username or password!")
print("我的Tina")
---------------------------------------------------------------------
while循环:
count = 0
while True:
print("count:", count)
count = count + 1 #count += 1
if count == 10 :
break
---------------------------------------------------------------------
三次输入练习:
age_of_oldboy = 56;
count = 0
while count < 3:
guess_age = int(input("guess age:"))
if guess_age == age_of_oldboy :
print("YES,you got it")
elif guess_age > age_of_oldboy :
print("think smaller...")
else:
print("think bigger...")
count += 1
print("count:", count)
else:
print("对不起,您的次数已经使用完了")
---------------------------------------------------------------------
for循环打印:
#循环10次 range相当于变量的集合,一组数据 0-9
#打印0,2,4,6,8 0,10,2
#打印1,3,5,7,9 1,10,2
for i in range(0, 10, 3) :
print("loop", i)
for i in range(10) :
print("loop", i)
for循环的嵌套:
for i in range(4) :
print("-------", i)
for j in range(4) :
print(j)
if j > 1 :
break
---------------------------------------------------------------------