빅데이터 분석기사/퇴근 후 딴짓
python 2, 3 - 인덱싱, 슬라이싱, 반복분 -enumerate(), def 함수
유방울
2024. 5. 5. 02:37
오류 모음
1. AttributeError
# 밸류 값
# dictbox.values()
# di에는 이런 속성이 없다는 의미 -> 오류
di.value()
AttributeError: 'dict' object has no attribute 'value'
2. IndexError
listbox = [2,4,6,8,10]
# list[index] 원소 값
# print(listbox[0]) # index 첫번째 값
# print(listbox[3]) # index 3 (네번째)
# python은 index는 0부터 시작함
# 첫번째 인덱스 값을 출력하려면 0을 써야 함
# 범위가 넘어섰다는 에러 메세지를 받게 됨 -> 오류
listbox[5]
IndexError: list index out of range
3. NameError
# lisbox가 정의되지 않았다 -> 오타라는 오류
# 최소값
# min(listbox)
min(listbox)
min(lisbox)
NameError: name 'lisbox' is not defined
4. typeError
# 반올림
# round(1.2345, 2)
# round(1.2375, 2)
# round에는 2가지 값만 적어야 하는데 3가지를 적었다 -> 오
round(1,1235, 2)
TypeError: round() takes at most 2 arguments (3 given)
문자열
replace('원래 내', '바꾸고 싶은 내용')
# 한 단어 변경
# text = "빅데이터 분석기사 파이썬 공부"
# text = text.replace("공부","스터디")
# text
text = '빅데이터 분석기사 파이썬 공부'
text = text.replace("공부","스터디")
text
반복문
# 0부터 4까지 반복 출력
for item in range(5):
print(item)
# 5부터 9까지 반복 출력
for i in range(5,10):
print(i)
enumerate()
인덱스, 값 모두 추출할 수 있음!!!
# 리스트에 있는 문자열과 인덱스값 출력
listbox = ['네모','세모','동그라미', '별']
# enumerate()
# 리스트박스를 묶어주면 인덱스, 아이템 값 모두 얻어올 수 있음
# for index, item in enumerate(listbox):
# print(index)
# print(item)
def 함수
1. 기본함수
# 함수 정의
def hello():
print("안뇨옹")
# 함수 호출
hello()
2. 함수(파라미터)
# 함수 정의 (파라미터)
def plus(x, y):
print(x+y)
3. 함수(리턴 값)
# 함수 정의 (리턴 값)
def plus(x, y):
result = x+y
return result
# 리턴값이 2개 (최소, 최대값을 구하는 함수)
listbox = [15, 46, 78, 24, 56]
def min_max(data):
mi = min(data)
ma = max(data)
return mi, ma
a, b = min_max(listbox)
print(a,b)