숫자형
더보기
절댓갓 리턴 - abs
abs(-1) #1
반올림 - round
round(4.1) #4
round(4.5) #4
round(4.6) #5
합산 - sum
sum([5, 5, 5]) #15
제곱 - pow
pow(2, 4) #16
pow(3, 3) #27
문자의 유니코드 숫자 값 리턴 - ord
ord("a") #97
유니코드 숫자 값을 입력받아 해당 문자를 리턴 - chr
chr(97) #a
최댓값 - max
max([1, 2, 3]) #3
최솟값 - min
min([1, 2, 3]) #1
문자열
더보기
문자열로 변환 - str
str(3) #'3'
위치 알려 주기 - find, index
find, 값 없을시 -1 반환
a = "Python is the best choice"
a.find("b") #14
a.find("k") #-1
index, 값 없을 시 에러
a.index("b") #14
# a.index("k")
문자열 삽입 - join
",".join(["join", "the", "string"]) #join,the,string
소문자를 대문자로 바꾸기 - upper
a = "hi"
a.upper() #HI
대문자를 소문자로 바꾸기 - lower
a = "HI"
a.lower() #hi
양쪽 공백 지우기 - strip
test = " hi "
test.strip() #"hi"
문자열 바꾸기 - replace
a = "Life is too short"
# (바뀔문자열, 바꿀문자열)
a.replace("Life", "Your leg") #Your leg is too short
문자열 나누기 - split
a = "Life is too short"
# 공백을 기준으로 문자열 나눔
a.split() #['Life', 'is', 'too', 'short']
리스트
더보기
리스트에 요소 추가하기 - append
a = [1, 2, 3]
a.append(4) #[1, 2, 3, 4]
a.append([5, 6]) #[1, 2, 3, 4, [5, 6]]
리스트에 요소 삽입 - insert
a = [1, 2, 3]
# (삽입할 위치, 삽입할 값)
a.insert(0, 4) #[4, 1, 2, 3]
리스트 요소 제거 = remove
a = [1, 2, 3, 4, 1, 2, 3]
# 첫 번째로 나오는 3을 제거
a.remove(3) #[1, 2, 4, 1, 2, 3]
# 3을 재 제거하면 다음 3이 제거됨
a.remove(3) #[1, 2, 4, 1, 2]
리스트 요소 뽑기 - pop
a = [1, 2, 3]
# 맨 마지막 요소를 리턴하고 그 요소는 삭제
b = a.pop()
a #[1, 2]
b #3
리스트에 포함된 요소 개수 세기 - count
a = [1, 2, 3, 4, 1, 2]
a.count(1) #2
리스트 확장 - extend
a = [1, 2, 3]
# (리스트만 넣을 수 있음)
a.extend([4, 5]) #[1, 2, 3, 4, 5]
b = [7, 8]
a.extend(b) #[1, 2, 3, 4, 5, 7, 8]
딕셔너리
더보기
key로 value 얻기 - get
test = {1 : "a", 2 : "b", 3 : "c"}
test.get(2) #"b"
존재하지 않은 key로 value 얻기 실행 차이
test["4"] #KeyError: '4'
test.get(4) #None
딕셔너리에 해당 key 가 존재하는지 체크 - in
test = {"1" : "a", "2" : "b", "3" : "c"}
"1" in test #True
1 in test #False
key 로만 구성된 리스트 생성 - keys
test = {1 : "a", 2 : "b", 3 : "c"}
test.keys() #dict_keys([1, 2, 3])
test 딕셔너리의 key 만 모아서 dict_keys 객체를 리턴
test = {1 : "a", 2 : "b", 3 : "c"}
for i in test.keys() :
print(i) #1 2 3
for문을 이용하여 객체 출력
list(test.keys()) #[1, 2, 3]
list 로 변환하여 출력
value 로만 구성된 리스트 생성 - values
test = {1 : "a", 2 : "b", 3 : "c"}
test.values() #dict_values(['a', 'b', 'c'])
(key, value) 쌍으로 묶기 - items
test = {1 : "a", 2 : "b", 3 : "c"}
test.items() #dict_items([(1, 'a'), (2, 'b'), (3, 'c')])
list(test.items()) #[(1, 'a'), (2, 'b'), (3, 'c')]
딕셔너리 항목 모두 지우기 - clear
test.clear()
'python' 카테고리의 다른 글
| 딕셔너리 자료형 (0) | 2025.05.30 |
|---|---|
| range() (0) | 2025.05.30 |
| datetime - strftime() (0) | 2025.05.29 |
| 표준 라이브러리 (0) | 2025.05.27 |
| 문자열 자료형 - f 문자열 포매팅 (0) | 2025.05.16 |