Naver Project (Python 02)

Jacob Kim·2024년 1월 25일
0

Naver Project Week 1

목록 보기
4/28

Python list to dictionary examples

Python List

data = [1, 2]
data.append(3)
print(data)

del data[2]
print(data)
#[1, 2, 3]
#[1, 2]

List 선언하기

  • 아래 데이터들을 이용해서 List를 선언해보세요
서울, 인천, 경기, 충청, 전라, 경상, 강원
city = ["서울", "인천", "경기", "충청", "전라", "경상", "강원"]
print(city)
#['서울', '인천', '경기', '충청', '전라', '경상', '강원']

List 추가하기

  • 위에 선언한 리스트에 '제주'를 추가해보세요
city.append("제주")
print(city)
#['서울', '인천', '경기', '충청', '전라', '경상', '강원', '제주']

List 제거하기

  • 해당 리스트 중 서울을 제거해보세요.
del city[0]
print(city)
#['인천', '경기', '충청', '전라', '경상', '강원', '제주']

List 연산

  • 두 개의 리스트가 주어졌을 때 두 리스트를 합쳐서 출력해보세요
a = [1, 3, 5]
b = [2, 4, 6]
a = [1, 3, 5]
b = [2, 4, 6]

c = a + b

print(c)
#[1, 3, 5, 2, 4, 6]

List 정렬

  • 주어진 리스트를 오름차순으로 정렬하세요.
print(sorted(c))
print(sorted(city))
#[1, 2, 3, 4, 5, 6]
#['강원', '경기', '경상', '인천', '전라', '제주', '충청']

Python Tuple

Tuple 생성하기

  • 다음 데이터를 이용해서 튜플을 생성해보세요
사과, 체리, 바나나, 참외
fruits = ('사과', '체리', '바나나', '참외')
print(fruits)
#('사과', '체리', '바나나', '참외')

Tuple을 List로 변환

  • 아래 데이터를 Tuple로 만들고 리스트로 변환해보세요.
fruits = ('사과', '체리', '바나나', '참외')
fruits = ('사과', '체리', '바나나', '참외')
print(fruits)

list_fruits = list(fruits)
print(list_fruits)
#('사과', '체리', '바나나', '참외')
#['사과', '체리', '바나나', '참외']

Unpacking Tuple

  • 아래 튜플을 unpacking해서 출력해보세요
fruits = ('사과', '체리', '바나나', '참외')
fruits = ('사과', '체리', '바나나', '참외')
print(fruits)

a, b, c, d = fruits
print(a, b)
print(c, d)
#('사과', '체리', '바나나', '참외')
#사과 체리
#바나나 참외

Python Dictionary

데이터를 가지고 있지 않은 Dictionary

  • 데이터 없이 선언된 dictionary 변수를 생성해보세요.
no_data = {"김밥":1000, "라면":2000}
print(no_data)

no_data["우동"] = 2500
print(no_data)
#{'김밥': 1000, '라면': 2000}
#{'김밥': 1000, '라면': 2000, '우동': 2500}

Dictionary 데이터 추가

key, value로 이뤄진 데이터를 추가해보세요.

국어 85, 수학 80, 영어 75
score = {"국어":85, "수학":80, "영어":75}
print(score)
#{'국어': 85, '수학': 80, '영어': 75}

기존 Dictionary에 새로운 데이터 추가

  • 위에 선언한 score 변수에 아래 key, value를 추가해보세요.
사회 80, 과학 85
score["사회"] = 80
score["과학"] = 85
print(score)
#{'국어': 85, '수학': 80, '영어': 75, '사회': 80, '과학': 85}

Dictionary 데이터 삭제

  • 위 score 값 중 사회 값을 삭제해보세요.
del score["사회"]
print(score)
#{'국어': 85, '수학': 80, '영어': 75, '과학': 85}

Dictionary 값 업데이트

  • score 데이터 중 수학의 value를 85로 변경해보세요.
score["수학"] = 85
print(score)
#{'국어': 85, '수학': 85, '영어': 75, '과학': 85}

Dictionary 에서 key 와 value 값 출력

  • 주어진 dictionary 데이터가 있을때, key, value 값을 따로 출력해보세요.
print(score.keys())
print(score.values())

#dict_keys(['국어', '수학', '영어', '과학'])
#dict_values([85, 85, 75, 85])
data = [1, 1, 2, 3, 5, 5, 6]
set_data = set(data)
print(set_data)
#{1, 2, 3, 5, 6}
profile
AI, Information and Communication, Electronics, Computer Science, Bio, Algorithms

0개의 댓글