Python Basic _ 8-1. 파이썬 내장함수

WONY_yoon·2025년 10월 9일
post-thumbnail

Python Built-in 함수 알아보기

# Chapter 08-1
# 파이썬 내장(Built-in) 함수
# 자주 사용하는 함수 위주로 실습
# str(), int(), tuple() 형변환은 이미 학습함

# 절댓값
print(abs(-3))  # 3

print()

# all, any : iterable 요소 검사 (and / or)
print(all([1, 2, '']))  # False (빈 문자열은 False로 간주)
print(all([3, 4, 5]))   # True (모두 참)
print(any([1, 2, 0]))   # True (하나라도 참)

print()

# chr : 아스키 -> 문자 / ord : 문자 -> 아스키
print(chr(67))   # 'C'
print(ord('T'))  # 84

# enumerate : index + iterable 객체 생성
for i, name in enumerate(['abc', 'bcd', 'cdf', 'def']):
    print(i, name)

print()

# filter : 반복 가능한 객체에서 조건에 맞는 값만 추출
def conv_pos(x):
    return abs(x) > 2

print(list(filter(conv_pos, [1, 2, 3, -1, -2, -3])))
print(list(filter(lambda x: abs(x) > 2, [1, 2, 3, -1, -2, -3])))

# id : 객체의 주소값(참조값) 반환
print(id(int(5)))
print(id(4))

# len : 요소의 길이 반환
print(len('abcdefgh') - 1)
print(len([1, 2, 3, 4, 5, 6, 7]))

# max, min : 최댓값, 최솟값
print(max([1, 2, 3]))
print(max('python study'))
print(min([1, 2, 3]))
print(min('python_study'))

print()

# map : 반복 가능한 객체의 각 요소에 함수를 적용한 결과 반환
def conv_abs(x):
    return abs(x)

print(list(map(conv_abs, [1, 2, 3, -1, -2, -3])))
print(list(map(lambda x: abs(x), [1, 2, 3, -1, -2, -3])))

print()

# pow : 제곱값 반환
print(pow(2, 10))  # 1024

# range : 반복 가능한 객체(iterable) 반환
print(list(range(1, 10, 2)))    # [1, 3, 5, 7, 9]
print(list(range(0, -15, -2)))  # [0, -2, -4, -6, -8, -10, -12, -14]

# round : 반올림
print(round(6.5781, 2))  # 6.58
print(round(5.7))        # 6 (기본은 정수로 출력)

# sorted : 반복 가능한 객체를 정렬 후 리스트로 반환
print(sorted([6, 7, 4, 3, 1, 2]))
print(sorted(['p', 'y', 'z', 'p']))
print(sorted(['p', 'y', 't', 'h', 'o', 'n']))

# sum : 반복 가능한 객체의 합 반환
print(sum([6, 7, 8, 9, 10]))
print(sum(range(1, 101)))

# type : 자료형 확인
print(type(3))   # int
print(type({}))  # dict
print(type(()))  # tuple
print(type([]))  # list

# zip : 여러 iterable을 묶어서 튜플 형태로 반환
print(list(zip([10, 20, 30], [40, 50, 777])))
print(type(list(zip([10, 20, 30], [40, 50, 777]))))    # list
print(type(list(zip([10, 20, 30], [40, 50, 777]))[0])) # tuple

0개의 댓글