๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Study_note(zb_data)/Python

์Šคํ„ฐ๋”” ๋…ธํŠธ (์ž๋ฃŒ๊ตฌ์กฐ 01~26)_0719

๐Ÿ“Œ ์ž๋ฃŒ๊ตฌ์กฐ๋ž€?

์ถœ์ฒ˜ : ์ œ๋กœ๋ฒ ์ด์Šค ๋ฐ์ดํ„ฐ ์Šค์ฟจ

  • List, Tuple, Dic, Set ์œผ๋กœ ๊ตฌ๋ถ„๋˜์–ด์ง„๋‹ค.
    • List - ์•ˆ์— ๋ฐ์ดํ„ฐ๋ฅผ ์–ธ์ œ๋“ ์ง€ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋‹ค.
    • Tuple : ๋ฐ์ดํ„ฐ๊ฐ€ ์ •ํ•ด์ง€๋ฉด ๋ฐ”๊ฟ€ ์ˆ˜ ์—†๋‹ค.
    • Dic : key / value๋กœ ์ž๋ฃŒ๊ตฌ์กฐ๊ฐ€ ์ด๋ค„์ ธ ์žˆ๋‹ค.
    • Set : ์ค‘๋ณต๋œ ๋ฐ์ดํ„ฐ๊ฐ€ ํ—ˆ์šฉ๋˜์ง€ ์•Š๋Š”๋‹ค.

๐Ÿ“Œ ๋ฆฌ์ŠคํŠธ๋ž€? (List)

์ถœ์ฒ˜ : ์ œ๋กœ๋ฒ ์ด์Šค ๋ฐ์ดํ„ฐ ์Šค์ฟจ

  • []๋ฅผ ์ด์šฉํ•˜์—ฌ ์„ ์–ธํ•˜๊ณ , ๋ฐ์ดํ„ฐ ๊ตฌ๋ถ„์€ ','๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
  • ๋ฆฌ์ŠคํŠธ์— ๋˜ ๋‹ค๋ฅธ ์ปจํ…Œ์ด๋„ˆ ์ž๋ฃŒํ˜• ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ“Œ ์ธ๋ฑ์Šค (Index)

  • ์•„์ดํ…œ์— ์ž๋™์œผ๋กœ ๋ถ€์—ฌ๋˜๋Š” ๋ฒˆํ˜ธํ‘œ
    • ์ˆœ์„œ๋Š” 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•œ๋‹ค.
    • ๋ฆฌ์ŠคํŠธ ์•„์ดํ…œ์€ ์ธ๋ฑ์Šค๋ฅผ ์ด์šฉํ•˜์—ฌ ์กฐํšŒ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.

๐Ÿ“Œ ์•„์ดํ…œ ๊ฐœ์ˆ˜

  • ๋ฆฌ์ŠคํŠธ ๊ธธ์ด = ๋ฆฌ์ŠคํŠธ์— ์ €์žฅ๋œ ์•„์ดํ…œ ๊ฐœ์ˆ˜
  • len()๊ณผ ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ, ์•„์ดํ…œ ์กฐํšŒ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.
myFavoriteSports = ['์ˆ˜์˜', '๋ฐฐ๊ตฌ', '์•ผ๊ตฌ', '์กฐ๊น…']

for i in range(4):
    print(f'myFavoriteSports[{i}] : {myFavoriteSports[i]}')

print()

for i in range(len(myFavoriteSports)):
    print(f'myFavoriteSports[{i}] : {myFavoriteSports[i]}')

print()

for item in myFavoriteSports:
    print(item)
    
>>
myFavoriteSports[0] : ์ˆ˜์˜
myFavoriteSports[1] : ๋ฐฐ๊ตฌ
myFavoriteSports[2] : ์•ผ๊ตฌ
myFavoriteSports[3] : ์กฐ๊น…

myFavoriteSports[0] : ์ˆ˜์˜
myFavoriteSports[1] : ๋ฐฐ๊ตฌ
myFavoriteSports[2] : ์•ผ๊ตฌ
myFavoriteSports[3] : ์กฐ๊น…

