[レコーダなし]Pythonを使用したWebスクリプト-Day 3の作成

6437 ワード

1.9 Conditionals part One


📝 if - else

  • 条件文
  • インスタントラーメン~~、でなければ~
  • # 문법
    if CONDITION:
        ...
    else:
        ...
        
    # 예시
    def plus(a, b):
        if type(b) is int or type(b) is float:
            return a + b
        else:
            return None
            
    plus(12, "10")
  • type():タイプチェック
  • 1.10 if else and or

  • Boolean Operations
  • x or y:両方ともtrue
  • x and y:両方ともtrue
  • not x:xはfalseでなければならない
  • elif
  • else ifなどの意味
  • 複数のifゲートが使用可能
  • # 예시
    def age_check(age):
        print(f"you are {age}")
        if age < 18:
            print("you can't drink")
        elif age == 18:
            print("you are new to this!")
        elif age > 20 and age < 25:
            print("you are still kind of young")
        else:
            print("enjoy your drink")
    
    age_check(18)
    age_check(23)
    # 결과
    you are 18
    you are new to this!
    you are 23
    you are still kind of young

    1.11 for in


    📝 for loop

  • 逐次操作
  • # 문법
    for 변수명 in 배열:
        body
  • 配列名も配列自体も可能
    ex) days (O), [1, 2, 3, 4, 5] (O)
  • # 예시
    days = {"Mon", "Tue", "Wed", "Thu", "Fri"}
    
    for day in days:
        if day is "Wed":
            break
        else:
            print(x)
    # 결과
    Mon
    Tue
    Wed
  • 変数day
  • item値入力
  • for文実行時生成
  • 順番に動くたびに値が変わる
  • 初期値xを設定し、動作時に使用する
  • break
  • loopを脱退