基本運算符和if判斷

基本運算符

算數運算符 比較運算符 賦值運算符 邏輯運算符 身份運算符python

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 = trueui

身份運算符

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語句

編程語言:人與計算機交流的介質 編程:人操控計算機工做,讓計算機幹活,代替勞動力 計算機模仿人類 好比面前一條狗spa

  1. 判斷是哪一種生物-狗code

  2. 小奶狗仍是瘋狗 3.1. 小奶狗 -上千撫摸 3.2. 瘋狗-遠離 人有判斷能力,計算機也有判斷能力 python中判斷使用if語句ip

if 條件: 代碼塊 code1 code2input

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如下免票string

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
相關文章
相關標籤/搜索