day04 運算子 流程控制 (if while/of)
阿新 • • 發佈:2019-01-02
1. 運算子
算數運算子
+ - * /
int / float :數字型別
# print(10 + 3.1)
# print(10 / 3)
# print(10 // 3)
# print(10 % 3)
# print(10 ** 2)
賦值運算子
增量賦值
age=18
age=age+1
print=(age) #19
age+=1
print(gae)#19
交叉賦值
x=18 y=22
x,y=22,18
print(x,y) # 22,18
鏈式賦值
x=y=z=10
print(x,y,z) #10,10,10
解壓賦值
列表的解壓賦值
# salaries=[1.1,2.2,3.3,4.4,5.5]
# a,b,_,_,_=salaries
# a,b,*_=salaries
# *_,a,b=salaries
# a,*_,b=salaries
# print(a,b,)
2. 字典的解壓賦值
dic={'aaa':1,'bbb':2,'ccc':3}
# x,y,z=dic
# print(x,y,z)
邏輯運算子
and or not
# and: 左右兩個條件必須同時成立,最終結果才為True
# print(10 < 3 and 3 == 3)
# or: 左右兩個條件只要有一個成立,最終結果就為True
# print(10 < 3 or 3 == 3)
# print(10 < 3 or 3 < 3)
# not:將緊跟其後的條件結果取反
# print(not 10 > 3)
# print(not 10 < 3 or 3 < 3)
# res=(10 > 3 and 3 == 1) or ((4 < 3 and True) or (not 1 > 2 or 3 > 2))
# print(res)
比較運算子
# ==
# print(10 != 3)
# 瞭解
# msg1='abcdef'
# msg2='abcz'
# print(msg2 > msg1)
l1=[1,'aaa',333]
l2=[2,'b']
print(l2 > l1)
2. 流程控制
if
語法一:
# if 條件:
# 程式碼1
# 程式碼2
# 程式碼3
# gender='female'
# age=18
# is_beautiful=True
#
# if gender == 'female' and age > 16 and age < 20 and is_beautiful:
# print('開始表白。。。。')
#
# print('其他程式碼')
#語法二:
# if 條件:
# 程式碼1
# 程式碼2
# 程式碼3
# else:
# 程式碼1
# 程式碼2
# 程式碼3
# gender='female'
# age=26
# is_beautiful=True
#
# if gender == 'female' and age > 16 and age < 20 and is_beautiful:
# print('開始表白。。。。')
# else:
# print('阿姨好')
#
# print('其他程式碼')
#語法三:
# if 條件1:
# if 條件2:
# 程式碼1
# 程式碼2
# 程式碼3
# gender='female'
# age=18
# is_beautiful=True
# is_successfull=True
#
# if gender == 'female' and age > 16 and age < 20 and is_beautiful:
# print('開始表白。。。。')
# if is_successfull:
# print('在一起,,,')
# else:
# print('逗你玩呢。。。')
# else:
# print('阿姨好')
#
# print('其他程式碼')
#
#語法四:
# if 條件1:
# 程式碼1
# 程式碼2
# 程式碼3
# elif 條件2:
# 程式碼1
# 程式碼2
# 程式碼3
# elif 條件3:
# 程式碼1
# 程式碼2
# 程式碼3
# .......
# else:
# 程式碼1
# 程式碼2
# 程式碼3
'''
如果:成績>=90,那麼:優秀
如果成績>=80且<90,那麼:良好
如果成績>=70且<80,那麼:普通
其他情況:很差
'''
score=input('your score: ')
score=int(score)
if score >= 90:
print('優秀')
elif score >= 80:
print('良好')
elif score >= 70:
print('普通')
else:
print('很差')
迴圈(while/for)
引入:
# name='egon'
# pwd='123'
#
# inp_name=input('your name: ')
# inp_pwd=input('your password: ')
# if inp_name == name and inp_pwd == pwd:
# print('login successfull')
# else:
# print('name or password error')
#
# inp_name=input('your name: ')
# inp_pwd=input('your password: ')
# if inp_name == name and inp_pwd == pwd:
# print('login successfull')
# else:
# print('name or password error')
#
# inp_name=input('your name: ')
# inp_pwd=input('your password: ')
# if inp_name == name and inp_pwd == pwd:
# print('login successfull')
# else:
# print('name or password error')
#
#迴圈就是重複做某件事
# 語法:
# while 條件:
# 程式碼1
# 程式碼2
# 程式碼3
# while True:
# 1+1
# n=1
# while n < 10:
# print(n)
# n+=1
# name='egon'
# pwd='123'
#
# tag=True
# while tag:
# inp_name=input('your name: ')
# inp_pwd=input('your password: ')
# if inp_name == name and inp_pwd == pwd:
# print('login successfull')
# tag=False
# else:
# print('name or password error')
# while+break:終止本層迴圈
name='egon'
pwd='123'
while True:
inp_name=input('your name: ')
inp_pwd=input('your password: ')
if inp_name == name and inp_pwd == pwd:
print('login successfull')
break
else:
print('name or password error')
1. 運算子
算數運算子
+ - * /
int / float :數字型別
# print(10 + 3.1)
# print(10 / 3)
# print(10 // 3)
# print(10 % 3)
# print(10 ** 2)
賦值運算子
增量賦值
age=18
age=age+1
print=(age) #19
age+=1
print(gae)#19
交叉賦值
x=18 y=22
x,y=22,18
print(x,y) # 22,18
鏈式賦值
x=y=z=10
print(x,y,z) #10,10,10
解壓賦值
列表的解壓賦值
# salaries=[1.1,2.2,3.3,4.4,5.5]
# a,b,_,_,_=salaries
# a,b,*_=salaries
# *_,a,b=salaries
# a,*_,b=salaries
# print(a,b,)
2. 字典的解壓賦值
dic={'aaa':1,'bbb':2,'ccc':3}
# x,y,z=dic
# print(x,y,z)
邏輯運算子
and or not
# and: 左右兩個條件必須同時成立,最終結果才為True
# print(10 < 3 and 3 == 3)
# or: 左右兩個條件只要有一個成立,最終結果就為True
# print(10 < 3 or 3 == 3)
# print(10 < 3 or 3 < 3)
# not:將緊跟其後的條件結果取反
# print(not 10 > 3)
# print(not 10 < 3 or 3 < 3)
# res=(10 > 3 and 3 == 1) or ((4 < 3 and True) or (not 1 > 2 or 3 > 2))
# print(res)
比較運算子
# ==
# print(10 != 3)
# 瞭解
# msg1='abcdef'
# msg2='abcz'
# print(msg2 > msg1)
l1=[1,'aaa',333]
l2=[2,'b']
print(l2 > l1)
2. 流程控制
if
語法一:
# if 條件:
# 程式碼1
# 程式碼2
# 程式碼3
# gender='female'
# age=18
# is_beautiful=True
#
# if gender == 'female' and age > 16 and age < 20 and is_beautiful:
# print('開始表白。。。。')
#
# print('其他程式碼')
#語法二:
# if 條件:
# 程式碼1
# 程式碼2
# 程式碼3
# else:
# 程式碼1
# 程式碼2
# 程式碼3
# gender='female'
# age=26
# is_beautiful=True
#
# if gender == 'female' and age > 16 and age < 20 and is_beautiful:
# print('開始表白。。。。')
# else:
# print('阿姨好')
#
# print('其他程式碼')
#語法三:
# if 條件1:
# if 條件2:
# 程式碼1
# 程式碼2
# 程式碼3
# gender='female'
# age=18
# is_beautiful=True
# is_successfull=True
#
# if gender == 'female' and age > 16 and age < 20 and is_beautiful:
# print('開始表白。。。。')
# if is_successfull:
# print('在一起,,,')
# else:
# print('逗你玩呢。。。')
# else:
# print('阿姨好')
#
# print('其他程式碼')
#
#語法四:
# if 條件1:
# 程式碼1
# 程式碼2
# 程式碼3
# elif 條件2:
# 程式碼1
# 程式碼2
# 程式碼3
# elif 條件3:
# 程式碼1
# 程式碼2
# 程式碼3
# .......
# else:
# 程式碼1
# 程式碼2
# 程式碼3
'''
如果:成績>=90,那麼:優秀
如果成績>=80且<90,那麼:良好
如果成績>=70且<80,那麼:普通
其他情況:很差
'''
score=input('your score: ')
score=int(score)
if score >= 90:
print('優秀')
elif score >= 80:
print('良好')
elif score >= 70:
print('普通')
else:
print('很差')
迴圈(while/for)
引入:
# name='egon'
# pwd='123'
#
# inp_name=input('your name: ')
# inp_pwd=input('your password: ')
# if inp_name == name and inp_pwd == pwd:
# print('login successfull')
# else:
# print('name or password error')
#
# inp_name=input('your name: ')
# inp_pwd=input('your password: ')
# if inp_name == name and inp_pwd == pwd:
# print('login successfull')
# else:
# print('name or password error')
#
# inp_name=input('your name: ')
# inp_pwd=input('your password: ')
# if inp_name == name and inp_pwd == pwd:
# print('login successfull')
# else:
# print('name or password error')
#
#迴圈就是重複做某件事
# 語法:
# while 條件:
# 程式碼1
# 程式碼2
# 程式碼3
# while True:
# 1+1
# n=1
# while n < 10:
# print(n)
# n+=1
# name='egon'
# pwd='123'
#
# tag=True
# while tag:
# inp_name=input('your name: ')
# inp_pwd=input('your password: ')
# if inp_name == name and inp_pwd == pwd:
# print('login successfull')
# tag=False
# else:
# print('name or password error')
# while+break:終止本層迴圈
name='egon'
pwd='123'
while True:
inp_name=input('your name: ')
inp_pwd=input('your password: ')
if inp_name == name and inp_pwd == pwd:
print('login successfull')
break
else:
print('name or password error')