python 기본문법2
- 리스트 ~ 딕셔너리
a = [1,2,3]
b = [1,2,'강아지','귀여워']
c = []
d = list()
a = [1,2,3]
print(len(a)) #3
b = [1,2,3,[4,5],6]
print(len(b)) #5
a = [1,2,3,4]
print(a[2]) #3
print(a[1:3]) #[2,4] 1~3전까지
print(a[-1]) # 4 맨마지막
a = [1,2,[3,4],5]
print(a[2]) #[3,4]
print(a[2][0]) #3
a = [1,2,3]
a.append(4)
print(a) #[1,2,3,4]
a.append([5,6])
print(a) #[1,2,3,4,[5,6]]
## 더하기 연산
a +=[7,8]
print(a) #[1,2,3,4,[5,6],7,8]
a = [2,4,3,1]
a.sort()
print(a) #[1,2,3,4]
## 반대로 정렬
a.sort(reverse = True)
print(a) #[4,3,2,1]
a = [1,4,3,2,5]
print(4 in a) #True
print('2' in a) #False
print(9 not in a) #True
a = {'first' : 1 , 'second':2} #value:'first' value:1
print(a['first']) #1
#빈 딕셔너리 만들기
a = {}
a = dict()
## 수정
dog = {"name" : 'choco', 'age': 10}
dog['name'] = 'maroo'
print(dog) #{"name" : 'maroo', 'age': 10}
##추가
dog['weight'] = 4
print(dog) #{"name" : 'maroo', 'age': 10, 'weight' :4 }
dog = {"name" : 'choco', 'age': 10, 'favorite':{'snack':'gum', 'play' : 'walk'}}
print(dog['favorite']) #{'snack':'gum', 'play' : 'walk'}
print(dog['favorite']['play']) #walk
dog = {"name" : 'choco', 'age': 10}
print('gender' in dog) #False
print('haircolor' not in dog) #True
people = [
{'name': 'bob', 'age': 20, 'score':{'math':90,'science':70}},
{'name': 'carry', 'age': 38, 'score':{'math':40,'science':72}},
{'name': 'smith', 'age': 28, 'score':{'math':80,'science':90}},
{'name': 'john', 'age': 34, 'score':{'math':75,'science':100}}
]
##john 의 math 점수?
print(people[3]['score']['math']) #75