์ˆ˜์˜
๋ฐฐ๊ตฌ
์•ผ๊ตฌ
์กฐ๊น…

๐Ÿ“Œ ๋ฆฌ์ŠคํŠธ์™€ for๋ฌธ

  • for๋ฌธ์„ ์ด์šฉํ•˜๋ฉด, ๋ฆฌ์ŠคํŠธ์˜ ์•„์ดํ…œ์„ ์ž๋™์œผ๋กœ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋‹ค.
studentsCnts = [[1, 19], [2, 20], [3, 22], [4, 18], [5, 21]]

for i in range(len(studentsCnts)):
    print('{}ํ•™๊ธ‰ ํ•™์ƒ ์ˆ˜ : {}'.format(studentsCnts[i][0], studentsCnts[i][1])) #ํ—ท๊ฐˆ๋ฆฌ๊ธฐ ์‰ฌ์›€!
print()
for classNo, cnt in studentsCnts:
    print('{}ํ•™๊ธ‰ ํ•™์ƒ ์ˆ˜ : {}'.format(classNo, cnt))
studentsCnts = [[1, 18], [2, 19], [3, 23], [4, 21], [5, 20], [6, 22], [7, 17]]

sum = 0

for classNo, cnt in studentsCnts:
    print('{}ํ•™๊ธ‰ ํ•™์ƒ ์ˆ˜ : {}'.format(classNo, cnt))
    sum += cnt

print('์ „์ฒด ํ•™์ƒ ์ˆ˜ : {}'.format(sum))
print('ํ‰๊ท  ํ•™์ƒ ์ˆ˜ : {}'.format(int(sum / len(studentsCnts))))

๐Ÿ“Œ Item ํ‚ค์›Œ๋“œ์™€ List

minScore = 60
scores = [['๊ตญ์–ด',58],
          ['์˜์–ด',77],
          ['์ˆ˜ํ•™',89],
          ['๊ณผํ•™',99],
          ['๊ตญ์‚ฌ',50]]

for item in scores:
    if item[1] < minScore:
        print('๊ณผ๋ฝ ๊ณผ๋ชฉ : {}, ์ ์ˆ˜ {}'.format(item[0], item[1]))
print()
for subject, score in scores:
    if score < minScore:
        print('๊ณผ๋ฝ ๊ณผ๋ชฉ : {}, ์ ์ˆ˜ {}'.format(subject, score))
print()
for subject, score in scores:
    if score >= minScore: continue
    print('๊ณผ๋ฝ ๊ณผ๋ชฉ : {}, ์ ์ˆ˜ {}'.format(subject, score))
studentCnts = [[1, 18], [2, 19], [3, 23], [4, 21], [5, 20], [6, 22], [7, 17]]
minclassNo = 0; maxclassNo = 0
minCnt = 0; maxCnt = 0
for classNo, cnt in studentCnts:
    if minCnt == 0 or minCnt > cnt:
        minclassNo = classNo
        minCnt = cnt

    if maxCnt < cnt:
        maxclassNo = classNo
        maxCnt = cnt

print('ํ•™์ƒ ์ˆ˜๊ฐ€ ๊ฐ€์žฅ ์ ์€ ํ•™๊ธ‰(ํ•™์ƒ์ˆ˜): {}ํ•™๊ธ‰({}๋ช…)'.format(minclassNo, minCnt))
print('ํ•™์ƒ ์ˆ˜๊ฐ€ ๊ฐ€์žฅ ๋งŽ์€ ํ•™๊ธ‰(ํ•™์ƒ์ˆ˜): {}ํ•™๊ธ‰({}๋ช…)'.format(maxclassNo, maxCnt))

๐Ÿ“Œ enumerate() 

sports = ['๋†๊ตฌ', '์ˆ˜๊ตฌ', '์ถ•๊ตฌ', '๋งˆ๋ผํ†ค', 'ํ…Œ๋‹ˆ์Šค']

