코린이의 기록

ing [python] 파이썬 내장함수 본문

Python

ing [python] 파이썬 내장함수

코린이예요 2018. 9. 13. 18:38
반응형
# 함수

# 2진수 8진수 16진수 변환
print(int(bin(10), 2))
print(int(oct(10), 8))
print(int(hex(10), 16))

print("{:b}".format(10))
print("{:o}".format(10))
print("{:x}".format(10))

# ascii
print(chr(97))
print(chr(65))
print(chr(54620))
print(ord("한"))


# dir
# 객체 멤버 목록을 반환한다.
print(dir("a"))

# divmod
print(4//3)
print(4%3)
# 몫과 나머지를 튜플형태로 나타내줌 (정수형 나눗셈만 함)
print(divmod(8,3))

# enumerate
# 객체 요소의 일련 번호를 반환한다.
for i, entry in enumerate ([1, 2, 3, 4], 1):
print(i, entry)

# eval -> 보안상의 이유로 ? 쓰지 않는것을 권장함
# 문자열을 받아서 평가해줌
print(eval("1+2"))

# help
help(print) # print 함수에 대한 도움말을 얻을 수 있다.

# isinstance
a = "apple"
print(isinstance(a, str))

# b = [1, 2, 3, 4]
# a_iter = iter(b)
# print(a.next(a_iter))
# print(a.next(a_iter))
# print(a.next(a_iter))
# print(a.next(a_iter))

print("1", "2", "3", "4", "5")
# 개행을 하지 않을때
print("1", "2", "3", "4", "5", end="")
print("1", "2", "3", "4", "5")
# file에 출력
#print("1", "2", "3", "4", "5", file=open("stdout.txt"))

print(range(1, 11))
print(repr(range(1, 11)))
print(sum(range(1,1000)))


# type 구하기
a = 1000000000000000000000
print(type(a))

# zip
a = (1, 2, 3, 4, 5)
b = (6, 7, 8, 9 , 10)
c = ( 6, 7, 8, 9, 11)
for item1, item2, item3 in zip(a, b, c ):
print(item1, item2, item3)

# 함수
def hello(a, b, c):
return None
print(hello(1, 2, 3))

def hello(a, b, c=3):
return None
print(hello(1, 2))

# python에서 오버로딩을 하려면 매개변수앞에 *을 붙여서 무한정으로 받을 수 있도록 한다.
print("function")
def hello(a, b, c, d, *e):
print(e) # 튜플 형태로 출력된다.
return None
hello(1,2,3,4)
hello(1,2,3,4,5)
hello(1,2,3,4,5,6)

# 경로 조합
def hello(*e):
return "\\".join(e)
print(hello("Windows", "system32", "drivers", "etc", "hosts"))
# 운영체제에 따른 경로 조합
import os
def hello(*e):
return os.sep.join(e)
print(hello("Windows", "system32", "drivers", "etc", "hosts"))

# 키워드 인자전달방식
def hello(a, b, c=1, d=2, e=3, f=4):
print(a, b, f)
return
print(hello(1,2, f=10))

# 가변 키워드 인자 전달 방식
def hello(a, b, c=1, d=2, e=3, f=4, **kwargs):
print(kwargs['k'])
return
print(hello(b=2, a=1, f=10, k=40, z=50, w=80))

def hello(a, b, c=1, *d):
print(d)
hello(3,4)
hello(3,4,7,8,9)
#hello(3, 4, z='k') -> c 건너띄고 d에 인자 넣는방법이라는데 왜 안됌?

def hello(a, b):
print(a, b)
pass
a = (1,2)
hello(*a) # 언패킹 방식 a와 b에 각각 1, 2가 들어감


a = {'a': 10, 'b' : 20}
hello(**a); # 사전타입 변수 전달할때에는 *가 반드시 두개


반응형

'Python' 카테고리의 다른 글

[Python] 파이썬 반복문  (0) 2018.09.13
[Python] 파이썬 조건식  (0) 2018.09.13
[Python] 파이썬 변수와 표현식  (0) 2018.09.13
Comments