본문 바로가기
Language/Python

[Python] str / function 이용하기 (+args/kwargs)

by 별토끼. 2017. 8. 8.
반응형


[Python] str / function 이용하기



  • Str 이용하기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#-*- coding: utf-8 -*-
 
# str type 데이터를 만들어서 참조 값을 변수에 담기
 
myComment = "abcdeee"
 
print "id :", id(myComment) # 아이디값 확인
print u"길이:"len(myComment) #문자열의 길이 확인
print u"e 의 포함 횟수:", myComment.count("e")
print u"시작하는 글자 확인:", myComment.startswith("a")
 
name1=u"김구라"
name2=u"이정호"
name3=u"김구라"
 
print "name1 id:",id(name1)
print "name2 id:",id(name2)
print "name3 id:",id(name3)
 
isEqual=name1==name2
isEqual2=name1==name3
 
print "name1 == name2 :", isEqual
print "name1 == name3 :", isEqual2
 
cs


* 출력결과



  • Function 이용하기
1.
function의 개념
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#-*- coding: utf-8 -*-
 
# test1 이라는 이름의 빈 함수 만들기
 
def test1():
    pass
 
# test2 라는 이름의 함수 만들기
def test2():
    print u"test2 함수가 호출됨"
    
test2()
 
# 함수의 인자를 1개 전달 받는 함수
def test3(a):
    print u"전달받은 내용:", a
    
test3(u"김구라")
test3(999)
 
# 함수의 인자를 2개 전달 받는 함수
def test4(arg1, arg2):
    print "arg1:",arg1
    print "arg2:",arg2
    
print test4(u"하나",u"두울")
print u"Step03_Function 모듈의 실행 순서가 종료 됩니다."
 
# None을 리턴하는 3개의 함수
def test5():
    print "test5()"
    
def test6():
    print "test6()"
    return
 
def test7():
    print "test7()"
    return None
 
#리턴값 없으려면 위 3개처럼 
#파이썬의 none은 javascript의 undefine, java의 null
 
# unicode type을 리턴하는 함수
def test8():
    print "test8()"
    return u"김구라"
 
result1 = test5()
result2 = test6()
result3 = test7()
result4 = test8()
 
print "result1:", result1
print "result2:", result2
print "result3:", result3
print "result4:", result4
cs


* 출력 결과


2.

function 에서의 args 개념


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#-*- coding: utf-8 -*-
 
'''
    파이썬 에서는 함수에 전달받는 인자의 갯수를 
    자유롭게 지정할 수 있다.
  
  *args
  - args 는 arguments(인자들) 의 약자
  - args 대신에 다른 변수명을 쓸 수 도 있지만 관례상 args 라고 한다.
'''
# 변수 선언할 때 *를 붙여주면 
# tuple type : list의 read only 버전
def test(*args):
    # args 는 list type의 read only version 인 tuple type
    print "args type:", type(args)
    print args
 
test()
test(10)
test(10,20,30)
test("a","b","c")
 
print "-----------"
 
def test2(a, *args):
    print a, args
 
# test2() #오류발생 인자는 최소 1개는 전달해야 한다.
test2("aaa")
# "bbb" 는  a라는 변수에 담기고 10, 20 , 30은 tuple로 args에 전달됨
test2("bbb"102030)
 
print u"Step03_Function2 모듈이 종료 됩니다."
cs


* 출력결과


3.

function 에서 default 값 지정하기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#-*- coding: utf-8 -*-
 
 
# 함수에 전달받는 인자의 default 값을 지정할 수도 있다.
 
def test(num=0):
    print "num:", num
    
test()
test(999)
print"----------"
def test2(num=0, name=u"아무게", isMan=True):
    print "num:", num
    print "name:", name
    print "isMan:", isMan
    
test2()
print"----------"
test2(1,u"오",False)
print"----------"
test2(2,u"해골",True)
cs


* 출력결과


4.

문자열 format만들기 및 kwargs (keyword arguments) 전달받기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#-*- coding: utf-8 -*-
 
# 문자열 format 만들기
 
result = u"번호:{} 이름:{} 주소:{}".format(1,u"김구라", u"노량진")
 
print result
 
print "-------"
 
'''
    keyword arguments 를 함수에 전달 받을수도 있다
    
    **kwargs
    
    - kwargs 는 keyword arguments 의 약자이다.
'''
 
def test(**kwargs):
    print "kwargs type:", type(kwargs)
    print kwargs
 
test()
test(num=1)
test(num=1, name=u"김구라", isMan=True)
 
 
 
 
cs


* 출력결과


반응형

댓글