정수형 int
Python의 정수형은 다양한 진법 변환과 비트 연산을 지원합니다.
# 문자열을 정수로 변환
num_str = "456"
converted = int(num_str)
print(converted) # 456
# 앞에 0이 있는 경우 기본적으로 10진수로 해석
leading_zero = "0077"
result = int(leading_zero)
print(result) # 77
# 특정 진법에서 10진수로 변환
hex_val = "00FF"
oct_val = "0077"
print(int(hex_val, base=16)) # 255
print(int(oct_val, base=8)) # 63
# 이진 비트 길이 확인
val_a = 31 # 11111
val_b = 32 # 100000
print(val_a.bit_length()) # 5
print(val_b.bit_length()) # 6
# 잘못된 변환은 예외 발생
# int("12abc") # ValueError
# int("0b1010") # ValueError (base=2 필요)
문자열 str
Python 문자열은 불변(immutable) 객체로, 풍부한 내장 메서드를 제공합니다.
대소문자 변환
text = "python Programming"
# 첫 글자만 대문자
print(text.capitalize()) # Python programming
# 전체 소문자 변환
print(text.casefold()) # python programming (유니코드 지원)
print(text.lower()) # python programming (ASCII 기반)
# 전체 대문자 변환
print(text.upper()) # PYTHON PROGRAMMING
# 대소문자 스왑
print(text.swapcase()) # PYTHON pROGRAMMING
정렬과 패딩
word = "code"
# 중앙 정렬
print(word.center(20)) # " code "
print(word.center(20, "-")) # "-------code-------"
# 좌우 정렬
print(word.ljust(15, "*")) # "code***********"
print(word.rjust(15, "*")) # "***********code"
# 앞쪽 0 채우기
num_str = "42"
print(num_str.zfill(8)) # "00000042"
검색과 조회
sentence = "programming is fun programming"
# 위치 찾기 (없으면 -1 반환)
print(sentence.find("gram")) # 3
print(sentence.find("gram", 10)) # 18
print(sentence.find("xyz")) # -1
# 위치 찾기 (없으면 예외 발생)
# sentence.index("xyz") # ValueError
# 에서부터 검색
print(sentence.rfind("pro")) # 20
# 포함 횟수
print(sentence.count("pro")) # 2
print(sentence.count("m", 5, 15)) # 2 (슬라이스 범위)
검증 메서드
print("Python3".isalnum()) # True (문자+숫자)
print("Python".isalpha()) # True (문자만)
print("2024".isdigit()) # True (숫자)
print("2024".isdecimal()) # True (10진수 숫자)
print("Ⅷ".isnumeric()) # True (로마 숫자 등)
print(" \t\n".isspace()) # True (공백 문자)
print("Title Case".istitle()) # True (단어 첫 글자 대문자)
print("UPPER".isupper()) # True
print("lower".islower()) # True
# 식별자 확인
print("var_name".isidentifier()) # True
print("2nd_var".isidentifier()) # False
문자열 분리와 결합
# 분할
path = "/usr/local/bin"
print(path.partition("/")) # ('', '/', 'usr/local/bin')
print(path.split("/")) # ['', 'usr', 'local', 'bin']
print(path.rsplit("/", 1)) # ['/usr/local', 'bin']
# 줄 단위 분리
multiline = "line1\nline2\nline3"
print(multiline.splitlines()) # ['line1', 'line2', 'line3']
print(multiline.splitlines(True)) # ['line1\n', 'line2\n', 'line3']
# 결합
chars = ["P", "y", "t", "h", "o", "n"]
print("".join(chars)) # Python
print("-".join(chars)) # P-y-t-h-o-n
치환과 정리
# 치환
quote = "to be or not to be"
print(quote.replace("be", "code")) # to code or not to code
print(quote.replace("be", "code", 1)) # to code or not to be
# 앞뒤 제거
messy = " \t hello world \n "
print(messy.strip()) # "hello world"
print(messy.lstrip()) # "hello world \n "
print(messy.rstrip()) # " \t hello world"
# 특정 문자 제거
data = "xx--data--xx"
print(data.strip("x-")) # "data"
# 탭 확장
tabbed = "col1\tcol2\tcol3"
print(tabbed.expandtabs(8))
포맷팅
# 위치 기반
template = "{} + {} = {}"
print(template.format(2, 3, 5)) # 2 + 3 = 5
# 이름 기반
template = "Language: {lang}, Version: {ver}"
print(template.format(lang="Python", ver=3.12))
# 딕셔너리 언패킹
data = {"lang": "Python", "ver": 3.12}
print(template.format(**data))
# f-string (권장)
lang, ver = "Python", 3.12
print(f"Language: {lang}, Version: {ver:.2f}")
슬라이싱과 인덱싱
alphabet = "ABCDEFGHIJ"
# 인덱싱
print(alphabet[0]) # A
print(alphabet[-1]) # J
# 슬라이싱 [시작:끝:간격]
print(alphabet[2:5]) # CDE
print(alphabet[:4]) # ABCD
print(alphabet[6:]) # GHIJ
print(alphabet[::2]) # ACEGI
print(alphabet[::-1]) # JIHGFEDCBA
# 길이
print(len(alphabet)) # 10
불리언 bool
# 참/거짓 변환
print(bool(42)) # True
print(bool(0)) # False
print(bool("")) # False
print(bool([])) # False
print(bool({})) # False
print(bool(None)) # False
# 조건에서의 활용
value = ""
if not value: # 빈 문자열은 False
print("Empty!")
리스트 list
리스트는 가변(mutable) 시퀀스로, 다양한 타입의 요소를 저장할 수 있습니다.
생성과 접근
# 다양한 요소 타입
mixed = [42, "text", [1, 2, 3], {"key": "value"}, (5, 6)]
# 인덱싱과 중첩 접근
print(mixed[2]) # [1, 2, 3]
print(mixed[2][1]) # 2
print(mixed[3]["key"]) # value
# 슬라이싱
nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(nums[2:7]) # [2, 3, 4, 5, 6]
print(nums[::2]) # [0, 2, 4, 6, 8]
print(nums[::-1]) # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
수정과 삭제
items = [10, 20, 30, 40, 50]
# 단일 수정
items[2] = 99
print(items) # [10, 20, 99, 40, 50]
# 슬라이스 수정
items[1:3] = [200, 300, 400]
print(items) # [10, 200, 300, 400, 40, 50]
# 삭제
del items[0]
print(items) # [200, 300, 400, 40, 50]
del items[1:3]
print(items) # [200, 40, 50]
주요 메서드
data = [3, 1, 4, 1, 5]
# 추가
data.append(9) # 끝에 추가
print(data) # [3, 1, 4, 1, 5, 9]
data.extend([2, 6]) # 여러 요소 추가
print(data) # [3, 1, 4, 1, 5, 9, 2, 6]
data.insert(0, 0) # 특정 위치 삽입
print(data) # [0, 3, 1, 4, 1, 5, 9, 2, 6]
# 제거
print(data.pop()) # 6 (마지막 제거 및 반환)
print(data.pop(0)) # 0 (인덱스 0 제거 및 반환)
data.remove(1) # 값 1 제거 (첫 번째만)
print(data) # [3, 4, 1, 5, 9, 2]
# 정렬과 뒤집기
data.sort()
print(data) # [1, 2, 3, 4, 5, 9]
data.sort(reverse=True)
print(data) # [9, 5, 4, 3, 2, 1]
data.reverse()
print(data) # [1, 2, 3, 4, 5, 9]
# 기타
print(data.count(3)) # 1
print(data.index(4)) # 3
print(data.copy()) # 얕은 복사
data.clear() # 전체 삭제
리스트와 문자열 변환
# 문자열 → 리스트
text = "hello"
print(list(text)) # ['h', 'e', 'l', 'l', 'o']
# 리스트 → 문자열 (숫자 포함 시)
numbers = [10, 20, 30, "end"]
result = "".join(str(x) for x in numbers)
print(result) # "102030end"
# 또는 map 사용
result = "".join(map(str, numbers))
튜플 tuple
튜플은 불변(immutable) 시퀀스로, 리스트와 유사하지만 수정이 불가능합니다.
# 생성 (쉼표가 중요)
single = (42,) # 튜플
not_tuple = (42) # 정수
# 다양한 요소
record = (100, "kim", [1, 2], (3, 4), True)
# 접근 (리스트와 동일)
print(record[1]) # kim
print(record[1:3]) # ('kim', [1, 2])
print(record[-1]) # True
# 중첩 수정 가능 (내부 가변 객체)
record[2][0] = 99
print(record) # (100, 'kim', [99, 2], (3, 4), True)
# 튜플 자체는 불가능
# record[0] = 200 # TypeError
# 변환
print(tuple([1, 2, 3])) # (1, 2, 3)
print(list((1, 2, 3))) # [1, 2, 3]
# 메서드
print(record.count(100)) # 1
print(record.index("kim")) # 1
딕셔너리 dict
키-값 쌍으로 구성된 해시 테이블 기반 자료구조입니다.
생성과 접근
# 생성
user = {
"id": 1001,
"name": "lee",
"active": True,
"scores": [85, 90, 78],
"address": {"city": "Seoul", "zip": "06234"}
}
# 키로 접근
print(user["name"]) # lee
print(user.get("phone")) # None (키 없음)
print(user.get("phone", "N/A")) # N/A (기본값)
# 존재 확인
print("email" in user) # False
수정과 삭제
# 추가/수정
user["email"] = "lee@email.com"
user["scores"].append(92)
# 삭제
removed = user.pop("active") # 키 제거 및 값 반환
print(removed) # True
# 임의 제거 (Python 3.7+ 순서 유지)
key, value = user.popitem()
print(f"Removed: {key} = {value}")
# 전체 삭제
# user.clear()
순회와 조회
# 키 순회
for key in user:
print(key)
# 값 순회
for value in user.values():
print(value)
# 키-값 순회
for key, value in user.items():
print(f"{key}: {value}")
# 뷰 객체
print(user.keys())
print(user.values())
print(user.items())
업데이트와 기본값
# 여러 키 업데이트
user.update({"level": 5, "points": 1200})
user.update(status="online", last_login="2024-01-15")
# 기본값 설정 (없을 때만)
user.setdefault("nickname", "unknown")
user.setdefault("name", "default") # 기존 값 유지
# fromkeys로 일괄 생성
keys = ["a", "b", "c"]
print(dict.fromkeys(keys, 0)) # {'a': 0, 'b': 0, 'c': 0}
집합 set
중복 없는 해시 가능한 요소의 모음입니다.
생성과 특성
# 자동 중복 제거
unique = {3, 1, 4, 1, 5, 9, 2, 6}
print(unique) # {1, 2, 3, 4, 5, 6, 9} (순서 없음)
# 순서 없음
for item in {"apple", "banana", "cherry"}:
print(item, end=" ") # 실행마다 순서 다름
# 변환
print(set("hello")) # {'h', 'e', 'l', 'o'}
print(set([1, 2, 2, 3])) # {1, 2, 3}
집합 연산
a = {1, 2, 3, 4, 5}
b = {4, 5, 6, 7, 8}
# 교집합
print(a & b) # {4, 5}
print(a.intersection(b))
# 합집합
print(a | b) # {1, 2, 3, 4, 5, 6, 7, 8}
print(a.union(b))
# 차집합
print(a - b) # {1, 2, 3}
print(a.difference(b))
# 대칭차집합
print(a ^ b) # {1, 2, 3, 6, 7, 8}
print(a.symmetric_difference(b))
# 관계 검사
print({1, 2}.issubset(a)) # True
print(a.issuperset({1, 2})) # True
print(a.isdisjoint({10, 20})) # True
수정 메서드
s = {1, 2, 3}
# 추가
s.add(4)
s.update([5, 6], (7, 8)) # 여러 요소 추가
# 제거
s.remove(4) # 없으면 KeyError
s.discard(10) # 없어도 무시
popped = s.pop() # 임의 요소 제거 및 반환
# 복사와 비교
s2 = s.copy()
print(s == s2) # 값 비교
print(s is s2) # 동일성 비교
# 불변 집합
frozen = frozenset([1, 2, 3])
# frozen.add(4) # AttributeError
가변성과 불변성
# 불변 타입: 수정 시 새 객체 생성
x = "hello"
print(id(x)) # 140012345678
x += " world"
print(id(x)) # 140012345999 (다른 주소)
y = 100
print(id(y))
y += 1
print(id(y)) # 다른 주소
# 가변 타입: 수정 시 동일 객체
lst = [1, 2, 3]
print(id(lst)) # 140012346000
lst.append(4)
print(id(lst)) # 140012346000 (동일 주소)
d = {"a": 1}
print(id(d))
d["b"] = 2
print(id(d)) # 동일 주소