모각코/어쩌다보니 박준태가 또 조장이조
어쩌다보니 박준태가 또 조장이조 4차
potatoo
2024. 7. 29. 23:00
728x90
Dictionaries
순서없는 매핑객체 저장, 키-값쌍을 가진다.
{key:value, key:value} 형태로 된다.
key를 사용해 value를 찾는다. 객체 사이에 순서가 없어서 분류할 수 없다.
리스트와 달리 인덱스나 슬라이스가 불가능하다.
d = {'key1':'value1','key2':'value2'}
d['key1']
salaries = {'John':20,'Sally':30,'Sammy':15}
salaries['Sally']
salaries['Cindy']=100 #새로운 값 추가
salaries["John"] = salaries['John'] + 40 # 값 갱신
people = {'John':[1,2,3], 'Sally':[40,10,30]}
people['Sally'][0]
people = {'John':{'salary':10,'age':30}}
people["John"]['age'] # 중첩 딕셔너리
people.items() # 키값 튜플 반환
Tuples
리스트와 유사하지만, 불변성이 중요한 차이점이다.
리스트에서는 기존의 내부 요소를 변형하거나 변경할 수 있다.
순서와 시퀀스가 있는 리스트와 작동 방식이 유사하지만 튜플은 변경이 불가능하다.
튜플 내부에 할당한 요소는 재할당할 수 없다.
튜플 구문에서는 대괄호 대신 소괄호를 사용한다.
Sets
고유한 요소의 정렬되지 않은 집합이다.
고유하다는 말은 같은 종류의 객체는 하나의 대표 객체만 갖는다는 뜻이다.
세트는 고유한 요소의 정렬되지 않은 집합이다.
함수 def
def name_of_function(name='BLANK'): #default 값 지정
'''
Docstring explains function.
'''
print("hello"+name)
return
def add_num(num1, num2):
return num1+num2
result = add_num(2,4)
print(result+10)
print(max([1,4,7,12,100]))
print(min([1,4,7,12,100]))
# enumerate
for letter in ['a', 'b', 'c']:
print(letter)
mylist = ['a', 'b', 'c']
index = 0
for letter in mylist:
print(letter)
print('is at index: {}'.format(index))
index = index + 1
print('')
for index, item in enumerate(mylist):
print(item)
print(f"is at index {index}")
print('')
#해당 인덱스를 가져올 수 있다.
mylist = ['a','b','c','d']
print(''.join(mylist)) # abcd
print('--'.join(mylist)) # a--b--c--d
중첩문과 유효범위
찾는 순서 Local -> Enclosing -> Global -> Built in
#Local
det report():
x = 'local'
print(x)
#Enclonsing
x = 'This is global level'
def enclosing():
x = 'Enclosing Level'
def inside():
print(x)
inside()
enclosing()
def report():
global x # global 변수를 가져오라는 의미
x = 'inside'
return x
728x90