for i in range(len(sports)):
    print('{} : {}'.format(i,sports[i]))
print()
for idx, value in enumerate(sports): #idx, value๋กœ ๊ฐ’์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.
    print('{} : {}'.format(idx,value))
>>>>>>>>
0 : ๋†๊ตฌ
1 : ์ˆ˜๊ตฌ
2 : ์ถ•๊ตฌ
3 : ๋งˆ๋ผํ†ค
4 : ํ…Œ๋‹ˆ์Šค

0 : ๋†๊ตฌ
1 : ์ˆ˜๊ตฌ
2 : ์ถ•๊ตฌ
3 : ๋งˆ๋ผํ†ค
4 : ํ…Œ๋‹ˆ์Šค

๐Ÿ“Œ insert()

  • ํŠน์ • ์œ„์น˜(index)์— ์•„์ดํ…œ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.
numbers = [1, 3, 6, 11, 45, 54, 62, 74, 85]
inputNumber = int(input('์ˆซ์ž ์ž…๋ ฅ : '))
insertIdx = 0

for idx, number in enumerate(numbers):
    print(idx, number) #์•„์ดํ…œ์„ ์ˆœ์ฐจ์ ์œผ๋กœ ์ถœ๋ ฅํ•œ๋‹ค.

    if insertIdx == 0 and inputNumber < number: #ํ•ด๋‹น ์ธ๋ฑ์Šค์˜ ์ˆซ์ž๊ฐ’์ด ์ž…๋ ฅํ•œ ์ˆซ์ž๋ณด๋‹ค ํฌ๋‹ค๋ฉด
        insertIdx = idx #insertIdx๋Š” ํ•ด๋‹น ์ธ๋ฑ์Šค ๊ฐ’์ด ๋œ๋‹ค.

numbers.insert(insertIdx, inputNumber)
print(numbers)
>>
์ˆซ์ž ์ž…๋ ฅ : 30
0 1
1 3
2 6
3 11
4 45
5 54
6 62
7 74
8 85
[1, 3, 6, 11, 30, 45, 54, 62, 74, 85]

๐Ÿ“Œ pop()

  • pop ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ์ธ๋ฑ์Šค์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค.
playerScore = [9.5, 8.9, 9.2, 9.8, 8.8, 9.0]
print('playerScore : {}'.format(playerScore))

minScore = 0; maxScore = 0
minScoreIdx = 0; maxScoreIdx =0

for idx, score in enumerate(playerScore):

    if idx == 0 or minScore > score:
        minScoreIdx = idx
        minScore = score

print('minScore : {}, minScoreIdx : {}'.format(minScore, minScoreIdx))
playerScore.pop(minScoreIdx)
print('playerScore : {}'.format(playerScore))

for idx, score in enumerate(playerScore):

    if maxScore < score:
        maxScore = score
        maxScoreIdx = idx

print('maxScore : {}, maxScoreIdx : {}'.format(maxScore, maxScoreIdx))
playerScore.pop(maxScoreIdx)

print('playerScore : {}'.format(playerScore))
>>
playerScore : [9.5, 8.9, 9.2, 9.8, 8.8, 9.0]
minScore : 8.8, minScoreIdx : 4
playerScore : [9.5, 8.9, 9.2, 9.8, 9.0]
maxScore : 9.8, maxScoreIdx : 3
playerScore : [9.5, 8.9, 9.2, 9.0]

๐Ÿ“Œ remove()

  • remove ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ, ํŠน์ • ์•„์ดํ…œ์„ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๋‹ค.
subjects = ['๊ตญ์–ด', '์˜์–ด', '์ˆ˜ํ•™', '๊ณผํ•™', '๊ตญ์‚ฌ']
print(subjects)
removeSubject = input('remove subject : ')

while removeSubject in subjects:
    subjects.remove(removeSubject)

print(subjects)
>>
['๊ตญ์–ด', '์˜์–ด', '์ˆ˜ํ•™', '๊ณผํ•™', '๊ตญ์‚ฌ']
remove subject : ์˜์–ด
['๊ตญ์–ด', '์ˆ˜ํ•™', '๊ณผํ•™', '๊ตญ์‚ฌ']

