changed 2 years ago
Published Linked with GitHub

Python Tutorial 🐍

Chapter 1


📍 GMJH

2023/07/10 ~ 2023/07/13


Today Topic

  • if / else
    • elif
    • conditional operator
    • logic operator
  • loop
    • while
    • for

Before we start


Slido


Tools ⚙️


Replit


Warm up 🔥


X = X + 1


特別的運算子


if / else


應該會希望程式在
符合某些條件 的時候
才執行相關的指令 👀


當符合 條件時


if

if 條件:
    要執行的程式
if a == 10:
    print("a is 10")
  • 現在的 == 跟昨天的 = 不一樣 🧠
  • 要注意 : 跟縮排!

python 中的縮排

必須要一致!

  • tab
  • 4 個空格

conditional operator

條件運算子

  • == : 等於
  • != : 等於
  • < : 小於
  • > : 大於
  • <= : 小於等於
  • >= : 大於等於

condition & bool

print( 10 > 2 )
print( 5 <= 3 )

a=5
b=20
print( a+b == 25 )

print( a!=b )

print( (a!=b) == True )

Example

a = 59
if a >= 60 :
    print("pass exam")

if a < 60 :
    print("fail exam")
x=3
y=4

if x==y:
    print("same")
    
if x!=y:
    print("not same")

else

條件不符合 時執行

if a>b:
    print("a is bigger than b")
else:
    print("a is smaller equal then b")

也要注意 : 跟縮排!


如果還想要判斷更多形況呢?


elif

判斷 score 通過、被當、死當

score = int(input("input a number"))
if score < 40:
    print("死當 @@")
elif score < 60:
    print("被當 ==")
else:
    print("過了 ouo")

elif 需要注意的地方

猜測 if+elifif+if 的輸出結果

a=10
b=5
if a==10:
    print("first")
elif b==b:
    print("second")
a=10
b=5
if a==10:
    print("first")
if b==b:
    print("second")

if/else 小練習

試著把剛剛的範例改寫成 if/else

a = 59
if a >= 60 :
    print("pass exam")

if a < 60 :
    print("fail exam")
x=3
y=4

if x==y:
    print("same")
    
if x!=y:
    print("not same")

if/else 練習 - 判斷奇偶數

如何判斷奇偶數?

a = int(input("input a number"))
if ??? :
    print("even")
else :
    print("odd")


if/elif/else 練習-猜數字

  • 先自定一個 ans 變數
  • 輸入
    • 讓使用者輸入一個數字 guess
  • 輸出
    • 如果 guess 大於 ans
      輸出 guess is larger than ans
    • 如果 guess 小於 ans
      輸出 guess is smaller than ans
    • 如果 guess 等於 ans
      輸出 U guess the answer !!!

multi-conditions

程式中要如何表達 一次符合多個條件 呢?


Example

  • a > 10a 是奇數
  • score > 60bouns > 5
  • bouns > 5

  • nested if/else
  • logic operator

nested if/else

巢狀 if/else


Example

  • a > 10a 是奇數
    • 輸出 a>10 and odd
  • a > 10a 是偶數
    • 輸出 a>10 and even

Example

  • a > 10a 是奇數
    • 輸出 a>10 and odd
  • a > 10a 是偶數
    • 輸出 a>10 and even

a > 10 這個條件是共同的!


Example

if a > 10 and a%2:
    print("a>10 and odd")
if a > 10 and a%2 == 0:
    print("a>10 and even")
if a > 10:
    if a%2 :
        print("a>10 and odd")
    else:
        print("a>10 and even")

要注意縮排 !!!


小練習

試著把「判斷閏年」用 巢狀 if/else 改寫


if else hell


logic operator

邏輯運算子:

  • and
  • or
  • not

邏輯運算子範例

print( 2>=3 and 9==9 )
print( 2>=3 or 9==9 )

print( not 8==9 )
print( 3>=2 and not 8==9 )
print( 3!=2 or not 8==9 )

綜合 if/else 練習 - 閏年判斷

如何判斷機 平年閏年

規則:

  • 平年:
    • 除以 4 不整除
    • 除以 100 可整除,且除以 400 不整除
  • 閏年:
    • 除以 4 可整除,且除以 100 不整除
    • 除以 400 可整除

閏年判斷

閏年: print("leap year")
平年: print("normal year")

year = int( input("input a year") )

# 寫一些 if/else ...

details of bool & other data type


來講一些 boolean 與 其他資料型態 的細節


int & bool

if 666:
    print("ok 666")
if 1:
    print("ok 1")
if 0:
    print("ok 0")
if -1:
    print("ok -1")
if -999:
    print("ok -999")
if not 0:
    print("not 0")
if not 12:
    print("not 12")

int & bool

  • 0 會被視為 False
  • 其他的數(包括負數) 會被視為 True

None & bool

n = None
if n:
    print("None")
if not n:
    print("not None")
Select a repo