Python 프로그래밍 소개 - 제어 흐름

어떤 시점에서 프로그램을 작성하는 동안 비즈니스 논리 또는 셸이나 웹 페이지와 같은 클라이언트의 사용자 입력을 기반으로 결정을 내려야 합니다. 또한 프로그램의 일부가 반복적으로 실행되기를 원할 수도 있습니다. 이 기사는 파이썬에서 프로그램의 흐름을 제어하는 ​​방법을 보여줍니다.

목차



  • Conditions
  • If-Elif-Else Statements
  • Nested If Statements
  • If-Else Expression
  • match statement


  • Loops
  • For Loop
  • While Loop
  • Break and Continue Statements

  • Pass statement
  • Try-Except Statements

  • 정황



    If-Elif-Else 문



    Use an if or if-elif-else statement when you want to perform an operation or logic based on some conditions. The overall format is:

    # do not run this!
    # It's just for illustration
    
    if condition1:
        # run if the condition is true, avoid the others if they specified
    elif condition2:
        # run here if condition1 is false and condition2 is true
    else:
        # do something else
    

    Note that in python, indentation matters. In the above statement, there are three blocks or indents - The if, elif, and the else blocks.
    Among the three, only the if block is required. The elif and else parts are optional.

    Let's say we run a restaurant that only allows people 18 years and above. We may set a variable called age_allowed to the number 18. Then we check if the customer's age matches our expectations. Let's set three variables associated with age. Also, add two string variables as messages to the customers.

    # The allowed age
    allowed_age = 18
    
    # age of the first customer
    age_1 = 57
    
    # age of the second customer
    age_2 = 17
    
    # some message to the customer depending on our criterium
    not_ok_message = "You are not allowed to enter!"
    ok_message = "Welcome to the DELISH haven!"
    

    We do the tests:

    # True: prints the ok_message
    if age_1 >= allowed_age:
        print(ok_message)
    
    
    # False: does not print the ok_message
    if age_2 >= threshold_age:
        print(ok_message)
    

    Print an "okay message" if the customer's age is higher or equals the allowed age. We used two if statements or blocks, and that's fine. However, what if the test fails? We could use another if block or add an else block like so

    # True: prints the ok_message
    if age_1 >= allowed_age:
        print(ok_message)
    else:
        print(not_ok_message)
    
    # False: prints the not_ok_message
    if age_2 >= allowed_age:
        print(ok_message)
    else:
        print(not_ok_message)
    

    What if we have multiple conditions that are different? We test many conditions using elif blocks. Only the block whose condition passes executes. If available, an else block executes if all tests fail.

    Let's say we give out separate seats to customers based on their age group. For customers between 18 and 27, we will give them the first-row seats, those between 28 and 37 will take the second-row seats, and so on. Let's assign a seat to a customer!

    # Yeah, a_customer
    a_customer = 45
    
    # I like laziness. Let's save typing
    message = "Please take the {} seat"
    
    if a_customer < 18:
        print("Sorry, our DELISH only allows customers 18 and above!")
    
    # checks between 18 and 27
    elif age <= 27:
        print(message.format("first"))
    
    # checks between 28 and 37
    elif age <= 37:
        print(message.format("second"))
    
    # checks between 38 and 47
    elif age <= 47:
        print(message.format("third"))
    
    # 81 and above
    else:
        print("Well, this part is not always necessary!")
    

    Wondering about the message variable in the above code block? Remember that python has a string format function/method that lets you do something like this "My name is {}, and I am {} years old".format("Peter Griffin", 90).

    One last example, just for fun!

    # Reset age_1 and age_2
    
    age_1 = 5
    age_2 = 30
    
    pro_footballer = True
    
    if (age_2 > 18) and pro_footballer:
        # runs
        print("Seems appropriate, maybe! I guess you deserve more credit!")
    else:
        print("Dude? why not!")
    
    if (age_1 < 10) and pro_footballer:
        print("You are a prodigy! Have you seen the news?")
    else:
        print("You probably read too much!")
    

    중첩된 If 문



    An if statement (top-level) can contain other if-statements (child-level). The child-level or contained if statements can contain other if statements, and so on. For example

    # do not run this!
    # It's just for illustration
    if condition:
        # top level if block
        if another_conditon:
            # second-level if block
            if yet_another_condition:
                # you get the gist
            else:
                # definitely!
        else:
            # second-level else block
    else:
        # top/first-level else block
    

    Heads up! Use nested ifs wisely. Nesting too deep is terrible for eggs! Sorry :)

    If-Else 표현식



    An if-else expression has the pattern True if condition else False . This expression is "give me the True part if the condition is true or, give me the False part". For example, chosen_age is age_2 if we want a pro_footballer else it is age_1 .

    chosen_age = age_2 if pro_footballer else age_1
    
    print("The chosen age is {} years old".format(chosen_age))
    # The chosen age is 30 years old
    

    매치 진술



    You can use a match statement in place of an if-elif-else with multiple elif-blocks. The format is

    match an_expression:
        case option_1:
            # do something if an_expression equals/matches option_1
        case option_2:
            # do something if an_expression equals/matches option_2
        ...
        case option_n:
            # do something if an_expression equals/matches option_n
        case _:
            # do something if an_expression does not match any of the provided options
    

    Only the code in the first block that matches the expression executes. In the case of no match, the default case block case _ runs.

    Let's say we have a list of fruits and would like to react to a customer's choice of the best fruit.

    
    best_fruit = 'oranges'
    
    match best_fruit:
        case 'apples':
            print('nice fruit..but not as nice as...whatever!')
        case 'oranges':
            print('even better...Lorem ipsum...Don\'t worry')
        case 'mangoes':
            print('You made a great choice!')
        case 'banana':
            print('We are quite far from the trees...Might you pick something else?')
        case _:
            print('Don\'t know what this is...Try again?')
    
    

    The code above will print 'even better...Lorem ipsum...Don't worry' because best_fruit is oranges in the example.

    If you want to react to multiple options, use the pipe (|) operator. For example

    
    food = 'rice'
    
    match food:
        case 'rice' | 'potatoes' | 'corn':
            print('We are rich in carbohydrates, and you can spend it!')
        case 'carrot' | 'cabbage' | 'lettuce':
            print('What a bunch of vegetables!')
        case _:
            print('Probably an Aliens\' diet!')
    
    

    If a user selects any of rice or potatoes or corn then 'We are rich in carbohydrates, and you can spend it!' is printed. Conversely, the same code runs if the user chooses carrot, cabbage, or lettuce.

    루프



    Using loops, we can repeat specific parts of our code. Or we can go through sequences or dictionaries one item at a time. : The for and the while loop are types of loops in python.

    For 루프




    The for statement allows us to go through the elements of a collection (such as list, tuples, dictionary, string) one after the other (iteration). It follows the following pattern:
    for element in collection:
        # do something with the element
    

    The element variable represents each item in the collection during iteration. Note that the element can take any name. Let's see some examples.

    We start by creating four variables, my_list , a list of numbers. A list containing tuples list_of_tuples , menu a dictionary, and name a string.

    
    my_list = [1, 2, 3, 4, 5]
    
    list_of_tuples = [(1, 2), (3, 4), (9, 10), (11, 12)]
    
    menu = { "rice": 45, "apples": 33, "garlic": 20, "pepper": 10 }
    
    name = "John Doe"
    

    Let's print two statements using the my_list elements.

    for num in my_list:
        print("The number is {}".format(num))
        print(f"num times 2 is {num * 2}\n") # notice the \n
    
    The number is 1
    num times 2 is 2
    
    The number is 2
    num times 2 is 4
    
    The number is 3
    num times 2 is 6
    
    The number is 4
    num times 2 is 8
    
    The number is 5
    num times 2 is 10
    

    \n is called a newline character. The pattern f"{num}" is equivalent to "{}".format(num).

    Using tuple unpacking, we can retrieve the elements of the tuple in the list within the iteration.

    for first, second in list_of_tuples:
        print(f"First is {first}, second is {second}")
    
    First is 1, second is 2
    First is 3, second is 4
    First is 9, second is 10
    First is 11, second is 12
    

    First run: first, second = (1, 2). Second run: first, second = (3, 4). Third run: first, second = (9, 10). Fourth run: first, second = (11, 12).

    We get a series of letters when we loop through a string. For example:

    for letter in name:
        print(letter)
    
    J
    o
    h
    n
    
    D
    o
    e
    

    사전을 통한 반복



    사전을 반복하는 것은 그렇게 간단하지 않습니다. keys() 메서드를 사용하여 사전 키를 반복하거나 values() 메서드를 사용하여 값을 사용하거나 둘 다( items() ) 반복할 수 있습니다.

    # Loop through the keys only
    for key in menu.keys():
        print(key)
    



    rice
    apples
    garlic
    pepper
    
    keys() 메서드를 생략해도 같은 결과가 나옵니다.

    # Same as above
    for key in menu:
        print(key)
    



    rice
    apples
    garlic
    pepper
    
    values() 메서드를 사용하여 값만 검색합니다.

    # Loop through the values only
    for value in menu.values():
        print(value)
    



    45
    33
    20
    10
    
    items() 메소드를 사용하여 키와 값을 검색하십시오!

    # loop through the keys and values
    for key, value in menu.items():
        print("{}: {}".format(key, value))
    



    rice: 45
    apples: 33
    garlic: 20
    pepper: 10
    

    동안 루프



    The template is:

    while condition_is_true:
        # keep doing whatever we put here
    

    Let's say we want to print out some numbers 1 to 5. We could set a count variable and increment it within a loop.

    count = 1
    
    while count <= 5:
        print(count)
        count += 1  # increment the count
    
    1
    2
    3
    4
    5
    

    To avoid an infinite loop, the while condition must evaluate as False at some point. We increment the count variable count += 1 to ensure that the count becomes greater than five eventually.

    Operators like += -= \= *= allows us to perform arithmetics and assignment in a single step. For example,
    a = a + 1 can be written as a += 1. So also a = a * 7 can be replaced with a *= 7 and so on.

    Note that just like the if statement, loops can be nested.

    Give me all even numbers between 1 and 20:

    # we're starting from zero
    count = 0
    
    while count <= 20:
        if count % 2 == 0:
            print(count)
        count += 1  # increment the count
    
    0
    2
    4
    6
    8
    10
    12
    14
    16
    18
    20
    

    중단 및 계속 문



    The break statement helps us control a loop's termination. The continue statement skips an iteration step.

    In the example below, we want to print the multiples of 5, specifically from 5 to 20. We set the counter to 5 and then increment by 5 . When the counter becomes 20 , we exit the while loop using a break statement.

    counter = 5
    
    while True:
        print(f'counter is {counter}')
        if counter == 20:
            print("**** stopping here ****")
            break
        counter += 5
    
    counter is 5
    counter is 10
    counter is 15
    counter is 20
    **** stopping here ****
    

    To demonstrate the continue statement, create a numbers list like the one below.

    numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
    

    If we want the odd numbers between 1 and 11, we will write:

    
    for n in numbers:
        if n % 2 == 0:
            continue # skip even numbers
        print(n)
        if n == 11:
            break  # stop at 11
    
    1
    3
    5
    7
    9
    11
    

    In the code above, we skipped the even numbers with continue and then exited with break when the current number n was 11 ,

    How do I choose between a for and a while loop? It largely depends on the situation. If you're dealing with a collection, a for loop is preferable most of the time. In other cases, consider a while loop.

    통과 진술서



    주석( # )과 마찬가지로 pass 문은 파이썬에게 프로그램의 일부를 무시하도록 지시합니다. 그러나 pass 문은 코드 블록을 무시하는 데 탁월합니다. 이 코드 블록은 if-elif-else, 루프 블록 또는 함수 또는 클래스 블록일 수 있습니다. 예를 들어:

    
    # I'm still thinking of what to do
    # so I'll just use a pass so python won't complain
    num_years = 45
    
    if num_years > 80:
        pass
    else:
        pass
    
    
    # I'm not ready to work with num
    for num in [1, 2, 3, 4, 5]:
        pass
    
    


    Try-Except 문



    The try-except statement enables us to perform an alternative action if the intended action fails. For example, we may want to open a file, convert a string to an integer, and so on. These attempts might fail, resulting in an error that may crash our program. The try-except statement solves this problem. The format is:

    try:
        # do something that may fail
    except:
        # do something else. for example, handle the error and inform the user of your program
    

    The example below demonstrates the many uses of try-except . Converting from integers may produce errors. To guarantee the continuation, use a try-except statement.

    bad_integer_string = "aa12r"
    
    good_integer_string = "12345"
    
    try:
        good_integer = int(good_integer_string)
        print("Worked! The number is", good_integer)
    except:
        print("Failed")
    
    Worked! The number is 12345
    
    try:
        bad_integer = int(bad_integer_string)
        print("Worked??? The number is", bad_integer)
    except:
        print("Failed!!!")
    
    Failed!!!

    결론



    이 기사에서는 if-elif-statements 문, match 문, for 및 while 루프, pass 문 등을 사용하여 프로그램 실행을 제어하는 ​​방법을 살펴보았습니다. 다음으로 몇 가지 프로그램을 작성합니다. 읽어 주셔서 감사합니다!

    좋은 웹페이지 즐겨찾기