๐Ÿ“Œ extend()

  • ๋ฆฌ์ŠคํŠธ์— ๋˜ ๋‹ค๋ฅธ ๋ฆฌ์ŠคํŠธ๋ฅผ ์—ฐ๊ฒฐ(ํ™•์žฅ)ํ•  ์ˆ˜ ์žˆ๋‹ค.
    • A + B = A์— ๋”ํ•ด์ง„๋‹ค (extend)
  • ๋ง์…ˆ ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ๋ฅผ ์—ฐ๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค. 
    • A + B = C, ์ƒˆ๋กœ์šด ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋งŒ๋“ค์–ด์ง„๋‹ค.
myFavoriteNumber = [1, 3, 5, 6, 7]
friendFavoriteNumber = [2, 3, 5, 8, 10]

print('myFavoriteNumber : {}'.format(myFavoriteNumber))
print('friendFavoriteNumber : {}'.format(friendFavoriteNumber))

addList = myFavoriteNumber + friendFavoriteNumber #๋ง์…ˆ ์—ฐ์‚ฐ์ž ํ™œ์šฉ

print('addList : {}'.format(addList))

result = []

for number in addList: #addList์— ์žˆ๋Š” ๊ฐ’์ด
    if number not in result: #๋งŒ์•ฝ result ๋ฆฌ์ŠคํŠธ์— ์—†๋‹ค๋ฉด
        result.append(number) #result ๋ฆฌ์ŠคํŠธ์— ๋ฐ์ดํ„ฐ๋ฅผ appendํ•œ๋‹ค.

result.sort()

print('result : {}'.format(result))
>>>
myFavoriteNumber : [1, 3, 5, 6, 7]
friendFavoriteNumber : [2, 3, 5, 8, 10]
addList : [1, 3, 5, 6, 7, 2, 3, 5, 8, 10]
result : [1, 2, 3, 5, 6, 7, 8, 10]

๐Ÿ“Œ sort()

  • ์•„์ดํ…œ์„ ์ •๋ ฌํ•  ์ˆ˜ ์žˆ๋‹ค. (๊ธฐ๋ณธ์ ์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ)
    • sort(reverse = True) ๋Š” ๋‚ด๋ฆผ์ฐจ์ˆœ
scores = [9.5, 8.9, 9.2, 9.8, 8.8, 9.0]
print('playerScore : {}'.format(scores))
scores.sort() #scores ๋ฐ์ดํ„ฐ ๊ฐ’ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
print('playerScore : {}'.format(scores))
scores.pop(0) #๋งจ ์ฒ˜์Œ ์ธ๋ฑ์Šค (์ตœ์†Œ๊ฐ’) ๋ฐ˜ํ™˜
scores.pop(len(scores)-1) #๋งจ ๋’ท์ž๋ฆฌ ์ธ๋ฑ์Šค (์ตœ๋Œ“๊ฐ’) ๋ฐ˜ํ™˜
print('playerScore : {}'.format(scores))

sum = 0
avg = 0

for score in scores:
    sum += score

avg = round((sum / len(scores)), 2)

print('์ด์  : %.2f' % sum)

print('ํ‰๊ท  : %.2f' % avg)
>>
playerScore : [9.5, 8.9, 9.2, 9.8, 8.8, 9.0]
playerScore : [8.8, 8.9, 9.0, 9.2, 9.5, 9.8]
playerScore : [8.9, 9.0, 9.2, 9.5]
์ด์  : 36.60
ํ‰๊ท  : 9.15

๐Ÿ“Œ reverse()

secret = '27156231'
secretList = []
solvedsecret = ''

for cha in secret:
    secretList.append(int(cha))
secretList.reverse() 
print('secretList = {}'.format(secretList)) 

val = secretList[0] * secretList[1]
secretList.insert(2, val)
print('secretList = {}'.format(secretList))

