220906 TIL
📍강의 내용 복습
🌀list, tuple, set, dictionary
▪️list 자료형 : list 안에 들어있는 각 요소는 0부터 시작해 순서대로 index 번호를 가지며, indexing과 slicing 기능을 활용해 활용해 원하는 값을 가져올 수 있다. 값 추가, 수정, 삭제 가능!
▪️tuple 자료형 : list와 같이 indexing 사용 가능! 값 추가만 가능!!
▪️set 자료형 : 중복된 값 제외하고 저장! indexing, slicing XX!
▪️dictionary 자료형 : key: value=> key를 사용해 value를 가져옴! 값 추가, 수정, 삭제 가능!
🌀자료형 변환
특정 값의 자료형을 조건이 맞을 경우 자유롭게 변환 가능!
ex) list를 tuple로, tuple을 set로
sample_list = [1, 2, 3, 4, 5]
sample_tuple = tuple(sample_list)
sample_set = set(sample_tuple)
print(sample_list)
print(sample_tuple)
print(sample_set)
# result print
"""
[1, 2, 3, 4, 5]
(1, 2, 3, 4, 5)
{1, 2, 3, 4, 5}
"""
🌀다른 파일에 있는 코드 import해서 사용하기
│ a.py
│ main.py
├─ folder
│ ├─ b.py
│ ├─ c.py
▪️import “파일명”을 사용해 다른 파일에 선언된 코드를 가져와서 사용.
# a.py
def a_funtion():
print("execute a")
# main.py
import a # a 파일을 import
a.a_funtion() # a 파일의 a_funtion() 사용
▪️from "파일명" import " "
# a.py
def a_funtion():
print("execute a")
# main.py / case 1
from a import a_funtion # a 파일에 있는 a_funtion을 import
# 이 경우 위 예제와 다르게, a.a_funtion가 아닌 a_funtion으로 사용한다.
a_funtion() # execute a
# main.py / case 2
from a import * # a 파일에 있는 모든 함수를 import
a_funtion() # execute a
▪️다른 폴더에 있는 코드 import
# folder/b.py
def b_funtion():
print("execute b")
# folder/c.py
def c_funtion1():
print("execute c1")
def c_funtion2():
print("execute c2")
# main.py
from folder import b
from folder.c import *
b.b_funtion() # execute b
c_funtion1() # execute c1
c_funtion2() # execute c2
▪️from 과 import
python에서 다른 파일에 있는 코드를 사용할 때에는 어디서(from) 어떤(import) 것을
가져와서 사용할지 지정해 줘야 합니다.
main.py 파일을 기준으로, 선언된 함수와 변수의 경로는 아래와 같습니다.
a.a_funtion
folder.b.b_funtion
folder.b.PIE
folder.b.HELLO
folder.c.c_funtion1
folder.c.c_funtion2
여기서 다양한 방식으로 from과 import를 사용할 수 있습니다.
from a import a_funtion
from a import *
import a
from folder import b
from folder.b import *
from folder.c import c_funtion1, c_funtion2
🌀조건문 : and, or을 사용해 2개 이상의 조건을 복합적으로도 사용 가능!
if 조건문 :
code
elif 조건문 :
code
else : //만족하는 조건 없을 때
code
비어있는 string, list 등은 분기문에서 False로 판단합니다.
empty_string = ""
empty_list = []
if not empty_string:
print("string is empty!!")
if not empty_list:
print("list is empty!!")
📍백준 단계별로 풀어보기 알고리즘 실습
🌀10818번 배열, 리스트에서 최댓값, 최솟값 구하기
max(배열이름) : 최댓값. //// min(배열이름) : 최솟값
🌀배열에 저장하기(2562번 최댓값)
numbers = [int(input()) for i in range(9)]
=> 한줄에 하나씩 정수를 range(9)만큼 입력받아 numbers라는 배열에 저장한다.
🌀배열 안에 중복되는 요소 제거 set(배열이름)
numbers = [(int(input()) % 42) for i in range(10)]
print(len(set(numbers)))
=> 위에 코드에 42로 나눈 나머지라는 코드만 추가하면 나머지의 배열을 구할수 있다. 중복되는 요소를 제거하기 위해 집합을 구하는 set함수를 사용했다.