Skip to content
Snippets Groups Projects
Commit f245abcb authored by KimMinSeob's avatar KimMinSeob
Browse files

Upload New File

parent f124450b
No related branches found
No related tags found
No related merge requests found
Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 03:13:28)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license()" for more information.
>>> def hello():
print("hello world!")
>>> hello()
hello world!
>>> def abs(arg):
if(arg<0):
result = arg*-1
else:
result = arg
return result
>>> value=abs(10)
>>> value
10
>>> value =abs(-100)
>>> value
100
>>> def print_string(text,count):
for i in range(count):
print(text)
>>> print_string("hi",5)
hi
hi
hi
hi
hi
>>> def print_string(text,count=1):
for i in range(count):
print(text)
>>> #위의 함수를 호출할때 count값을 정해주지 않으면 자동으로 1로 할당된다.
>>> print_string('hi',3)
hi
hi
hi
>>> print_string('hi')
hi
>>> def merge_string(*text_list):
result = ''
for s in text_list:
result= result + s
return result
>>> merge_string('father','go','room')
'father'
>>> def merge_string(*text_list):
for s in text_list:
result= result + s
return result
>>> merge_string('father','go','room')
Traceback (most recent call last):
File "<pyshell#36>", line 1, in <module>
merge_string('father','go','room')
File "<pyshell#35>", line 4, in merge_string
result= result + s
UnboundLocalError: local variable 'result' referenced before assignment
>>> def merge_string(*text_list):
result=''
for s in text_list:
result= result + s
return result
>>> merge_string('아버지가','방에','들어가신다.')
'아버지가'
>>> def merge_string(*text_list):
result = ''
for s in text_list:
result = result + s
return result
>>> merge_string('father','goto','room')
'fathergotoroom'
>>> #return의 위치 중요
>>> def print_team(**players):
for k in players.key():
print('{0} = {1}'.format(k,players[k]))
>>> print_team(k='gk', r = 'fw', a= 'mf, p = 'df')
SyntaxError: invalid syntax
>>> print_team(k='gk', r = 'fw', a= 'mf', p = 'df')
Traceback (most recent call last):
File "<pyshell#48>", line 1, in <module>
print_team(k='gk', r = 'fw', a= 'mf', p = 'df')
File "<pyshell#46>", line 2, in print_team
for k in players.key():
AttributeError: 'dict' object has no attribute 'key'
>>> def print_team(**players):
for k in players.keys():
print('{0} = {1}'.format(k,players[k]))
>>> print_team(k='gk', r = 'fw', a= 'mf', p = 'df')
k = gk
r = fw
a = mf
p = df
>>> def print_args(*argv,argc):
for i in range(argc):
print(argv[i])
SyntaxError: expected an indented block
>>> def print_args(*argv,argc):
for i in range(argc):
print(argv[i])
>>> print_args("a","b","c",argc=3)
a
b
c
>>> print_args("a","b","c",argc=2)
a
b
>>> #가변 매개변수와 일반 매개변수를 모두 사용할때는 일반매개변수가 가변매개변수 뒤에 와야한다.
>>> def multiply(a,b):
return a*b
>>> result = multiply(2,3)
>>> result
6
>>> def my_abs(arg):
if arg<0:
return arg*-1
else:
return arg
>>> result = my_abs(-50)
>>> result
50
>>> result = my_abs(50)
>>> result
50
>>> #none을 반환하는 경우
>>> def my_abs(arg):
if arg<0:
return arg*-1
elif arg>0:
return arg
>>> result = my_abs(0)
>>> result
>>> result == None
True
>>> type(result)
<class 'NoneType'>
>>> def ogamdo(num):
for i in range(1,num+1):
print("the {0}'s ahae".format(i))
if i==5:
return
>>> ogamdo(5)
the 1's ahae
the 2's ahae
the 3's ahae
the 4's ahae
the 5's ahae
>>> ogamdo(8)
the 1's ahae
the 2's ahae
the 3's ahae
the 4's ahae
the 5's ahae
>>> #위의 return은 함수 종료의 의미로 사용되었다.
>>> def print_something(*args):
for s in args:
print(s)
>>> print_something(1,2,3)
1
2
3
>>> print_something(1,2,3,4,5)
1
2
3
4
5
>>> #함수 밖의 변수와 함수 안의 변수
>>> def scope_test():
a=1
print('a:{0}'.format(a))
>>> a=0
>>> scope_test()
a:1
>>> print(a)
0
>>> #함수안에서의 a는 1이지만 밖에서는 0 이다.
>>> def scope_test():
global a
a=1
print('a:{0}'.format(a))
>>> a=0
>>> scope_test()
a:1
>>> print(a)
1
>>> #재귀함수
>>> def factorial(n):
if n == 0:
return 1
elif n>0:
return factorial(n-1)*n
>>> factorial(5)
120
>>> factorial(10)
3628800
>>>
>>>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment