1. ๋ฒ”์œ„  range

 

๐Ÿ‘พ  ํŠน์ •ํ•œ ํšŸ์ˆ˜๋งŒํผ ๋ฐ˜๋ณตํ•ด์„œ ๋Œ๋ฆฌ๊ณ  ์‹ถ์„ ๋•Œ for ๋ฐ˜๋ณต๋ฌธ๊ณผ ๋ฒ”์œ„๋ฅผ ์กฐํ•ฉํ•ด์„œ ์‚ฌ์šฉํ•œ๋‹ค.

๐Ÿ‘พ  ์ •์ˆ˜ ๋ฒ”์œ„๋ฅผ ๋งŒ๋“ค์–ด ๋‚ผ ๋•Œ ์œ ์šฉํ•œ ํ•จ์ˆ˜ 

# ๊ธฐ๋ณธ๊ตฌ์กฐ
range(์ดˆ๊นƒ๊ฐ’, ์ข…๋ฃŒ๊ฐ’, ์ฆ๊ฐ๊ฐ’)

 

  โšก๏ธ  ํŠน์ง•   1. ์ดˆ๊นƒ๊ฐ’๋ถ€ํ„ฐ ์ข…๋ฃŒ๊ฐ’ ์ „๊นŒ์ง€ ์ˆซ์ž(n)๋“ค์˜ ์ปฌ๋ ‰์…˜์„ ๋งŒ๋“ฆ (์ดˆ๊นƒ๊ฐ’ <= n < ์ข…๋ฃŒ๊ฐ’)
                   2. ์ดˆ๊นƒ๊ฐ’์„ ์ƒ๋žตํ•˜๋ฉด 0๋ถ€ํ„ฐ ์‹œ์ž‘
                   3. ์ข…๋ฃŒ๊ฐ’์€ ์ƒ๋žตํ•  ์ˆ˜ ์—†์Œ
                   4. ์ฆ๊ฐ๊ฐ’์„ ์ƒ๋žตํ•˜๋ฉด 1์”ฉ ์ฆ๊ฐ€

# ์ข…๋ฃŒ๊ฐ’๋งŒ ์กด์žฌ
range(5) : 0, 1, 2, 3, 4
# ์ดˆ๊นƒ๊ฐ’, ์ข…๋ฃŒ๊ฐ’๋งŒ ์กด์žฌ
range(1, 11) : 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
# ์ดˆ๊นƒ๊ฐ’, ์ข…๋ฃŒ๊ฐ’, ์ฆ๊ฐ๊ฐ’ ๋‹ค ์กด์žฌ
range(1, 10, 2) : 1, 3, 5, 7, 9

# ๋ฆฌ์ŠคํŠธ์™€ ํŠœํ”Œ
print(list(range(1, 6)))  # [1, 2, 3, 4, 5]
print(tuple(range(1, 6)))  # (1, 2, 3, 4, 5)

 


๐Ÿš€  ์ˆ˜์‹์— ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•œ ๊ฒฝ์šฐ

 

  - range() ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ˜๋“œ์‹œ '์ •์ˆ˜'๋ฅผ ์ž…๋ ฅํ•ด์•ผ ํ•จ

