본문 바로가기
개발/Python

[Python] Comprehensive Guide to Using "for" Loops

by 꾀돌이 개발자 2024. 7. 24.
반응형

 

Using "for" Loops in Python

 

 

 
 

Table of Contents

     

    * Code is basically composed of <input # output> format.

     

    Basic Form (1): for in

    for variable in object:
        statement
    • Here, the object typically refers to a string, list, tuple, or dictionary.
    • The variable is sequentially assigned values from the first to the last index of the object.
    • for x in 'abc': Here, x starts with 'a' and is subsequently assigned 'b' and 'c'.
    • for x in [1, 2, 3]: Here, x starts with 1 and is subsequently assigned 2 and 3.
    • for x in {'one': 1, 'two': 2, 'three': 3}: Here, x starts with 'one' and is subsequently assigned 'two' and 'three'.
    • for [x, y] in [[1, 2], [3, 4], [5, 6]]: Here, x and y start with 1 and 2, and are subsequently assigned 3 and 4, then 5 and 6 respectively.
    for x in 'abe':
    	print(x)
    
    # output : a / b / c
    
    for x in [1, 2, 3]:
    	print(x)
    
    # output : 1 / 2 / 3
    
    for x in {'one':1, 'two':2, 'three':3}:
    	print(x)
    
    # output : one / two / three
    
    for [x, y] in [[1, 2], [3, 4], [5, 6]]:
    	print(x, y)
        
    # output : 1 2 / 3 4 / 5 6

     

    Basic Form (2): for in range

    for variable in range(start, end, step):
        statement
    • The variable increments from (start) to (end-1) by (step), executing the statement each time.
    • for i in range(0, 10, 1): Here, i starts from 0 and increments by 1 up to 9, executing the statement a total of 10 times.
    • for variable in range(value1): If range() has one argument, it means end=value1, start=0, and step=1.
    • for variable in range(value1, value2): If range() has two arguments, it means start=value1, end=value2, and step=1.
    • You can omit the step, or omit both the start and step.
    for i in range(0, 10, 2):
    	print(i)
    
    # output : 0 / 2 / 4 / 6 / 8
    
    for i in range(0, 10):
    	print(i)
    
    # output : 0 / 1 / 2 / 3 / 4 / 5 / 6 / 7 / 8 / 9
    
    for i in range(10):
    	print(i)
    
    # output : 0 / 1 / 2 / 3 / 4 / 5 / 6 / 7 / 8 / 9

     

    Basic Form (3): Nested "for" Loops

    • You can include multiple "for" loops within a "for" loop.
    for i in range(2):
    	for j in range(2):
    		for k in range(2):
    			print(i, j, k)
    
    # output : 0 0 0 / 0 0 1 / 0 1 0 / 0 1 1 / 1 0 0 / 1 0 1 / 1 1 0 / 1 1 1

     

    Functions Related to "for": continue, break

    • continue: The statements following continue are not executed.
    • break: Terminates the "for" loop.
    • Generally, continue skips a particular moment in the "for" loop, while break cancels the remaining iterations of the "for" loop entirely.
    for i in range(3):
    	if i == 1:
        	continue
    	print(i)
        
    # output : 0 / 2
    
    for i in range(10):
    	if i == 3:
        	break
    	print(i)
        
    # output : 0 / 1 / 2

     

    Functions Related to "for": else

    • else: Executed after the "for" loop completes normally. It is not executed if the "for" loop is terminated by a break statement.
    for x in [1, 2, 3]:
    	print(x)
    else:
    	print('end')
    
    # output : 1 / 2 / 3 / end
    
    for x in [1, 2, 3]:
    	if x == 2:
        	break
    	print(x)
    else:
    	print('end')
        
    # output : 1

     

    List Comprehension

    [expression for variable1 in object1 if condition1
                 for variable2 in object2 if condition2
                 ......
                 for variablen in objectn if conditionn]
    • As shown above, [for variable in object if condition] can consist of two or more clauses.
    • The if condition part is optional.
    a = 'abcde'
    b = [val + 'k' for val in a]
    print(b)
    
    # output : ['ak', 'bk', 'ck', 'dk', 'ek']
    
    a = [1, 2, 3, 4, 5]
    b = [val * 5 for val in a]
    print(b)
    
    # output : [5, 10, 15, 20, 25]
    
    a = 'abcde'
    b = [val + 'k' for val in a if val == 'c']
    print(b)
    
    # output : ['ck']
    
    a = [1, 2, 3, 4, 5]
    b = [val * 5 for val in a if val < 3]
    print(b)
    
    # output : [5, 10]
    
    a1 = [1, 2, 3]
    a2 = [6, 7 ,8]
    b = [val1 + val2 for val1 in a1 if val1 < 3
    		 for val2 in a2 if val2 < 7]
    print(b)
    
    # output : [7, 8]
    # The order of (val1, val2) is (1,6), (1,7), (1,8), (2,6), (2,7), (2,8), (3,6), (3,7), (3,8), 
    # and only (1,6) and (2,6) are selected according to each condition.

     

    반응형