a = 1 # 성공
a = 1 # 실패
File "<ipython-input-1-0453a5e2fe16>", line 2 a = 1 # 실패 ^ IndentationError: unexpected indent
if a > 1:
print('big')
print('really?')
File "<ipython-input-2-c77db9f95c1e>", line 3 print('really?') ^ IndentationError: unexpected indent
if 조건식1:
statements
elif 조건식2:
statements
elif 조건식3:
statements
else:
statements
score = 90
if score >= 90:
print('Congratulations!!! ')
Congratulations!!!
a = 10
if a > 5:
print('Big')
else:
print('Small')
Big
a = 10
if a > 5: print('Big')
else: print('Small')
Big
n = -2
if n > 0:
print('Positive')
elif n < 0:
print('Negative')
else:
print('Zero')
Negative
order = 'spagetti'
if order == 'spam':
price = 500
elif order == 'ham':
price = 700
elif order == 'egg':
price = 300
elif order == 'spagetti':
price = 900
print(price)
900
order = 'spagetti'
menu = {'spam':500, 'ham':700, 'egg':300, 'spagetti':900}
price = menu[order]
print(price)
900
a = ['cat', 'cow', 'tiger']
for x in a:
print(len(x), x)
3 cat 3 cow 5 tiger
for x in [1,2,3]:
print(x, end=" ")
1 2 3
print(range(10))
print(list(range(10)))
for x in range(10):
print(x, end=" ")
range(0, 10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 0 1 2 3 4 5 6 7 8 9
sum = 0
for x in range(1, 11):
sum = sum + x
print(sum)
55
prod = 1
for x in range(1, 11):
prod = prod * x
print(prod)
3628800
l = ['cat', 'dog', 'bird', 'pig']
for k, animal in enumerate(l):
print(k, animal)
0 cat 1 dog 2 bird 3 pig
t = ('cat', 'dog', 'bird', 'pig')
for k, animal in enumerate(t):
print(k, animal)
0 cat 1 dog 2 bird 3 pig
d = {'c':'cat', 'd':'dog', 'b':'bird', 'p':'pig'}
for k, key in enumerate(d):
print(k, key, d[key])
0 c cat 1 d dog 2 b bird 3 p pig
for x in range(10):
if x > 3:
break
print(x)
print('done')
0 1 2 3 done
for x in range(10):
if x < 8:
continue
print(x)
print('done')
8 9 done
for x in range(10):
print(x, end=" ")
else:
print('else block')
print('done')
0 1 2 3 4 5 6 7 8 9 else block done
for x in range(10):
break
print(x, end=" ")
else:
print('else block')
print('done')
done
for x in range(2, 4):
for y in range(2, 10):
print(x, '*', y, '=', x*y)
print()
2 * 2 = 4 2 * 3 = 6 2 * 4 = 8 2 * 5 = 10 2 * 6 = 12 2 * 7 = 14 2 * 8 = 16 2 * 9 = 18 3 * 2 = 6 3 * 3 = 9 3 * 4 = 12 3 * 5 = 15 3 * 6 = 18 3 * 7 = 21 3 * 8 = 24 3 * 9 = 27
for x in range(2, 4):
for y in range(2, 10):
print("{0} * {1} = {2}".format(x, y, x * y))
print()
2 * 2 = 4 2 * 3 = 6 2 * 4 = 8 2 * 5 = 10 2 * 6 = 12 2 * 7 = 14 2 * 8 = 16 2 * 9 = 18 3 * 2 = 6 3 * 3 = 9 3 * 4 = 12 3 * 5 = 15 3 * 6 = 18 3 * 7 = 21 3 * 8 = 24 3 * 9 = 27
count = 1
while count < 11:
print(count)
count = count + 1
1 2 3 4 5 6 7 8 9 10
sum = 0
a = 0
while a < 10:
a = a + 1
sum = sum + a
print(sum)
55
x = 0
while x < 10:
print(x, end=" ")
x = x + 1
else:
print('else block')
print('done')
0 1 2 3 4 5 6 7 8 9 else block done
def add(a, b):
return a + b
print(add(3, 4))
print(add([1,2,3], [4,5,6]))
7 [1, 2, 3, 4, 5, 6]
c = add(10, 30)
print(c)
40
f = add
print(f(4, 5))
print(f)
print(f is add)
9 <function add at 0x1037c01e0> True
def simple():
pass
print(simple())
None
def add(a, b):
return a + b
def myabs(x):
if x < 0:
x = -x
return x
def addabs(a, b):
c = add(a, b)
return myabs(c)
print(addabs(-5, -7))
12
def minus(a, b):
return a - b
print(minus(a=12, b=20))
print(minus(b=20, a=12))
-8 -8
def incr(x, y=1):
return x + y
print(incr(5))
print(incr(5, 10))
6 15
def calc(x, y):
return x + y, x - y, x * y, x / y
print(calc(10, 2))
(12, 8, 20, 5.0)
def add(a, b):
return a + b
c = add(1, 3.4)
d = add('dynamic', 'typing')
e = add(['list'], ['and', 'list'])
print(c)
print(d)
print(e)
4.4 dynamictyping ['list', 'and', 'list']
재귀(Recursive) 함수: 함수 몸체에서 자기 자신을 호출하는 함수
1부터 N까지 더하는 재귀 함수
def sum(N):
if N == 1: # 종결 조건
return 1 # 종결 조건이 만족할 때의 반환 값
return N + sum(N-1) # 재귀 호출
print(sum(10))
55
참고 문헌: 파이썬(열혈강의)(개정판 VER.2), 이강성, FreeLec, 2005년 8월 29일