val = secretList[3] * secretList[4]
secretList.insert(5, val)
print('secretList = {}'.format(secretList))

val = secretList[6] * secretList[7]
secretList.insert(8, val)
print('secretList = {}'.format(secretList))

val = secretList[9] * secretList[10]
secretList.insert(11, val)
print('secretList = {}'.format(secretList))
>>
secretList = [1, 3, 2, 6, 5, 1, 7, 2]
secretList = [1, 3, 3, 2, 6, 5, 1, 7, 2]
secretList = [1, 3, 3, 2, 6, 12, 5, 1, 7, 2]
secretList = [1, 3, 3, 2, 6, 12, 5, 1, 5, 7, 2]
secretList = [1, 3, 3, 2, 6, 12, 5, 1, 5, 7, 2, 14]

๐Ÿ“Œ ๋ฆฌ์ŠคํŠธ ์Šฌ๋ผ์ด์‹ฑ

  • [n:m]์„ ์ด์šฉ, ๋ฆฌ์ŠคํŠธ์—์„œ ์›ํ•˜๋Š” ์•„์ดํ…œ์„ ๋ฝ‘์„ ์ˆ˜ ์žˆ๋‹ค. 
  • [slice(n, m)] ์œผ๋กœ๋„ ๊ฐ€๋Šฅ

์ถœ์ฒ˜ : ์ œ๋กœ๋ฒ ์ด์Šค ๋ฐ์ดํ„ฐ ์Šค์ฟจ
์ถœ์ฒ˜ : ์ œ๋กœ๋ฒ ์ด์Šค ๋ฐ์ดํ„ฐ ์Šค์ฟจ

๐Ÿ“Œ ๋ฆฌ์ŠคํŠธ์˜ ๊ทธ ์™ธ ๊ธฐ๋Šฅ

  • ๋ฆฌ์ŠคํŠธ๋Š” ๊ณฑ์…ˆ ์—ฐ์‚ฐ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
  • index ํ•จ์ˆ˜๋กœ ๋ฆฌ์ŠคํŠธ์˜ ์ธ๋ฑ์Šค๋ฅผ ์•Œ ์ˆ˜ ์žˆ๋‹ค.
import random

lucNum = int(input('Where is lucky number 7? : '))

sampleList = random.sample(range(1, 11), 10)
sampleIdx = sampleList.index(7)

if sampleIdx == lucNum:
    print('bingo!')
else:
    print('false')

print('sampleList = {}'.format(sampleList))
print('sampleIdx = {}'.format(sampleIdx))
>>>>
Where is lucky number 7? : 6
false
sampleList = [8, 6, 10, 4, 9, 5, 3, 7, 2, 1]
sampleIdx = 7

๐Ÿ“Œ count()

  • ๋ฆฌ์ŠคํŠธ ๋‚ด์˜ ํŠน์ • ์•„์ดํ…œ ๊ฐœ์ˆ˜๋ฅผ ์•Œ์•„๋‚ผ ์ˆ˜ ์žˆ๋‹ค.
import random

types = ['A', 'B', 'AB', 'O'] #ํ˜ˆ์•กํ˜• ์œ ํ˜•์„ ๋ฆฌ์ŠคํŠธ์— ์ €์žฅ
todayData = [] 
typeCnt = []

for i in range(100): 
    type = types[random.randrange(len(types))] #๋ฆฌ์ŠคํŠธ์— ์ €์žฅ๋œ ํ˜ˆ์•กํ˜• ์œ ํ˜•์„ ๋žœ๋ค์œผ๋กœ 100๊ฐœ ์ƒ์‚ฐ
    todayData.append(type) #todayData์— append

print('todayData : {}'.format(todayData))
print('todayDataLength : {}'.format(len(todayData)))

for j in types: #types ๋ฆฌ์ŠคํŠธ์˜ ๋ฐ์ดํ„ฐ ๊ฐ’์„ j์— ํ• ๋‹น
    print('{}ํ˜•, {}๊ฐœ'.format(j, todayData.count(j)))
    