n = 10
a = range(0, n/2)
# -> ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋‚˜๋ˆ—์…ˆ์„ ์‚ฌ์šฉํ•œ ๊ฒฝ์šฐ 'TypeError'๋ฐœ์ƒ
a = range(0, n//2)
# -> ๋ชซ์€ ์ •์ˆ˜๋กœ ๋‚˜์˜ค๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ€๋Šฅ

 


 

2.  for ๋ฐ˜๋ณต๋ฌธ: ๋ฒ”์œ„์™€ ํ•จ๊ป˜ ์‚ฌ์šฉ

# ๊ธฐ๋ณธ ํ˜•์‹
for ์ˆซ์ž ๋ณ€์ˆ˜ in ๋ฒ”์œ„:
    ์ฝ”๋“œ
# for ๋ฐ˜๋ณต๋ฌธ
for n in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:
    print(n)

# range() ์‚ฌ์šฉํ•œ for ๋ฐ˜๋ณต๋ฌธ
for n in range(1, 11):
    print(n)

# range() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ์ƒ์„ฑํ•œ ๊ฐ’์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ
for n in range(10):  # 10๋ฒˆ ๋ฐ˜๋ณต
    print('Hello')

 


 

๐Ÿš€  ๋ฆฌ์ŠคํŠธ์™€ ๋ฒ”์œ„ ์กฐํ•ฉ

# ๋ฆฌ์ŠคํŠธ ์„ ์–ธ
array = [270, 33, 100, 55, 11]

# ๋ฐ˜๋ณต๋ฌธ์— ์ ์šฉ
for i in range(len(array)):
    print("{}๋ฒˆ์งธ ๋ฐ˜๋ณต: {}".format(i, array[i])

'''
์‹คํ–‰๊ฒฐ๊ณผ)
0๋ฒˆ์งธ ๋ฐ˜๋ณต: 270
1๋ฒˆ์งธ ๋ฐ˜๋ณต: 33
2๋ฒˆ์งธ ๋ฐ˜๋ณต: 100
3๋ฒˆ์งธ ๋ฐ˜๋ณต: 55
4๋ฒˆ์งธ ๋ฐ˜๋ณต: 11
'''

 


 

3. for ๋ฐ˜๋ณต๋ฌธ: ๋ฐ˜๋Œ€๋กœ ๋ฐ˜๋ณตํ•˜๊ธฐ

 

1) range() ํ•จ์ˆ˜ ๋งค๊ฐœ๋ณ€์ˆ˜ ์‚ฌ์šฉ

for i in range(4, 0-1, -1):
    print("ํ˜„์žฌ ๋ฐ˜๋ณต ๋ณ€์ˆ˜: {}".format(i))

'''
์‹คํ–‰๊ฒฐ๊ณผ)
ํ˜„์žฌ ๋ฐ˜๋ณต ๋ณ€์ˆ˜: 4
ํ˜„์žฌ ๋ฐ˜๋ณต ๋ณ€์ˆ˜: 3
ํ˜„์žฌ ๋ฐ˜๋ณต ๋ณ€์ˆ˜: 2
ํ˜„์žฌ ๋ฐ˜๋ณต ๋ณ€์ˆ˜: 1
ํ˜„์žฌ ๋ฐ˜๋ณต ๋ณ€์ˆ˜: 0
'''

 

 

2) reversed() ํ•จ์ˆ˜ ์‚ฌ์šฉ

for i in reversed(range(5)):
    print("ํ˜„์žฌ ๋ฐ˜๋ณต ๋ณ€์ˆ˜: {}".format(i))
    
'''
์‹คํ–‰๊ฒฐ๊ณผ)
ํ˜„์žฌ ๋ฐ˜๋ณต ๋ณ€์ˆ˜: 4
ํ˜„์žฌ ๋ฐ˜๋ณต ๋ณ€์ˆ˜: 3
ํ˜„์žฌ ๋ฐ˜๋ณต ๋ณ€์ˆ˜: 2
ํ˜„์žฌ ๋ฐ˜๋ณต ๋ณ€์ˆ˜: 1
ํ˜„์žฌ ๋ฐ˜๋ณต ๋ณ€์ˆ˜: 0
'''

 


 

4. ์ค‘์ฒฉ ๋ฐ˜๋ณต๋ฌธ

output = ""

for i in range(1,10):
    for j in range(0,i):
        output += "*"
    output += "\n"
    
print(output)

 

 

 

  โšก๏ธ  ์™ธ๋ถ€์˜ ๋ฐ˜๋ณต๋ฌธ: ์ค„์ƒ์„ฑ(\n)

  โšก๏ธ  ๋‚ด๋ถ€์˜ ๋ฐ˜๋ณต๋ฌธ: ๋ณ„์ƒ์„ฑ(*)

 

 

 

 

 

 

 

output = ""

for i in range(1, 10):
    for j in range(9, i, -1):
        output += ' '
    for k in range(0, 2*i-1):
        output += '*'
    output += '\n'
   
print(output)

 

 

 

  โšก๏ธ  ์™ธ๋ถ€์˜ ๋ฐ˜๋ณต๋ฌธ: ์ค„์ƒ์„ฑ(\n)

  โšก๏ธ  ๋‚ด๋ถ€์˜ ๋ฐ˜๋ณต๋ฌธ 1 : ๊ณต๋ฐฑ์ƒ์„ฑ

 โšก๏ธ  ๋‚ด๋ถ€์˜ ๋ฐ˜๋ณต๋ฌธ 2 : ๋ณ„์ƒ์„ฑ(*)

 

 

 

 

 

 

 

 

 

[ ๋‚ด์šฉ์ฐธ๊ณ  : IT ํ•™์› ๊ฐ•์˜ ์ž๋ฃŒ ๋ฐ  ์ฑ… 'ํ˜ผ์ž ๊ณต๋ถ€ํ•˜๋Š” ํŒŒ์ด์ฌ' ]

+ Recent posts