算数运算符 比较运算符 赋值运算符 逻辑运算符 身份运算符
python运算优先级 链式赋值 交叉赋值 解压缩
x = 1
y = 2
print(x + y)
print(x - y)
print(x * y)
print(x / y)
print(x // y)# 整除
print(x ** y)# 幂 次方
3
-1
2
0.5
0
1
x = 1
y = 2
print(x == y)
print(x != y)
print(x > y)
print(x <= y)
False
True
False
True
a = 300
print(a)
id(a)
300
73389712
a +=100#a=a+100
print(a)
id(a)
400
73389584
a *=10
print(a)
id(a)
4000
73389968
a /= 10#a=a/10
print(a)
id(a)
400.0
73389296
a //=10
print(a)
id(a)
40.0
73389568
逻辑运算符
and(和)/or(或)/not(否)
true and true = true true and false = flase ture or true = true true or flase = true flase or flase = flase not true = flase not flase = true
身份运算符
x = 300
y = 300
print(id(x)==id(y))
False
x = y =300
print(id(x)==id(y))
True
链式赋值
x = 10
y = 10
z = 10
x = y = z =10
print(x,y,z)
10 10 10
交叉赋值
x = 10
y = 20
z = x
x = y
y = z
print(x,y)
20 10
x,y=y,x
print(x,y)
20 10
解压缩赋值
nick_hobby_list = ['run','eat']
somebody_hobby_list = ['chi','he','piao','du']
hobby1 = nick_hobby_list[0]
hobby2 = nick_hobby_list[1]
print(hobby1,hobby2)
run eat
somebody_hobby_list = ['chi','he','piao','du']
hobby1,hobby2 = somebody_hobby_list
print(hobby1,hobby2)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-24-a17f049955a2> in <module>
1 somebody_hobby_list = ['chi','he','piao','du']
----> 2 hobby1,hobby2 = somebody_hobby_list
3 print(hobby1,hobby2)
ValueError: too many values to unpack (expected 2)
somebody_hobby_list= ['piao','666','233','13']
hobby1,hobby2,hobby3,hobby4 = somebody_hobby_list
print(hobby1,hobby3)
piao 233
NICKOFNAME = 'nick'
NICKOFNAME = 'zhouruixing'
print(NICKOFNAME)
zhouruixing
somebody_hobby_list= ['piao','666','233','13']
hobby1,_,hobby3,_ = somebody_hobby_list
print(hobby1,hobby3)
piao 233
somebody_hobby_list= ['piao','666','233','13']
hobby1,_,_,_ = somebody_hobby_list
print(hobby1)
piao
somebody_hobby_list= ['piao','666','233','13','chi','he','du','ni']
hobby1,*_,hobby6,_ = somebody_hobby_list
print(hobby1,hobby6)
piao du
if语句
编程语言:人与计算机交流的介质 编程:人操控计算机工作,让计算机干活,代替劳动力 计算机模仿人类 比如面前一条狗
-
判断是哪种生物-狗
-
小奶狗还是疯狗 3.1. 小奶狗 -上千抚摸 3.2. 疯狗-远离 人有判断能力,计算机也有判断能力 python中判断使用if语句
if 条件: 代码块 code1 code2
dog_name1 = '疯狗'
dog_name2 = '小奶狗'
if dog_name1 == '疯狗':
print('远离它')
远离它
if.........else
dog_name1 = '疯狗1'
dog_name2 = '小奶狗'
if dog_name1 == '疯狗':
print('y远离他')
else:
print('抚摸它')
抚摸它
180以上全票 150-170半票 120以下免票
height = 150
if height >= 180:
print('全票')
else:
if height <= 120:
print('免票')
else:
print('半票')
半票
height = input('请输入你的身高: ')
height = int(height)
if height >= 180:
print('全票')
elif height <= 120:
print('免票')
elif height >= 150 and height <= 170:
print('半票')
else:
print('你可能不适合我')
请输入你的身高: 155
半票
if 嵌套
height = 160
if height >= 180:
print('全票')
else:
if height <= 120:
print('免票')
else:
print('你可能太高了')
你可能太高了
猜年龄
while True:
age = input('请输入年龄: ')
age = int(age)
if age > 19:
print("太长了")
elif age == 19:
print('bonggo')
break
else:
print("太小了")
请输入年龄: 15
太小了
请输入年龄: 20
太长了
请输入年龄: 19
bonggo