>>
todayData : ['B', 'O', 'A', 'AB', 'B', 'AB', 'O', 'A', 'A', 'O', 'B', 'AB', 'A', 'O', 'B', 'O', 'A', 'AB', 'O', 'O', 'AB', 'B', 'A', 'O', 'A', 'A', 'A', 'O', 'B', 'A', 'A', 'A', 'A', 'B', 'B', 'O', 'A', 'B', 'AB', 'AB', 'O', 'AB', 'AB', 'A', 'O', 'AB', 'AB', 'B', 'A', 'AB', 'A', 'B', 'B', 'AB', 'A', 'O', 'B', 'A', 'AB', 'A', 'A', 'AB', 'A', 'A', 'AB', 'A', 'A', 'O', 'A', 'AB', 'A', 'AB', 'O', 'O', 'AB', 'A', 'AB', 'AB', 'B', 'O', 'O', 'O', 'A', 'A', 'A', 'O', 'O', 'AB', 'A', 'B', 'A', 'AB', 'B', 'B', 'O', 'AB', 'O', 'B', 'A', 'O']
todayDataLength : 100
Aํ˜•, 34๊ฐœ
Bํ˜•, 18๊ฐœ
ABํ˜•, 24๊ฐœ
Oํ˜•, 24๊ฐœ

๐Ÿ“Œ del

students = ['ํ™๊ธธ๋™','๊ฐ•ํ˜ธ๋™', '๋ฐ•์ฐฌํ˜ธ', '์ด์šฉ๊ทœ', '๋ฐ•์Šน์ฒ ', '๊ฐ•ํ˜ธ๋™', '๊น€์ง€์€']
print('students : {}'.format(students))

del students[1:4]
print('students : {}'.format(students))

>>>>>>>
students : ['ํ™๊ธธ๋™', '๊ฐ•ํ˜ธ๋™', '๋ฐ•์ฐฌํ˜ธ', '์ด์šฉ๊ทœ', '๋ฐ•์Šน์ฒ ', '๊ฐ•ํ˜ธ๋™', '๊น€์ง€์€']
students : ['ํ™๊ธธ๋™', '๋ฐ•์Šน์ฒ ', '๊ฐ•ํ˜ธ๋™', '๊น€์ง€์€']

๐Ÿ“Œ ํŠœํ”Œ์ด๋ž€? (Tuple)

์ถœ์ฒ˜ : ์ œ๋กœ๋ฒ ์ด์Šค ๋ฐ์ดํ„ฐ ์Šค์ฟจ

๐Ÿ“Œ in, not in

  • in, not in ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•˜๋ฉด ์•„์ดํ…œ์˜ ์กด์žฌ ์œ /๋ฌด๋ฅผ ์•Œ ์ˆ˜ ์žˆ๋‹ค.
student = ('ํ™๊ธธ๋™', '๋ฐ•์ฐฌํ˜ธ', '์ด์šฉ๊ทœ', '๋ฐ•์Šน์ฒ ', '๊น€์ง€์€')

studentName = input('put Name : ')

if studentName not in student:
    print('{} ํ•™์ƒ์€ ํ•™๊ธ‰์— ์—†์Šต๋‹ˆ๋‹ค.'.format(studentName))
else:
    print('{} ํ•™์ƒ์€ ํ•™๊ธ‰์— ์žˆ์Šต๋‹ˆ๋‹ค.'.format(studentName))
import random

randomNumbers = random.sample(range(1, 11), 5) #1~10๊นŒ์ง€์˜ ์ˆซ์ž ์ค‘ ๋žœ๋ค์œผ๋กœ 5๊ฐœ์˜ ์ˆซ์ž๋ฅผ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฐ˜ํ™˜

userNumber = int(input('input Number : '))

if userNumber in randomNumbers: #if 5๊ฐœ ์ˆซ์ž ๋ฆฌ์ŠคํŠธ์— ์ž…๋ ฅํ•œ ์ˆซ์ž๊ฐ€ ์žˆ๋‹ค๋ฉด
    print('bingo!')
    print('random numbers : {}'.format(randomNumbers))
    print('user number : {}'.format(userNumber))

