第四屆【Python 全攻略】Week 5 - Operators, Truthy and Falsy
ฝัง
- เผยแพร่เมื่อ 18 พ.ย. 2024
- ▌導讀:Wendy
2024/11/6 PM 8
▌本次主題:
Chapter 2. Operators, Truthy and Falsy (37-40)
▌課程共同學習主頁:
vip.studycamp....
▌筆記:
hackmd.io/@40R...
▌線上討論及補充:
提醒:因為 TH-cam 說明中,不允許半型的<>符號,以下程式中以全型取代。
• Fuhua Yang:
itema = 23
itemb = 45
print(itema+itemb)
68
• Fuhua Yang:
68
-22
1035
• Yen:
item1 = 23
item2 = 45
total_price = item1+item2
print(total_price)
68
price_different = item1-item2
print(price_different)
-22
• Fuhua Yang:
68
-22
680(這樣才對)
• 萍:
s1 = (length + width) *2
s1
30
s2 = length * width
s2
50
• 耀德 蔡:
length = 10
width = 5
perimeter = 2 * (length + width)
area = length * width
print(perimeter, area)
30 50
• Fuhua Yang:
length = 10
width = 5
area = length * width
print(area)
50
• Fuhua Yang:
area1 = (length+width)*2
print(area1)
30
• Yen:
length = 10
width = 5
Perimeter=(length+width)*2
print(Perimeter)
#30
• Yen:
magic = ["rabbit", "hat", "wand", "cape"]
print("wand" in magic )
#True
• 萍:
lst = ["rabbit", "hat", "wand", "cape"]
"wand" in lst
True
• Fuhua Yang:
wand = ['rabbit','hat','wand','cape']
print('wand' in wand)
• 耀德 蔡:
magic = ["rabbit", "hat", "wand", "cape"]
if "wand" in magic:
print("可以施展魔法!")
else:
print("無法施展魔法。")
• Yen:
apples = 30
pears = 25
if(apples 〉 pears):
print("yes,apples is more than pears ")
#yes,apples is more than pears
• 萍:
apples = 30
pears =25
apples 〉 pears
True
• 耀德 蔡:
apples = 30
pears = 25
if apples 〉 pears:
print("蘋果比梨子多")
else:
print("蘋果比梨子少或一樣多")
• Fuhua Yang:
apples = 30
pears = 25
if apples 〉 pears:
print('apples are mores than pears')
else:
print('pears less than apples')
• Yen:
有
• Yen:
coins = 10
coins *=3
print(coins)
#30 我寫錯了
coins += 5
coins += 5
coins += 5
print(coins)
#25
• 耀德 蔡:
coins = 10
coins += 5 * 3
print(coins)
• Fuhua Yang:
coins += 5
print(coins*3)
45
• 萍:
coins = 10
add = 5
coins += add * 3
25
• Fuhua Yang:
但答案是 25
• Yen:
coins += 5
coins += 5
coins += 5
print(coins)
#25
我誤會題目了 我以為是每次贏 就加 10😆
• Yen:
age = 20
has_pass = True
if age 〉= 18 and has_pass:
print("you can passed")
#you can passed
• Fuhua Yang:
age = 20
has_pass = True
if age 〉=18 and has_pass:
print('你已成年,可以通過')
else:
print('你未成年,不能通過喔')
〉〉你已成年,可以通過
• 耀德 蔡:
age = 20
has_pass = True
if age 〉 18 and has_pass:
print("訪客可以通過。")
else:
print("訪客不可通過。")
• Yen:
and
• 耀德 蔡:
運算子ᅠ說明ᅠ示例ᅠ結果
&ᅠ位元 AND ᅠ 5 & 3 ᅠ 1
`ᅠ`ᅠ位元 OR ᅠ`5
^ᅠ位元 XOR ᅠ 5 ^ 3 ᅠ 6
~ᅠ位元取反ᅠ~5 ᅠ-6
〈〈ᅠ左移ᅠ 5 〈〈 1 ᅠ 10
〉〉ᅠ右移ᅠ 5 〉〉 1 ᅠ 2
• tzuyu ye:
&
• tzuyu ye:
|
• tzuyu ye:
A=60
B=13
A&B=
A|B=
• 萍:
a = 60
b = 13
a & b
12
a | b
61
• Fuhua Yang:
a = 60
b = 13
print(a & b)〉〉12
print(a | b)〉〉60
可是不會自動轉成 二進位
• 耀德 蔡:
a = 60
b = 13
and_result = a & b
print(f"AND: {and_result} (二進位: {bin(and_result)})")
or_result = a | b
print(f"OR: {or_result} (二進位: {bin(or_result)})")
• Fuhua Yang:
我打錯了,應該是 a|b〉〉61
• 萍:
bin(12)
'0b1100'
bin(61)
'0b111101'
• 萍:
可以使用 bin() 函式將 Int 轉換為二進位制
• Yen:
password = "1234"
if password:
print("密碼有效")
else:
print("密碼無效")
#密碼有效
• tzuyu ye:
bool()
• Fuhua Yang:
password = input("")
if password:
print('密碼有效')
else:
print('密碼無效')
• Fuhua Yang:
不確定要不要 input
• Fuhua Yang:
" "
密碼有效
• 耀德 蔡:
password = input()
if password == "":
print("密碼無效")
else:
print("密碼有效")
• 萍:
password = input("輸入密碼: ")
if bool(password):
print("密碼有效")
else:
print("密碼無效")
• 萍:
=== RESTART: /Users/ryohei3364/Desktop/Python/test.py ===
輸入密碼:
密碼無效
=== RESTART: /Users/ryohei3364/Desktop/Python/test.py ===
輸入密碼: ccs
密碼有效
• tzuyu ye:
if 2 or (10 / 0):
print("No error") # 因為 2 為 True,不會執行 10 / 0
• Yen:
numerator = 10
denominator = 0
result = denominator != 0 and numerator / denominator
if denominator != 0:
print("計算結果為:", result)
else:
print("無法執行除法,因為分母為零")
#無法執行除法,因為分母為零
• Fuhua Yang:
if 1 or (10 / 0):
print('no error')
• 萍:
a = 5
if a 〉 0 or 10 / 0:
print("No error")
No error