딕셔너리
참고자료: https://docs.python.org/3/tutorial/datastructures.html#dictionaries
5. Data Structures — Python 3.9.6 documentation
5. Data Structures This chapter describes some things you’ve learned about already in more detail, and adds some new things as well. 5.1. More on Lists The list data type has some more methods. Here are all of the methods of list objects: list.append(x)
docs.python.org
dictionary형태 {key, value} 로 이루어져있다.
phone = {'서울' : '02',
'경기': '031',
'인천': '032',
'대전': '042',
'부산': '051',
'울산': '052',
'대구': '053',
'광주': '062'}
"서울" 의 값 가져오기
# "서울" 의 값 가져오기
phone["서울"]
"경기" 의 값 가져오기
# "경기" 의 값 가져오기
phone["경기"]
"제주" : 063 을 딕셔너리에 추가하기
# "제주" : 063 을 딕셔너리에 추가하기
phone["제주"]="063"
딕셔너리 key 값 가져오기
phone.keys()
딕셔너리 value 값 가져오기
phone.values()
제어문
- 조건문(if)
- 반복문(for, while)
- 참고자료: More Control Flow Tools — Python documentation
4. More Control Flow Tools — Python 3.9.6 documentation
docs.python.org
range()
for문의 반복수를 지정할 수 있다.
for i in range(10):
print(i)
i=8
if i%2==0:
print("짝수")
else:
print("홀수")
range 를 사용해 5번 for문을 반복하도록 하고 인덱스 번호가 짝수일 때 구급차, 홀수 일 때 소방차를 출력하기
for i in range(5):
if i%2==0:
print(i,"구급차")
else:
print(i,"소방차")
반복문을 통해 car 변수에 담긴 리스트의 원소를 하나씩 출력하기
car=['경찰차','소방차','구급차','녹차']
for i in car:
print(i)
반복문을 통해 store 변수에 담긴 리스트의 원소를 하나씩 출력하기
store=["서울역점", "강남점", "마포점", "여의도점"]
for s in enumerate(store):
print(s)
enumerate()
인덱스 번호와 원소를 같이 가져오기
for s in enumerate(store):
print(s)
(0, '서울역점')
(1, '강남점')
(2, '마포점')
(3, '여의도점')
type 형태는 tuple이다
튜플: 안의 변수들 추가 삭제 불가능 -- > 튜플에서 리스트로 변경하면 변수 추가나 삭제가 가능하다.
리스트: 안의 변수들 추가 삭제 가능
type((0, '서울역점'))
enumerate를 사용하면 인덱스 번호와 원소를 같이 가져온다.
for i,s in enumerate(store):
print("i=",i,"s=",s)
i= 0 s= 서울역점
i= 1 s= 강남점
i= 2 s= 마포점
i= 3 s= 여의도점
type((0, '서울역점'))
'빅데이터 관련 자료 > Python' 카테고리의 다른 글
파이썬 기초 - 6 (0) | 2021.08.02 |
---|---|
파이썬 기초 - 5 (0) | 2021.07.29 |
파이썬 기초 - 4 (0) | 2021.07.26 |
파이썬 기초 - 2 (0) | 2021.07.21 |
파이썬 기초 - 1 (0) | 2021.07.20 |