else:
    print('fail..')
    print('random numbers : {}'.format(randomNumbers))
    print('user number : {}'.format(userNumber))
>>
input Number : 5
bingo!
random numbers : [9, 4, 5, 2, 7]
user number : 5

๐Ÿ“Œ ํŠœํ”Œ์˜ ๊ธธ์ด

playSports = ('์ˆ˜์˜', '๋ฐฐ๊ตฌ', '์•ผ๊ตฌ', '์กฐ๊น…')

for i in range(len(playSports)):
    print('myFavoriteSports[{}] : {}'.format(i, playSports[i]))

print()

for f in playSports:
    print('myFavoriteSports : {}'.format(f))

print()

n=0

while n < len(playSports):
    print('myFavoriteSports[{}] : {}'.format(n, playSports[n]))
    n += 1

 

๐Ÿ“Œ ํŠœํ”Œ์˜ ๊ฒฐํ•ฉ

  • ๋ง์…ˆ ์—ฐ์‚ฐ์ž ์ด์šฉ, ๋‘๊ฐœ์˜ Tuple์„ ๊ฒฐํ•ฉ
  • List์—์„œ ์‚ฌ์šฉํ•˜๋Š” extend ํ•จ์ˆ˜๋Š” ์‚ฌ์šฉ ๋ถˆ๊ฐ€๋Šฅ.

์ถœ์ฒ˜ : ์ œ๋กœ๋ฒ ์ด์Šค ๋ฐ์ดํ„ฐ ์Šค์ฟจ

myFavoriteNum = (1, 3, 5, 6, 7)
friendFavoriteNum = (2, 3, 5, 8, 10)

print('my Favorite Num : {}'.format(myFavoriteNum))
print('friend Favorite Num : {}'.format(friendFavoriteNum))

for number in friendFavoriteNum:
    if friendFavoriteNum not in myFavoriteNum:
        myFavoriteNum = myFavoriteNum + (number, ) #number๋Š” ์›๋ž˜ ์ •์ˆ˜ํ˜•, ํŠœํ”Œ๋กœ ๋ณ€ํ™˜ ํ›„ ๋ง์…ˆ ์—ฐ์‚ฐ์ž ํ™œ์šฉ

print('my Favorite Num : {}'.format(myFavoriteNum))
>>
my Favorite Num : (1, 3, 5, 6, 7)
friend Favorite Num : (2, 3, 5, 8, 10)
my Favorite Num : (1, 3, 5, 6, 7, 2, 3, 5, 8, 10)

๐Ÿ“Œ ๋ฆฌ์ŠคํŠธ์™€ ํŠœํ”Œ์˜ ์ฐจ์ด์ 

  • ์•„์ดํ…œ ์ถ”๊ฐ€, ๋ณ€๊ฒฝ, ์‚ญ์ œ ๋ถˆ๊ฐ€

์ถœ์ฒ˜ : ์ œ๋กœ๋ฒ ์ด์Šค ๋ฐ์ดํ„ฐ ์Šค์ฟจ

  • ์„ ์–ธ ์‹œ, ๊ด„ํ˜ธ ์ƒ๋žต์ด ๊ฐ€๋Šฅ

์ถœ์ฒ˜ : ์ œ๋กœ๋ฒ ์ด์Šค ๋ฐ์ดํ„ฐ ์Šค์ฟจ

  • ๋ฆฌ์ŠคํŠธ์™€ ํŠœํ”Œ์€ ์ž๋ฃŒํ˜• ๋ณ€ํ™˜์ด ๊ฐ€๋Šฅํ•˜๋‹ค

๐Ÿ“Œ ํŠœํ”Œ์˜ ์ •๋ ฌ

  • Tuple์€ ์ˆ˜์ •์ด ๋ถˆ๊ฐ€๋Šฅ, ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜ ํ›„ ์ •๋ ฌ
  • sorted() ํ•จ์ˆ˜๋ฅผ ์ด์šฉ, Tuple๋„ ์ •๋ ฌ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
    • ๋‹จ, sorted()๋Š” ์ •๋ ฌ ํ›„ ๋ฆฌ์ŠคํŠธ ์ž๋ฃŒํ˜•์œผ๋กœ ๋ฐ˜ํ™˜

