day 04

1、赋值运算

age = 18

增量赋值

age + 10 # age = age +10

print(age)

age %= 3 # age = age % 3

print(age)

 

链式赋值

x = 10

y = x

z = y

x = y = 10

print(x,y,z)

print(id(x),id(y),id(z))

 

交叉赋值

m = 10

n = 20

temp = n

n = m

m = temp

m,n = n,m

print(m,n)

 

解压赋值

salaries = [11,22,33,44,55,66,77]

mon1 = salaries[0]

mon1 = salaries[1]

mon1 = salaries[2]

mon1 = salaries[3]

mon1 = salaries[4]

mon1 = salaries[5]

mon1 = salaries[6]

mon1,mon2,mon3,mon4,mon5,mon6,mon7 = salaries

取前三位

mon1,mon2,mon3,*_ = salaries

print(mon1)

print(mon2)

print(mon3)

print(_)

取中间

_,*middle,_ = salaries

print(middle)

 

2、逻辑运算符

not:not将紧跟其后的那个条件取反

print(not 10 > 3)

 

and:连接左右两个条件,只有两个条件同时为True,最终结果才为True

print("gwj" == "gwj" and 10 > 3)

True

print("gwj" == "GWJ" and 10 > 3)

false

 

or:连接左右两个条件,但凡有一个条件为True,最终结果就为True

print("gwj" == "gwj" or 10 > 3)

True

print("gwj" == "GWJ" or 10 > 3)

True

 

优先级:()> not > and > or

print(3 > 4 and 4 > 3 or not 1 == 3 and 'x' == 'x' or 3 > 3)

True

print(1 != 1 or 3 > 4 and not "xx" == "gwj" ro 1> 10 and 9 > 3 and 1 >0)

fales

 

短路运算:偷懒原则

print(1 > 0 and 1 != 1 and 3 > 2)

# int ----> bool:非0即True,0即Flase
# int ----> bool:True 1,Flase 0

二、流程控制之if判断

1、什么是判断

  判断 条件:

    做XXX

  否则:

    做YYY

2、为何要判断

  为了让计算机像人一样根据条件的判断结果去做不同的事情

3、如何用:

  完整语法

print("start...")

if 条件1:

  代码1

  代码2

  代码3

elif 条件2:

  代码1

  代码2

  代码3

elif 条件3:

  代码1

  代码2

  代码3

......

else:

  代码1

  代码2

  代码3

print("end..")

 

案例

# 如果:成绩>=90,那么:优秀
# 如果成绩>=80且<90,那么:良好
#
# 如果成绩>=70且<80,那么:普通
#
# 其他情况:很差

score = input("请输入成绩:")

if score >= "90":

  print("优秀")

elif score >= "80":

  print("良好")

elif score >= "70":

  print("一般")

else:

  print("很差")

 

 

三、与用户交互

1、接收用户输入

案例1

print("start...")
inp_name = input("请输入您的用户名:") # inp_name = "gwj"
inp_pwd = input("请输入您的密码:") # inp_pwd = "123"

db_name = "gwj"
db_pwd = 456

if inp_name == db_name and inp_pwd == db_pwd:
print("用户登录成功")
else:
print("账号或密码输入错误")

print("end...")

 

案例2

score = input("请输入您的成绩:") 
score = int(score) # 将纯数字组成的字符串转成整型

if score >= 90: 
print("优秀")
elif score >= 80:
print("良好")
elif score >= 70:
print("普通")
else:
print("很差")

 

python2的raw_input()等同于python3的input

 

2、格式化输出

name = input("your name: ")
age = input("your age: ")

print("my name is %s my age is %s" % (name,age))
print("my name is %s my age is %d" % ("gwj",19))
print("my name is %s my age is %d" % ("gwj","19")) # %d 只能接收数字
print("my name is %s my age is %s" % ("gwj",[1,2,3]))

posted @ 2020-12-18 15:32  gwj02  阅读(85)  评论(0)    收藏  举报