본문 바로가기
파이썬/개념정리

[Python] 개념 정리 # 2 (feat. 나도 코딩)

by 로토마 2021. 7. 21.

# 리스트 (list)

선언)

name = [ "rotoma1" , "rotoma2", "rotoma3"]

 

함수)

- index 요소의 위치 값 반환

print(name.index("rotoma2"))  # 1

 

- append 리스트 뒤에 요소 추가

name.append("rotoma4")

print(name)  # name = [ "rotoma1" , "rotoma2", "rotoma3", "rotoma4"]

 

- insert(위치,요소) 해당 위치에 요소 삽입

name.insert(1,"rotoma1.5")

print(name) # name = [ "rotoma1" , "otoma1.5", "rotoma2", "rotoma3", "rotoma4"]

 

- pop() 뒤에 값 반환 후 삭제

print(name.pop()) # rotoma4

print(name) # name = [ "rotoma1" , "otoma1.5", "rotoma2", "rotoma3"]

 

선언)

num = [5, 2, 4, 3, 1]

 

함수)

- sort 오름차순 정렬

num.sort()

print(num) # num = [1, 2, 3, 4, 5]

 

- reverse 뒤집기 

num.reverse()

print(num) # num = [5, 4, 3, 2, 1]

 

- clear 모두 지우기

num.clear()

print(num) # num = [] 

 

- extend 리스트 확장

num = [1, 2, 3]

list_mix = ["rotoma", 20, True]

num.extend(list_mix)

print(num) # num = [ 1, 2, 3, "rotoma", 20, True]

 

# 사전 (dic)

dic={key:value, key:value ... } 형식으로 이루어진다.

key값은 항상 고유해야한다. 즉, 중복되면 안된다.

 

선언)

cabinet = {3 : 'rotoma' , 100 : 'tomato', 300 : 'coding'}

 

함수)

- print / .get() key에 해당하는 value값 출력하기 

print(cabinet[100])  # tomato

print(cabinet.get(100))  # tomato

print(cabinet.get(123))  # None

 

- in dic 해당 key가 dic에 있는지 확인하기

print(3 in cabinet)  # True

print(5 in cabinet)  # False

 

- dic 내용 수정 및 추가

cabinet[300] = "python"

cabinet[500] = "basic"

print(cabinet)   # {3 : 'rotoma' , 100 : 'tomato', 300 : 'python', 500 : 'basic' }

 

- del 삭제하기

del cabinet[500]

print(cabinet)   #  {3 : 'rotoma' , 100 : 'tomato', 300 : 'python'}

 

- .keys() key 들만 출력

print(cabinet.keys())  # dict_keys([3, 100, 300])

 

- .values() value 들만 출력

print(cabinet.values())   #dict_values(['rotoma', 'tomato', 'python'])

 

- .items() key, value 쌍으로 출력

print(cabinet.items())   #dict_items({3 : 'rotoma' , 100 : 'tomato', 300 : 'python'})

 

- .clear() 전체 삭제

cabinet.clear()

print(cabinet)   # {}

 

# 튜플

튜플은 리스트와 다르게 내용을 변경하거나 추가할 수 없다. 하지만, 속도는 리스트보다 빠르다. 

 

선언)

menu = ("돈까스", "치즈까스")

 

함수)

- 출력

print(menu[0])  # 돈까스

print(menu[1])  # 치즈까스

 

-여러 변수 한번에 선언

(name, age, hobby) = ("Rotoma", 20 , "coding")

-출력

print(name,age,hobby)  # Rotoma 20 coding

 

# 집합(set)

중복이 안되고, 순서가 없다. 

 

선언)

my_set = {1,2,3,3,3}

print(my_set)  # {1,2,3}

 

red = {"apple", "tomato" , "blood"}

round = set(["apple","ball"])

 

함수)

 

- 교집합

print(red & round)  #  {'apple'}

print(red.intersection(round))  # {'apple'}

 

- 합집합

print( red | round )  # {'apple','tomato','blood','ball'}

print(red.union(round))  #  {'apple','tomato','blood','ball'}

 

- 차집합

print( red - round )  # {'tomato','blood'}

print( red.difference(round) )   # {'tomato','blood'}

 

- add 추가

round.add("egg")

print(round)   #  {'apple','ball', 'egg'}

 

- remove 삭제

red.remove("blood")

print("red")   #  {"apple", "tomato" }

 

# 자료구조의 변경

my_inf = {"rotoma", 20, "coding"}

print(my_inf, type(my_inf))   #  {'rotoma', 20, 'coding'} <class 'set'>

 

- set -> list

my_inf = list(my_inf)

print(my_inf, type(my_inf))   #  ['rotoma', 20, 'coding'] <class 'list'>

 

- list -> tuple

my_inf = tuple(my_inf)

print(my_inf, type(my_inf))   #  ('rotoma', 20, 'coding') <class 'tuple'>

 

- tuple -> set

my_inf = set(my_inf)

print(my_inf, type(my_inf))   #  {'rotoma', 20, 'coding'} <class 'set'>

 

 

# 퀴즈

퀴즈4

우선 이 문제를 풀기에 압써  random 모듈을 import 해줘야 한다.

그리고 1~ 20 까지의 수를 담은 list를 선언하고 shuffle로 잘 섞어준다.

그리고 sample을 사용해 4개의 수를 뽑아 winners 리스트에 담아준다. 출력을 이쁘게 해주면 된당 ㅎ

 

코드)