์ถœ์ฒ˜ : ์ œ๋กœ๋ฒ ์ด์Šค ๋ฐ์ดํ„ฐ ์Šค์ฟจ

numbers = (2, 50, 0.12, 1, 9, 7, 17, 35, 100, 3.14)

numbers = sorted(numbers)
print(numbers)
>
[0.12, 1, 2, 3.14, 7, 9, 17, 35, 50, 100]
scores = (9.5, 8.9, 9.2, 9.8, 8.8, 9.0)
print(scores) #Tuple ํ˜•์œผ๋กœ ์ถœ๋ ฅ
scores = sorted(scores) #sorted ํ•จ์ˆ˜๋กœ ์ •๋ ฌ ํ›„ ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜, ํŠœํ”Œ์€ ๋ฐ์ดํ„ฐ ์ˆ˜์ •์ด ๋ถˆ๊ฐ€
print(scores)
 
scores.pop(0) #์ •๋ ฌ๋œ ๋ฆฌ์ŠคํŠธ์—์„œ ์ตœ์†Œ๊ฐ’ ๋ฐ˜ํ™˜
scores.pop(len(scores) - 1) #์ตœ๋Œ€๊ฐ’ ๋ฐ˜ํ™˜
print(scores)

sum = 0
avg = 0

for s in scores:
    sum += s

avg = sum / len(scores)

print('์ด์  : %.2f' %sum)
print('์ด์  : %.2f' %avg)
>
(9.5, 8.9, 9.2, 9.8, 8.8, 9.0)
[8.8, 8.9, 9.0, 9.2, 9.5, 9.8]
[8.9, 9.0, 9.2, 9.5]
์ด์  : 36.60
์ด์  : 9.15

๐Ÿ“Œ ํŠœํ”Œ๊ณผ for๋ฌธ

studentCnts = (1, 18), (2, 19), (3, 23), (4, 21), (5, 20), (6, 22), (7, 17)

minClassno = 0 ; minClasscnt = 0
maxClassno = 0 ; maxClasscnt = 0

for classNo, cnt in studentCnts:
    if minClasscnt == 0 or minClasscnt > cnt:
        minClassno = classNo
        minClasscnt = cnt

    if maxClasscnt < cnt:
        maxClassno = classNo
        maxClasscnt = cnt

print('๊ฐ€์žฅ ์ ์€ ํ•™๊ธ‰ : {}๋ฐ˜, ํ•™์ƒ์ˆ˜ : {}๋ช…'.format(minClassno, minClasscnt))
print('๊ฐ€์žฅ ๋งŽ์€ ํ•™๊ธ‰ : {}๋ฐ˜, ํ•™์ƒ์ˆ˜ : {}๋ช…'.format(maxClassno, maxClasscnt))
>
๊ฐ€์žฅ ์ ์€ ํ•™๊ธ‰ : 7๋ฐ˜, ํ•™์ƒ์ˆ˜ : 17๋ช…
๊ฐ€์žฅ ๋งŽ์€ ํ•™๊ธ‰ : 3๋ฐ˜, ํ•™์ƒ์ˆ˜ : 23๋ช…

๐Ÿ“Œ ํŠœํ”Œ๊ณผ while๋ฌธ

cars = ('๊ทธ๋žœ์ €', '์†Œ๋‚˜ํƒ€', '๋ง๋ฆฌ๋ถ€', '์นด๋‹ˆ๋ฐœ', '์˜๋ Œํ† ')

n = 0

while n < len(cars):
    print(cars[n])
    n+=1

flag = True
while flag:
    print(cars[n])
    n+=1

    if n == len(cars):
        flag = False

while True:
    print(cars[n])
    n+=1
    if n == len(cars):
        break