1.声明变量
# 声明一个变量name用来存储一个字符串'apollo' name = 'apollo' # 声明一个变量age用来存储一个数字20 age = 20 # 在控制台打印变量name中存储的字符串和变量age中存储的数字 # 打印结果:apollo 20 print(name,age) # 在控制台打印字符串"name"和"age" print('name','age') # 打印结果:name age
2.控制台交互
username = input('please input your username :') password = input('please input your password :') print('当前登录用户:', username, password) please input your username :apollo please input your password :111111 当前登录用户: apollo 111111
3.逻辑运算
# == , != , <= , >= , < , > 逻辑运算符 print(1 == 1) # True print(1 == 2) # False print(1 == "1") # False print(1 != 2) # True print(1 != 1) # False print(1 <= 2) # True print(1 >= 2) # False print(1 < 2) # True print(1 > 2) # False print(1 == 1 and 2 == 2) # True print(1 == 1 and 1 == 2) # False print(2 == 1 and 1 == 2) # False print(1 == 1 or 2 == 2) # True print(1 == 1 or 1 == 2) # True print(2 == 1 or 1 == 2) # False print(not 1 == 1) # False print(not 1 == 2) # True
4.流程控制
##1 如果if跟随的条件为真,那么执行属于if中的语句 if 1 == 1: print("1==1真的") ##2 如果if跟随的条件为假,那么不执行属于if的语句,然后寻找else,执行属于else中的语句 if 1 == 2: print("假的") else: print("1==2假的") ##3 如果if条件不成立,会进行第二次判断elif,如果elif条件成立,则执行属于elif中的语句,如不成立则else if 1 == 2: print("1==2") elif 1 == 1: print("1==1") else: print("全是骗人的") ##4 for循环 for i in range(10): # 当i为5时,停止当次循环回到最开始继续循环 if i == 5: continue # 当i为7时,停止全部循环 if i == 7: break # 打印结果: 0,1,2,3,4,6 print(i) ##5 死循环 a = 0 sum = 0 while a < 10: a += 1 sum += a print(sum) while True: username = input("username:") password = input("password:") if username == "apollo" and password == "111111": print("successful") break else: continue