3.map(함수, 반복 가능한 개체)
반복 가능한 객체(리스트,튜플)에 함수를 적용해서 새로운 반복가능한 객체를 반환 하는 함수
map(lambda x, y: x + y, a, b)
# update : 딕셔너리에 딕셔너리 추가
main_dict = {
"key1": {"subkey1": 1, "subkey2": 2}
}
# 새 딕셔너리 추가
new_dict = {
"key2": {"subkey3": 3},
"key3": {"subkey4": 4, "subkey5": 5}
}
main_dict.update(new_dict)
#
{k: v for k, v in d.items()}
# K:v / 딕셔너리 새키: 딕셔너리 새 값
# for k, v in d.items() / 기존 딕셔너리의 key와 value를 반복
{k.upper(): v * 2 for k, v in d.items()}
{k: v for k, v in d.items() if v >= 10}
pprint()
딕셔너리 등의 복잡한 중첩구조가 있을때 사용
함수의 매개변수는 기본값이 없는것이 앞으로 와야한다.
def test_func1(A, B='test'):
return A + B
# 1.
y = 50 # 전역 변수
def modify_variable():
global y # 전역 변수 y를 사용하겠다고 명시
y = y + 1
return y
print(modify_variable()) # 출력: 51
print(y) # 출력: 51 (전역 변수 값이 업데이트됨)
# 2.
def modify_variable():
y = 50 # 로컬 변수 초기화
y = y + 1
return y
print(modify_variable()) # 출력: 51
# 3.
y = 50 # 전역 변수
def read_variable():
return y + 1 # 전역 변수 y를 읽기만 함
print(read_variable()) # 출력: 51
print(y) # 출력: 50 (전역 변수는 수정되지 않음)
with open(path , 'r') as file:
files = file.read()
print(files)
with open(path 'w') as file:
file,write('test')
a,b,c = map(int,input().split())
isinstance(검증하려는데이터, 데이터타입)
True/False 값으로 값이 나온다
if 문의 조건으로 들어가기도 한다
딕셔너리 <-> json 변환
import json
dictionary # 딕셔너리 데이터
json.dumps(dictionary)