students = ['홍길동', '박찬호', '이용규']
print('students: {}'.format(students))
studentsMul = students * 2
print('studentsMul: {}'.format(studentsMul))
students = ['홍길동','강호동', '박찬호', '이용규', '박승철', '강호동', '김지은']
searchIdx = students.index('강호동')
print('searchIdx: {}'.format(searchIdx))
searchIdx = students.index('강호동', 2, 6) # 인덱스 2부터 5까지의 범위 중에 강호동의 위치를 찾는 것.
print('searchIdx: {}'.format(searchIdx))
------------------------------------------------------
# 실습: 1부터 10까지의 정수가 중복되지 않고 섞여 있을때 7의 위치를 찾아보자.
import random
sampleList = random.sample(range(1,11),10)
selectIdx = int(input('숫자 7의 위치 입력: '))
searchIdx = sampleList.index(7)
if searchIdx == selectIdx:
print('빙고!')
else:
print('ㅜㅜ')
print('sampleList: {}'.format(sampleList))
print('searchIdx: {}'.format(searchIdx))
students = ['홍길동','강호동', '박찬호', '이용규', '박승철', '강호동', '김지은']
print('students: {}'.format(students))
searchCnt = students.count('강호동')
print('searchCnt: {}'.format(searchCnt))
# 실습: 하루 동안 헌혈을 진행한 후 혈액형 별 개수를 파악하는 프로그램을 만들어보자.
import random
types = ['A', 'B', 'O', 'AB']
todayData = []
typeCnt = []
for i in range(100):
type = types[random.randrange(len(types))] # randrange() 함수는 여기서 0, 1, 2, 3 중에 하나를 추출
todayData.append(type)
print('todayData: {}'.format(todayData))
print('todayData length: {}'.format(len(todayData)))
for type in types:
print('{}형: {}개'.format(type, todayData.count(type)))
students = ['홍길동', '강호동', '박찬호', '이용규', '박승철', '강호동', '김지은']
del students[1] # 슬라이싱 기능도 사용 가능
print('students: {}'.format(students))
- 리스트와 비슷하지만 아이템 변경이 불가능하다. (수정, 삭제 등)
- ( )을 이용해서 선언하고, 데이터 구분은 ,를 이용한다.
- 숫자, 문자, 논리형 등 모든 기본 데이터를 같이 저장할 수 있다.
- 튜플에 또 다른 컨텐이너 자료형 데이터를 저장할 수도 있다.
students = ('홍길동', '박찬호', '이용규', '박승철', '김지은')
print('students: {}'.format(students))
print('students type: {}'.format(type(students)))
numbers = (10, 20, 30, 40, 50)
print('numbers: {}'.format(numbers))
students = ('홍길동', '박찬호', '이용규', '박승철', '김지은')
print('students[0]: {}'.format(students[0]))
print('students[1]: {}'.format(students[1]))
print('students[2]: {}'.format(students[2]))
print('students[3]: {}'.format(students[3]))
print('students[4]: {}'.format(students[4]))
# 실습: 5명의 학생 이름을 튜플에 저장하고 인덱스가 홀수인 학생과 짝수(0포함)인 학생을 구분해서 인덱스와 학생 이름을 출력하자.
students = ('김성예', '신경도', '박기준', '최승철', '황동석')
print('----------- 인덱스 짝수 -----------')
print('students[0]: {}'.format(students[0]))
print('students[2]: {}'.format(students[2]))
print('students[4]: {}'.format(students[4]))
print('----------- 인덱스 홀수 -----------')
print('students[1]: {}'.format(students[1]))
print('students[3]: {}'.format(students[3]))
for i in range(len(students)):
if i % 2 == 0:
print('인덱스 짝수: students[{}]: {}'.format(i, students[i]))
else:
print('인덱스 홀수: students[{}]: {}'.format(i, students[i]))
studentsTuple = ('홍길동', '박찬호', '이용규', '박승철', '김지은')
print('studentsTuple: {}'.format(studentsTuple))
searchName = input('학생 이름 입력: ')
# in
if searchName in studentsTuple:
print('{} 학생은 우리반 학생입니다.'.format(searchName))
else:
print('{} 학생은 우리반 학생이 아닙니다.'.format(searchName))
# not in
if searchName not in studentsTuple:
print('{} 학생은 우리반 학생이 아닙니다.'.format(searchName))
else:
print('{} 학생은 우리반 학생입니다.'.format(searchName))
studentsTuple = ('홍길동', '박찬호', '이용규', '박승철', '김지은', '강호동')
sLength = len(studentsTuple)
print('sLength: {}'.format(sLength))
# for문
for i in range(len(studentsTuple)):
print('i: {}'.format(i))
print('students[{}]: {}'.format(i, studentsTuple[i]))
# while문
n = 0
slength = len(studentsTuple)
while n < slength:
print('n: {}'.format(n))
print('students[{}]: {}'.format(n, studentsTuple[n]))
n += 1
# 기타
for s in studentsTuple:
print('student: {}'.format(s))
studentsTuple1 = ('홍길동', '박찬호', '이용규')
studentsTuple2 = ('박승철', '김지은', '강호동')
studentsTuple3 = studentsTuple1 + studentsTuple2
print('studentsTuple3: {}'.format(studentsTuple3))
# 실습: 튜플을 이용해서 나와 친구가 좋아하는 번호를 합치되 번호가 중복되지 않도록 하는 프로그램
myNumbers = (1, 3, 5, 6, 7)
friendNumbers = (2, 3, 5, 8, 10)
print('myNumbers: {}'.format(myNumbers))
print('friendNumbers: {}'.format(friendNumbers))
for number in friendNumbers:
if number not in myNumbers:
myNumbers = myNumbers + (number, ) # 정수인 number을 튜플로 바꾸는 법 -> (number, )
print('myNumbers: {}'.format(myNumbers))
students = ('홍길동', '박찬호', '이용규', '강호동', '박승철', '김지은')
print('students: {}'.format(students))
print('students[2:4]: {}'.format(students[2:4]))
print('students[:4]: {}'.format(students[:4]))
print('students[2:]: {}'.format(students[2:]))
print('students[2:-2]: {}'.format(students[2:-2]))
numbers = (2, 50, 0.12, 1, 9, 7, 17)
print('numbers: {}'.format(numbers))
print('numbers[2:4]: {}'.format(numbers[2:4]))
print('numbers[:4]: {}'.format(numbers[:4]))
print('numbers[2:]: {}'.format(numbers[2:]))
print('numbers[-5:-2]: {}'.format(numbers[-5:-2]))
print('numbers[-5:-2:2]: {}'.format(numbers[2:-2:2]))
students = ['홍길동', '박찬호', '이용규', '강호동', '박승철', '김지은']
print('students: {}'.format(students))
students[1:4] = ('park chanho', 'lee yonggyu', 'gang hodong')
print('students: {}'.format(students))
튜플은 리스트와 달리 아이템 추가, 변경, 삭제가 불가능하다.
students = '홍길동', '박찬호', '이용규', '이대호'
print(students)
print(type(students))
students = ['홍길동', '박찬호', '이용규', '박용택']
print('students: {}'.format(students))
print('students type: {}'.format(type(students)))
students = tuple(students)
print('students: {}'.format(students))
print('students type: {}'.format(type(students)))
students = list(students)
print('students: {}'.format(students))
print('students type: {}'.format(type(students)))
# 실습: 튜플을 이용한 점수표에서 최저, 최고 점수를 삭제한 후 총점과 평균을 출력해보자.
playerScore = (9.5, 8.9, 9.2, 9.8, 8.8, 9.0)
print('playerScore: {}'.format(playerScore))
print('playerScore type: {}'.format(type(playerScore)))
playerScore = list(playerScore)
print('playerScore: {}'.format(playerScore))
print('playerScore type: {}'.format(type(playerScore)))
playerScore.sort()
print('playerScore: {}'.format(playerScore))
playerScore.pop(0)
playerScore.pop(len(playerScore)-1)
print('playerScore: {}'.format(playerScore))
playerScore = tuple(playerScore)
print('playerScore: {}'.format(playerScore))
sum = 0
avg = 0
for score in playerScore:
sum += score
avg = sum / len(playerScore)
print('총점: %.2f' % sum)
print('평균: %.2f' % avg)
students = ('홍길동', '박찬호', '이용규', '강호동', '박찬호', '김지은')
print('students type: {}'.format(type(students)))
print('students: {}'.format(students))
students = list(students)
print('students: {}'.format(students))
students.sort(reverse=True)
print('students: {}'.format(students))
students = ('홍길동', '박찬호', '이용규', '강호동', '박찬호', '김지은')
sortedStudents = sorted(students)
print('sortedStudents type: {}'.format(type(sortedStudents)))
print('sortedStudents: {}'.format(sortedStudents))
cars = '그랜저', '쏘나타', '말리부', '카니발', '쏘렌토'
for i in range(len(cars)):
print(cars[i])
for car in cars:
print(car)
studentCnts = (1, 19), (2, 20), (3, 22), (4, 18), (5, 21)
for classNo, cnt in studentCnts:
print('{}학급 학생수: {}'.format(classNo, cnt))
minScore = 60
korScore = int(input('국어 점수: '))
engScore = int(input('영어 점수: '))
mathScore = int(input('수학 점수: '))
sciScore = int(input('과학 점수: '))
hisScore = int(input('국사 점수: '))
scores = (('국어', korScore), ('영어', engScore), ('수학', mathScore), ('과학', sciScore), ('국사', hisScore))
for subject, score in scores:
if score < 60:
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: # minCnt == 0 이면 들어오는 값 먼저 대입
minClassNo = classNo
minCnt = cnt
if maxCnt < cnt:
maxClassNo = classNo
maxCnt = cnt
print('학생 수가 가장 적은 학급(학생수): {}학급({})명'.format(minClassNo, minCnt))
print('학생 수가 가장 많은 학급(학생수): {}학급({})명'.format(maxClassNo, maxCnt))
cars = ('그랜저', '쏘나타', '말리부', '카니발', '쏘렌토')
# 1)
n = 0
while n < len(cars):
print(cars[n])
n += 1
# 2)
n = 0
flag = True
while flag:
print(cars[n])
n += 1
if n == len(cars):
flag = False
# 3)
n = 0
while True:
print(cars[n])
n += 1
if n == len(cars):
break
minScore = 60
scores = (('국어', 58), ('영어', 77), ('수학', 89), ('과학', 99), ('국사', 50))
n = 0
while n < len(scores):
if scores[n][1] < minScore:
print('과락 과목: {}, 점수: {}'.format(scores[n][0], scores[n][1]))
n += 1
# 실습: while문 이용해서 학급 학생 수가 가장 작은 학급, 가장 많은 학급 출력
studentCnt = (1, 18), (2, 19), (3, 23), (4, 21), (5, 20), (6, 22), (7, 17)
minClassNo = 0; maxClassNo = 0
minCnt = 0; maxCnt = 0
n = 0
while n < len(studentCnt):
if minCnt == 0 or minCnt > studentCnt[n][1]: # minCnt == 0, 처음에 오면 0이니 변경을 해줘야 함.
minClassNo = studentCnt[n][0]
minCnt = studentCnt[n][1]
if maxCnt < studentCnt[n][1]:
maxClassNo = studentCnt[n][0]
maxCnt = studentCnt[n][1]
n += 1
print('학생 수가 가장 적은 학급(학생수): {}학급({})명'.format(minClassNo, minCnt))
print('학생 수가 가장 많은 학급(학생수): {}학급({})명'.format(maxClassNo, maxCnt))
- 키(key)와 값(value)를 이용해서 자료를 관리한다.
-> 키는 절대 중복되지 말아야 한다. 값은 중복되도 가능하다.
students = {'이름': '홍길동', '메일': 'gildong@gmail.com', '학년': 3, '취미': ['농구', '게임']}
students = {1: student1, 2: student2, 3: student2}
students = {'s1':'홍길동', 's2':'박찬호', 's3':'이용규', 's4':['박세리','박공주']}
print(students['s1'])
print(students['s2'])
print(students['s3'])
print(students['s4'][1]) # 박공주만 출력
students = {'s1':'홍길동', 's2':'박찬호', 's3':'이용규', 's4':['박세리','박공주']}
print(students['s5'])
students = {'s1':'홍길동', 's2':'박찬호', 's3':'이용규', 's4':['박세리','박공주']}
print(students.get('s1'))
print(students.get('s6')) # 에러 발생 X, None 출력
myInfo = {}
myInfo['이름'] = '이철민'
myInfo['나이'] = 27
myInfo['취미'] = ['축구','야구']
print(myInfo)
myInfo['메일'] = 'sjleepc@naver.com' # 키 값은 중복될 수 없기에 메일의 값이 변경됨.
print(myInfo)
# 실습1: 학생 정보를 입력받아 딕셔너리에 추가해보자.
students = {}
students['name'] = input('이름 입력: ')
students['grade'] = input('학년 입력: ')
print(f'students: {students}')
---------------------------------------------------
# 실습2: 0부터 10까지의 각각의 정수에 대한 팩토리얼을 딕셔너리에 추가해보자.
factorialDic = {}
for i in range(11):
if i == 0:
factorialDic[i] = 1
else:
for j in range(1,i+1):
factorialDic[i] = factorialDic[i-1] * j
print(f'factorialDic: {factorialDic}')
# 실습1: 학생의 시험 점수가 60점 미만이면 'F(재시험)'으로 값을 변경해보자.
scores = {'kor':88, 'eng':55, 'math':85, 'sci':57, 'his':82}
print(f'scores: {scores}')
minScore = 60
fstr = 'F(재시험)'
if scores['kor'] < minScore: scores['kor'] = fstr
if scores['eng'] < minScore: scores['eng'] = fstr
if scores['math'] < minScore: scores['math'] = fstr
if scores['sci'] < minScore: scores['sci'] = fstr
if scores['his'] < minScore: scores['his'] = fstr
print(f'scores: {scores}')
# 실습2: 하루에 몸무게(kg)와 신장(m)이 각각 -0.3kg, + 0.001m씩 변한다고 할 때, 30일 후의 몸무게와 신장의 값을 저장하고 Bmi 값도 출력해보자.
myBodyInfo = {'이름':'gildong', '몸무게':83.0, '신장':1.8}
myBmi = myBodyInfo['몸무게'] / myBodyInfo['신장'] ** 2
print(f'myBodyInfo: {myBodyInfo}')
print(f'myBmi: {round(myBmi, 2)}')
date = 0
while True:
myBodyInfo['몸무게'] = round((myBodyInfo['몸무게'] - 0.3), 2)
print('몸무게: {}'.format(myBodyInfo['몸무게']))
myBodyInfo['신장'] = round((myBodyInfo['신장'] + 0.001), 3)
print('신장: {}'.format(myBodyInfo['신장']))
myBmi = myBodyInfo['몸무게'] / myBodyInfo['신장'] ** 2
date += 1
if date >= 30:
break
print(f'myBodyInfo: {myBodyInfo}')
print(f'myBmi: {round(myBmi, 2)}')
memInfo = {'이름':'홍길동', '메일':'cmleepc@naver.com', '학년':4}
ks = memInfo.keys()
print(f'ks: {ks}')
print(f'ks type: {type(ks)}')
vs = memInfo.values()
print(f'vs: {vs}')
print(f'vs type: {type(vs)}')
items = memInfo.items()
print(f'items: {items}')
print(f'items type: {type(items)}')
ks = list(ks)
print(f'ks: {ks}')
print(f'ks type: {type(ks)}')
for key in memInfo.keys():
print(f'{key}: {memInfo[key]}')
# 실습: 학생의 시험 점수가 60점 미만이면 F(재시험)으로 값을 변경하는 코드를 keys()를 이용해서 작성해보자.
scores = {'kor':88, 'eng':55, 'math':85, 'sci':57, 'his':82}
print(f'scores: {scores}')
minScore = 60
fDic = {}
fstr = 'F(재시험)'
for key in scores:
if scores[key] < minScore:
scores[key] = fstr
fDic[key] = fstr
print(f'scores: {scores}')
print(f'fDic: {fDic}')
memInfo = {'이름':'홍길동', '메일':'cmleepc@naver.com', '학년':4, '취미':['축구', '야구']}
print(f'memInfo: {memInfo}')
del memInfo['이름']
print(f'memInfo: {memInfo}')
memInfo = {'이름':'홍길동', '메일':'cmleepc@naver.com', '학년':4, '취미':['축구', '야구']}
print(f'memInfo: {memInfo}')
returnValue = memInfo.pop('메일')
print(f'memInfo: {memInfo}')
print(f'returnValue: {returnValue}')
print(f'returnValue type: {type(returnValue)}')
# 실습: 딕셔너리에 저장된 점수 중 최저 및 최고 점수를 삭제하는 프로그램을 만들어보자.
scores = {'s1':8.9, 's2': 8.1, 's3': 8.5, 's4': 9.8, 's5': 8.8}
minScore = 0; minScoreKey = 0
maxScore = 0; minSocreKey = 0
for key in scores.keys():
if minScore == 0 or scores[key] < minScore:
minScore = scores[key]
minScoreKey = key
if scores[key] > maxScore:
maxScore = scores[key]
maxScoreKey = key
print(f'scores: {scores}')
print(f'minScore: {minScore}')
print(f'minScoreKey: {minScoreKey}')
print(f'maxScore: {maxScore}')
print(f'maxScoreKey: {maxScoreKey}')
del scores[minScoreKey]
del scores[maxScoreKey]
print(f'scores: {scores}')
memInfo = {'이름':'홍길동', '메일':'cmleepc@naver.com', '학년':4, '취미':['축구', '야구']}
print('이름' in memInfo)
print('name' not in memInfo)
memInfo = {'이름':'홍길동', '메일':'cmleepc@naver.com', '학년':4, '취미':['축구', '야구']}
print('len(memInfo): {}'.format(len(memInfo)))
memInfo = {'이름':'홍길동', '메일':'cmleepc@naver.com', '학년':4, '취미':['축구', '야구']}
memInfo.clear()
print('memInfo: {}'.format(memInfo))
# 실습: 개인 정보에 '연락처'와 '주민등록번호'가 있다면 삭제하는 코드를 작성하자.
myInfo = {'이름':'이철민', '나이': 27, '연락처':'010-1234-5678',
'주민등록번호':'970000-1000000', '주소':'서울'}
deleteItems = ['연락처', '주민등록번호']
for item in deleteItems:
if item in myInfo:
del myInfo[item]
print(myInfo)