Introduction to Python II (Python Basics II)

Introduction to Python II (Python Basics II)

2019, Jan 04    
02_2

Introduction to Python II (Python Basics)

by JunPyo Park

Part of the Alpha Square Lecture Series:


파이썬 κΈ°μ΄ˆμ™€ 자료ꡬ쑰, λ¬Έμžμ—΄, 쑰건문, 반볡문 그리고 ν•¨μˆ˜μ— λŒ€ν•΄μ„œ κ°„λ‹¨ν•˜κ²Œ 정리해 λ³΄μ•˜μŠ΅λ‹ˆλ‹€. μ£Όν”Όν„° λ…ΈνŠΈλΆ ν™˜κ²½μ—μ„œ μž‘μ„±ν•œ λ¬Έμ„œμ΄κΈ° λ•Œλ¬Έμ— μ£Όν”Όν„° λ…ΈνŠΈλΆμ„ 켜고 λ”°λΌμ„œ μ‹€μŠ΅ν•΄ λ³΄μ‹œλ©΄ μ‰½κ²Œ μ΄ν•΄ν•˜μ‹€μˆ˜ μžˆμŠ΅λ‹ˆλ‹€.

문자열 처리하기

μœ„μ—μ„œ μ‚΄νŽ΄ λ³΄μ•˜λ“―μ΄ string νƒ€μž…μ€ ν…μŠ€νŠΈλ₯Ό ν‘œν˜„ν•˜λŠ”λ° 주둜 μ‚¬μš©λ©λ‹ˆλ‹€. μ—¬κΈ°μ„œλŠ” λͺ‡κ°€μ§€ λ‚΄μž₯ν•¨μˆ˜λ“€μ„ μ‚΄νŽ΄λ³΄κ³  ν•„μš”μ— 맞게 λ¬Έμžμ—΄μ„ λ‹€λ£° 수 μžˆλŠ” 방법듀을 μ•Œμ•„λ³΄λ„λ‘ ν•˜κ² μŠ΅λ‹ˆλ‹€.

In [58]:
first_string = 'I am '
second_string = 'a boy'
third_string = first_string + second_string
third_string
Out[58]:
'I am a boy'

슀트링 μžλ£Œν˜• λ˜ν•œ 인덱싱이 λ˜μ–΄ 있기 λ•Œλ¬Έμ—(ordered) 리슀트 μŠ¬λΌμ΄μ‹± 기법듀을 λ˜‘κ°™μ΄ ν™œμš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

In [60]:
my_string = 'Supercalifragilisticexpialidocious'
print('The first letter is: ', my_string[0]) # λŒ€λ¬Έμž S
print ('The last letter is: ', my_string[-1]) # μ†Œλ¬Έμž s
print ('The second to last letter is: ', my_string[-2]) # μ†Œλ¬Έμž u
print ('The first five characters are: ', my_string[0:5]) # Super
print ('Reverse it!: ', my_string[::-1]) # λ¬Έμžμ—΄ μ—­μˆœμœΌλ‘œ 좜λ ₯
The first letter is:  S
The last letter is:  s
The second to last letter is:  u
The first five characters are:  Super
Reverse it!:  suoicodilaipxecitsiligarfilacrepuS

λ‚΄μž₯된 객체(built-in objects)듀은 객체듀과 μ—°κ²°λœ νŠΉλ³„ν•œ ν•¨μˆ˜λ“€μ„ κ°€μ§ˆ 수 μžˆμŠ΅λ‹ˆλ‹€. 이λ₯Ό λ©”μ„œλ“œ(methods)라고 ν•©λ‹ˆλ‹€. λ§ˆμΉ¨ν‘œ(.)λ₯Ό μ‚¬μš©ν•˜μ—¬ λ©”μ„œλ“œμ— μ ‘κ·Όν•  수 μžˆμŠ΅λ‹ˆλ‹€. λ‚˜μ€‘μ— λ‹€λ₯Έ κ°•μ’Œμ—μ„œ μ΄μ—λŒ€ν•΄ μžμ„Ένžˆ 닀루도둝 ν•˜κ² μŠ΅λ‹ˆλ‹€.

  • count() λ©”μ„œλ“œμ„ ν™œμš©ν•˜μ—¬ λ‹€μŒκ³Ό 같이 νŠΉμ • λ¬Έμžμ—΄μ΄ λͺ‡ 번 λ“±μž₯ν•˜λŠ”μ§€ μ…€ 수 μžˆμŠ΅λ‹ˆλ‹€.
In [63]:
print('Count of the letter i in Supercalifragilisticexpialidocious: ', 
      my_string.count('i'))
print('Count of "li" in the same word: ', 
      my_string.count('li'))
Count of the letter i in Supercalifragilisticexpialidocious:  7
Count of "li" in the same word:  3
  • find() λ©”μ„œλ“œλ₯Ό ν™œμš©ν•˜μ—¬ νŠΉμ • 값이 제일 처음 λ‚˜μ˜€λŠ” 곳의 μœ„μΉ˜(인덱슀)λ₯Ό 찾을 수 μžˆμŠ΅λ‹ˆλ‹€.
In [64]:
print('The first time i appears is at index: ', my_string.find('i'))
The first time i appears is at index:  8
  • replace() λ©”μ„œλ“œλ₯Ό ν™œμš©ν•˜μ—¬ λ¬Έμžμ—΄μ„ λΆ€λΆ„μ μœΌλ‘œ μΉ˜ν™˜ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
In [66]:
print("All i's are now a's: ", my_string.replace('i', 'a'))
All i's are now a's:  Supercalafragalastacexpaaladocaous
In [67]:
print("It's raining cats and dogs".replace('dogs', 'more cats'))
It's raining cats and more cats
  • upper() κ³Ό lower() λ©”μ„œλ“œλ₯Ό ν™œμš©ν•˜μ—¬ λ¬Έμžμ—΄μ„ λŒ€λ¬Έμž/μ†Œλ¬Έμž 둜 λ³€κ²½ κ°€λŠ₯ν•©λ‹ˆλ‹€.
In [68]:
my_string = "I can't hear you"
print(my_string.upper())
my_string = "I said HELLO"
print(my_string.lower())
I CAN'T HEAR YOU
i said hello

문자열 포맷팅(String Formatting)

format() λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•˜μ—¬ λ¬Έμžμ—΄μ„ ν¬λ§·νŒ… ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

In [69]:
my_string = "{0} {1}".format('Marco', 'Polo')
print(my_string)
Marco Polo
In [70]:
my_string = "{1} {0}".format('Marco', 'Polo')
print(my_string)
Polo Marco

μ€‘κ΄„ν˜Έ({})λ₯Ό μ‚¬μš©ν•˜μ—¬ λ¬Έμžμ—΄μ΄ μ±„μ›”μ§ˆ κ³³μž„μ„ λ‚˜νƒ€λ‚Ό 수 있고 μ•ˆμ— λ“€μ–΄κ°€λŠ” 숫자λ₯Ό 톡해 λͺ‡ 번째둜 μ±„μ›Œμ§€λŠ” 곳인지 ν‘œκΈ°κ°€ κ°€λŠ₯ν•©λ‹ˆλ‹€.

format()κ³Ό κ΄€λ ¨λœ μžμ„Έν•œ μ„€λͺ…은 곡식 Documentation 을 μ°Έκ³ ν•΄ μ£Όμ„Έμš”.

ν¬λ§·νŒ…μ˜ λ‹€λ₯Έ λ°©λ²•μœΌλ‘œλŠ” λ‹€μŒκ³Ό 같이 %λ₯Ό μ‚¬μš©ν•˜λŠ” 방법이 μžˆμŠ΅λ‹ˆλ‹€.

print('insert %s here' % 'value')

In [71]:
print('There are %s cats in my %s' % (13, 'apartment'))
There are 13 cats in my apartment

μ—¬κΈ°μ„œ %s λŠ” 파이썬 μƒμ—μ„œ μ§€μ •λœ 값을 λ¬Έμžμ—΄λ‘œ λ³€ν™˜ν•˜μ—¬ μ²˜λ¦¬ν•˜λΌλŠ” 의미 μž…λ‹ˆλ‹€. formatting documentation을 ν™•μΈν•˜μ—¬ 좔가적인 μ˜ˆμ œλ“€κ³Ό κΈ°ν˜Έλ“€μ„ 확인 ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

논리 연산자(Logical Operators)

기초 논리(Basic Logic)

논리 μ—°μ‚°μžλŠ” boolean 값듀을 μ²˜λ¦¬ν•˜λŠ” μ—°μ‚°μžλ“€μ„ μ˜λ―Έν•©λ‹ˆλ‹€. boolνƒ€μž…μ€ μ°Έ(True)κ³Ό κ±°μ§“(False) 쀑 ν•˜λ‚˜μ˜ κ°’(λ˜λŠ” $1$μ΄λ‚˜ $0$)만 κ°€μ§ˆ 수 μžˆμŠ΅λ‹ˆλ‹€.

ν•™μ°½μ‹œμ ˆ 'λͺ…μ œ'λž€ μ°Έκ³Ό 거짓을 ꡬ뢄할 수 μžˆλŠ” λ¬Έμž₯ 이라고 λ°°μ› μŠ΅λ‹ˆλ‹€. νŒŒμ΄μ¬μ—μ„œλ„ 이런 μ°Έκ³Ό 거짓을 λͺ…ν™•νžˆ κ΅¬λΆ„λ˜λŠ” ꡬ문(statement)을 == (κ°™λ‹€), != (κ°™μ§€ μ•Šλ‹€), < (μž‘λ‹€), > (크닀), <= (μž‘κ±°λ‚˜ κ°™λ‹€), 그리고 >= (ν¬κ±°λ‚˜ κ°™λ‹€) 와 같은 기호λ₯Ό 톡해 ν‘œν˜„ ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

In [72]:
print(5==5)
True
In [73]:
print(5>5)
False

μ΄λŠ” λ¬Όλ‘  λ³€μˆ˜μ—λ„ 적용이 κ°€λŠ₯ν•©λ‹ˆλ‹€.

In [74]:
m = 2
n = 23
print(m < n)
True

μ°Έμ΄λ‚˜ κ±°μ§“ 값을 κ°–λŠ” 값이 λ‘κ°œ μžˆμ„ λ•Œ, or,and,notκ³Ό 같은 논리 μ—°μ‚°μž(logical operator)λ₯Ό μ •μ˜ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

In [75]:
statement_1 = 10 > 2
statement_2 = 4 <= 6
print("Statement 1 truth value: {0}".format(statement_1))
print("Statement 2 truth value: {0}".format(statement_2))
print("Statement 1 and Statement 2: {0}".format(statement_1 and statement_2))
Statement 1 truth value: True
Statement 2 truth value: True
Statement 1 and Statement 2: True

or μ—°μ‚°μžλŠ” 논리적 or 연산을 μˆ˜ν–‰ν•©λ‹ˆλ‹€. 두가지 statement 쀑 ν•˜λ‚˜λΌλ„ μ°Έ(True)값을 κ°€μ§€κ³  μžˆλ‹€λ©΄ 전체 ꡬ문도 Trueκ°€ λ©λ‹ˆλ‹€. and μ—°μ‚°μžλŠ” 두 statementκ°€ λͺ¨λ‘ True일 경우 μ—λ§Œ True값을 λ°˜ν™˜ν•©λ‹ˆλ‹€. κ·ΈλŸ¬μ§€ μ•Šλ‹€λ©΄ False 값을 λ°˜ν™˜ν•©λ‹ˆλ‹€. not μ—°μ‚°μžλŠ” 뒀에 μžˆλŠ” statement의 μ§„μœ„κ°’(True or False)λ₯Ό 거꾸둜 좜λ ₯ν•˜λŠ” μ—°μ‚°μž μž…λ‹ˆλ‹€. notλ’€μ˜ statementκ°€ True라면 Falseλ₯Ό False라면 Trueλ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.

P와 QλΌλŠ” λ‘κ°œμ˜ logical statementsλ₯Ό κ°€μ§€κ³  μžˆμ„ λ•Œ 각각 κ²½μš°μ— λŒ€ν•œ 연산값은 λ‹€μŒ ν‘œμ™€ 같이 λ‚˜μ˜€κ²Œ λ©λ‹ˆλ‹€.

P Q not P P and Q P or Q
True True False True True
False True True False True
True False False False True
False False True False False
In [76]:
print(((2 < 3) and (3 > 0)) or ((5 > 6) and not (4 < 2)))
True

μœ„μ˜ κ²°κ³Όκ°€ 좜λ ₯λ˜λŠ” 과정을 μ‚΄νŽ΄λ³΄λ©΄ λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

  • (True and True) or (False and not False)
  • True or (False and True)
  • True or False
  • True

진위값(Truthiness)

bool() ν•¨μˆ˜λŠ” μ–΄λ–€ 값을 μ°Έ/κ±°μ§“ ν˜•νƒœλ‘œ λ°”κΎΈμ–΄μ£ΌλŠ” ν•¨μˆ˜ μž…λ‹ˆλ‹€. κ·Έλ ‡λ‹€λ©΄ μ–΄λ–€ κ²½μš°μ— μ°Έ(True)이 λ‚˜μ˜€κ³  μ–΄λ–€ κ²½μš°μ— κ±°μ§“(False)이 λ‚˜μ˜€κ²Œ λ κΉŒμš”? 일반적으둜 string, tuple, dictionaries, lists의 경우 λ‚΄μš©λ¬Όμ΄ ν•˜λ‚˜λΌλ„ λ“€μ–΄μžˆλ‹€λ©΄ μ°Έ 값이 λ°˜ν™˜λ˜κ³  λΉ„μ–΄ μžˆλ‹€λ©΄ κ±°μ§“ 값이 λ°˜ν™˜λ©λ‹ˆλ‹€.

In [78]:
bool('')
Out[78]:
False
In [80]:
bool('I like Python!')
Out[80]:
True
In [81]:
bool([])
Out[81]:
False
In [82]:
bool([1,2,3])
Out[82]:
True
In [83]:
bool(1)
Out[83]:
True
In [85]:
bool(0)
Out[85]:
False
In [86]:
bool(3.141592)
Out[86]:
True

숫자의 경우 0이면 False, κ·Έ μ΄μ™Έμ—λŠ” True κ°€ λ‚˜μ˜΅λ‹ˆλ‹€.

If 구문

νŠΉμ • 쑰건을 λ§Œμ‘±ν•  λ•Œ μ–΄λ–€ μ½”λ“œλ₯Ό μ‹€ν–‰μ‹œν‚€κ³  μ‹Άλ‹€λ©΄ If ꡬ문을 μ‚¬μš©ν•˜μ—¬ 이λ₯Ό κ΅¬ν˜„ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

λ‹€μŒκ³Ό 같은 μ½”λ“œμ—μ„œ ifλ¬Έ μ•ˆμ˜ condition이 μ°Έ 이라면 μ•ˆμ—μžˆλŠ” μ½”λ“œκ°€ μ‹€ν–‰λ©λ‹ˆλ‹€. λ§Œμ•½ condition이 거짓이라면 ifλ¬Έ λ‚΄λΆ€μ˜ μ½”λ“œλŠ” μŠ€ν‚΅λ˜κ³  else ꡬ문이 μžˆλŠ” 곳으둜 κ°€μ„œ λ‚˜λ¨Έμ§€ μ½”λ“œκ°€ μ‹€ν–‰λ˜κ²Œ λ©λ‹ˆλ‹€. λ§Œμ•½ elseꡬ문이 μ—†λ‹€λ©΄ 아무 일도 μΌμ–΄λ‚˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. 이 λ•Œ condition은 μœ„μ—μ„œ 배운 논리 μ—°μ‚°μžλ₯Ό 톡해 ν‘œν˜„λ˜κ±°λ‚˜ μ§„μœ„κ°’μ„ 톡해 μ„€μ • ν•  수 μžˆμŠ΅λ‹ˆλ‹€. if ꡬ문은 콜둠(:)κ³Ό indented text(4-슀페이슀 λ˜λŠ” tab)을 톡해 μ •μ˜ ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

In [89]:
# if ꡬ문의 기초적인 λΌˆλŒ€ μž…λ‹ˆλ‹€.
# μ—¬κΈ°μ„œ "Condition"은 곡백이 μ•„λ‹ˆκΈ° λ•Œλ¬Έμ— 항상 True μž…λ‹ˆλ‹€. 
# μ—¬κΈ°μ„œλŠ” if 문이 μ‚¬μš©λ˜λŠ” ν˜•νƒœλ₯Ό μ΅νžˆμ‹œλ©΄ λ©λ‹ˆλ‹€.
if "Condition": 
    # "Condition"은 곡백이 μ•„λ‹Œ λ¬Έμžμ—΄μ΄κΈ° λ•Œλ¬Έμ— True 값을 κ°€μ§€κ²Œ 되고
    # 이 μ˜ˆμ œμ—μ„œ 이곳의 indent λ˜μ–΄μ§„ μ½”λ“œλŠ” 항상 μ‹€ν–‰λ©λ‹ˆλ‹€.
    print(True)
else:
    # λ§Œμ•½ condition이 거짓이라면 이곳의 μ½”λ“œκ°€ 처음 λΈ”λ‘μ˜ μ½”λ“œ λŒ€μ‹  μ‹€ν–‰λ˜κ²Œ λ©λ‹ˆλ‹€.
    print(False)
# 이 μ˜ˆμ œμ—μ„œ else에 μžˆλŠ” μ½”λ“œλŠ” μ ˆλŒ€ μ‹€ν–‰λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. 
# μ™œλƒν•˜λ©΄ "Condition"의 μ§„μœ„κ°’μ΄ True 이기 λ•Œλ¬Έμž…λ‹ˆλ‹€.
True
In [90]:
i = 4
if i == 5:
    print('The variable i has a value of 5')

μœ„ 예제λ₯Ό μ‚΄νŽ΄λ³΄λ©΄ i 값이 4이기 λ•Œλ¬Έμ— i==5 λΌλŠ” λͺ…μ œ(statement)λŠ” κ±°μ§“ 값을 κ°€μ§€κ²Œ 되고 μ•„λ¬΄λŸ° 문ꡬ도 좜λ ₯이 λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. μœ„μ— μ½”λ“œμ— else문을 μΆ”κ°€ν•¨μœΌλ‘œμ¨ μ•„λž˜μ™€ 같이 μ‹€ν–‰λ˜λŠ” 뢀뢄을 λ§Œλ“€ 수 μžˆμŠ΅λ‹ˆλ‹€.

In [91]:
i = 4
if i == 5:
    print("All lines in this indented block are part of this block")
    print('The variable i has a value of 5')
else:
    print("All lines in this indented block are part of this block")
    print('The variable i is not equal to 5')
All lines in this indented block are part of this block
The variable i is not equal to 5

if λ¬Έμ—μ„œ 쑰건이 거짓이라 λΉ μ Έλ‚˜μ™”μ„ λ•Œ 또 λ‹€λ₯Έ 쑰건을 확인해 λ³Ό 수 μžˆλ„λ‘ elif ꡬ문을 첨가할 수 μžˆμŠ΅λ‹ˆλ‹€. elifλŠ” "else if"의 μ€„μž„λ§ μž…λ‹ˆλ‹€. ν•œκ°œμ˜ if ꡬ문에 μ—¬λŸ¬κ°œμ˜ elifꡬ문듀을 포함할 수 μžˆμŠ΅λ‹ˆλ‹€.

In [93]:
i = 3
if i == 1:
    print('The variable i has a value of 1')
elif i == 2:
    print('The variable i has a value of 2')
elif i == 3:
    print('The variable i has a value of 3')
else:
    print("I don't care what i is")
The variable i has a value of 3

if ꡬ문을 λ‹€μŒκ³Ό 같이 μ€‘μ²©ν•˜μ—¬ μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

In [94]:
i = 10
if i % 2 == 0:
    if i % 3 == 0:
        print ('i is divisible by both 2 and 3! Wow!')
    elif i % 5 == 0:
        print ('i is divisible by both 2 and 5! Wow!')
    else:
        print ('i is divisible by 2, but not 3 or 5. Meh.')
else:
    print('I guess that i is an odd number. Boring.')
i is divisible by both 2 and 5! Wow!

μœ„μ—μ„œ κ³΅λΆ€ν•œ 논리 μ—°μ‚°μž(Logical Operator)λ₯Ό μ‚¬μš©ν•˜μ—¬ μ—¬λŸ¬κ°œμ˜ 쑰건을 λ¬Άμ–΄μ„œ ν•˜λ‚˜μ˜ if문에 집어넣을 수 μžˆμŠ΅λ‹ˆλ‹€.

In [97]:
i = 5
j = 12
if i < 10 and j > 11:
    print('{0} is less than 10 and {1} is greater than 11!'.format(i, j))
5 is less than 10 and 12 is greater than 11!

λ¬Έμžμ—΄ λ³€μˆ˜μ—λ„ 적용이 κ°€λŠ₯ν•©λ‹ˆλ‹€.

In [98]:
my_string = "Carthago delenda est"
if my_string == "Carthago delenda est":
    print('And so it was! For the glory of Rome!')
else:
    print('War elephants are TERRIFYING. I am staying home.')
And so it was! For the glory of Rome!

λ‹€μŒκ³Ό 같이 λ¬Έμžμ—΄μ—λ„ < λ˜λŠ” >와 같은 비ꡐ μ—°μ‚°μžλ₯Ό μ μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

In [101]:
'apple' < 'banana'
Out[101]:
True
In [102]:
'cat' < 'apple'
Out[102]:
False

μžμ„Έν•œ μ„€λͺ…은 λ¬Έμžμ—΄μ˜ 사전식 μˆœμ„œ ν•­λͺ©μ„ μ°Έκ³ ν•΄ μ£Όμ„Έμš”.

λͺ‡λͺ‡ λ‚΄μž₯ ν•¨μˆ˜λ“€μ€ boolνƒ€μž…μ˜ 데이터λ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€. λ”°λΌμ„œ 이듀은 if문의 쑰건으둜 λ°”λ‘œ μ‚¬μš©μ΄ κ°€λŠ₯ν•©λ‹ˆλ‹€. μ‚¬μš©μžκ°€ λ§Œλ“  ν•¨μˆ˜κ°€ bool νƒ€μž…μ˜ 데이터λ₯Ό λ°˜ν™˜ν•œλ‹€λ©΄ 이 λ˜ν•œ if문의 쑰건으둜 μ‚¬μš©μ΄ κ°€λŠ₯ν•©λ‹ˆλ‹€. 이에 λŒ€ν•œ μžμ„Έν•œ μ΄μ•ΌκΈ°λŠ” λ’€μ—μ„œ 닀루도둝 ν•˜κ² μŠ΅λ‹ˆλ‹€.

μ•žμ—μ„œ 잠깐 μ‚΄νŽ΄λ΄€λ˜ in ν‚€μ›Œλ“œ λ˜ν•œ if ꡬ문에 μ‚¬μš©μ΄ κ°€λŠ₯ν•©λ‹ˆλ‹€.

In [103]:
if 'a' in my_string or 'e' in my_string:
    print('Those are my favorite vowels!')
Those are my favorite vowels!

반복문(Loop Structures)

λ°˜λ³΅λ¬Έμ€ ν”„λ‘œκ·Έλž˜λ°μ—μ„œ κ°€μž₯ μ€‘μš”ν•œ νŒŒνŠΈμ€‘ ν•˜λ‚˜ μž…λ‹ˆλ‹€. forλ¬Έκ³Ό while문은 λ‚΄λΆ€μ˜ μ½”λ“œ λΈ”λŸ­μ„ 반볡적으둜 μ‹€ν–‰ν•˜λ„λ‘ ν•΄μ€λ‹ˆλ‹€. while문의 경우 νƒˆμΆœμ‘°κ±΄μ΄ μ°Έ 값이라면 계속 μ½”λ“œκ°€ μ‹€ν–‰λ©λ‹ˆλ‹€. μ–΄λ–€ μ‹œμ μ— νƒˆμΆœμ‘°κ±΄μ΄ 거짓이 λœλ‹€λ©΄ κ·Έ λ‹€μŒ 반볡 μ‹œ λ°˜λ³΅λ¬Έμ„ λΉ μ Έλ‚˜κ°€κ²Œ λ©λ‹ˆλ‹€. for문의 경우 μ–΄λ–€ μˆ˜μ—΄(sequence)의 값을 λ”°λΌμ„œ 반볡문이 μ§„ν–‰λ˜κ³  μˆ˜μ—΄μ΄ λλ‚˜κ²Œ 되면 λ°˜λ³΅λ¬Έλ„ μ’…λ£Œ λ©λ‹ˆλ‹€.

In [104]:
i = 5
while i > 0: # 0이 False이기 λ•Œλ¬Έμ— 이 뢀뢄은 'while i:' 라고 ν‘œκΈ°κ°€ κ°€λŠ₯ν•©λ‹ˆλ‹€.
    i -= 1
    print('I am looping! {0} more to go!'.format(i))
I am looping! 4 more to go!
I am looping! 3 more to go!
I am looping! 2 more to go!
I am looping! 1 more to go!
I am looping! 0 more to go!

while 문을 μ‚¬μš©ν•  λ•Œμ—λŠ” νƒˆμΆœμ‘°κ±΄μ— λ“€μ–΄κ°„ 값이 루프가 λ°˜λ³΅λ¨μ— 따라 변화함을 항상 확인해야 ν•©λ‹ˆλ‹€. κ·Έλ ‡μ§€ μ•ŠμœΌλ©΄ νƒˆμΆœμ‘°κ±΄μ΄ 항상 참이 되고 λ¬΄ν•œ 루프에 λΉ μ§€κ²Œ λ©λ‹ˆλ‹€. μœ„μ˜ μ˜ˆμ œμ—μ„œ iλΌλŠ” λ³€μˆ˜λ₯Ό νƒˆμΆœμ‘°κ±΄μ— μ‚¬μš©ν•˜μ˜€κ³  i 값은 루프 λ‚΄λΆ€μ—μ„œ i -= 1(i = i - 1의 μ€„μž„ λͺ…λ Ήμ–΄)μ΄λΌλŠ” λͺ…λ Ήμ–΄λ₯Ό 톡해 λ³€ν™”ν•˜κ²Œ λ©λ‹ˆλ‹€. 루프가 λ°˜λ³΅λ˜λ©΄μ„œ i 값은 점점 μ€„μ–΄λ“€κ²Œ 되고 0이 λ˜λŠ” μˆœκ°„μ΄ 있게 λ©λ‹ˆλ‹€. κ·Έ λ‹€μŒ 루프가 μ‹œμž‘ν•˜κΈ° μ „ νƒˆμΆœμ‘°κ±΄μ΄ Falseκ°€ 되고 λ£¨ν”„μ—μ„œ νƒˆμΆœν•˜κ²Œ λ©λ‹ˆλ‹€. κ·Έλž˜μ„œ 좜λ ₯된 κ²°κ³Όλ₯Ό λ³΄μ‹œλ©΄ 0 κΉŒμ§€ ν”„λ¦°νŠΈκ°€ λ˜μ–΄ μžˆμŠ΅λ‹ˆλ‹€.

for문은 μ§€μ •λœ 횟수만큼만 λ°˜λ³΅μ„ μ‹€ν–‰ν•©λ‹ˆλ‹€. μ•„λž˜μ™€ 같이 range()ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•  경우 i 값이 range(5)에 ν•΄λ‹Ήν•˜λŠ” [0,1,2,3,4,5] λ‚΄λΆ€μ˜ 값듀을 κ°€μ§€λ©΄μ„œ 반볡이 μ‹€ν–‰λ©λ‹ˆλ‹€.

In [1]:
for i in range(5):
    print('I am looping! I have looped {0} times!'.format(i + 1))
I am looping! I have looped 1 times!
I am looping! I have looped 2 times!
I am looping! I have looped 3 times!
I am looping! I have looped 4 times!
I am looping! I have looped 5 times!

μœ„μ—μ„œ in ν‚€μ›Œλ“œλ₯Ό 톡해 ν•΄λ‹Ή 데이터가 μžλ£Œκ΅¬μ‘°μ— λ“€μ–΄μžˆλŠ”μ§€ μ—¬λΆ€λ₯Ό νŒŒμ•…ν•œλ‹€κ³  λ°°μ› μŠ΅λ‹ˆλ‹€. ν•˜μ§€λ§Œ 이 in ν‚€μ›Œλ“œλŠ” μœ„μ™€ 같이 for문을 κ΅¬μ„±ν•˜λŠ”λ°λ„ μ‚¬μš©λ©λ‹ˆλ‹€. in ν‚€μ›Œλ“œ λ’€μ—λŠ” 리슀트, νŠœν”Œ, μ§‘ν•©(set)κ³Ό 같은 λ‹€λ₯Έ μžλ£Œκ΅¬μ‘°λ“€λ„ 올 수 μžˆμŠ΅λ‹ˆλ‹€.

In [2]:
my_list = {'cats', 'dogs', 'lizards', 'cows', 'bats', 'sponges', 'humans'} # Lists all the animals in the world
mammal_list = {'cats', 'dogs', 'cows', 'bats', 'humans'} # Lists all the mammals in the world
my_new_list = set()
for animal in my_list:
    if animal in mammal_list:
        # This adds any animal that is both in my_list and mammal_list to my_new_list
        my_new_list.add(animal)
        
print(my_new_list)
{'humans', 'dogs', 'cats', 'bats', 'cows'}

for와 whileλ¬Έ 같은 λ°˜λ³΅λ¬Έμ„ ꡬ성할 λ•Œ break와 continue ꡬ문을 ν™œμš©ν•˜μ—¬ μ†μ‰½κ²Œ λ°˜λ³΅λ¬Έμ„ λ‹€λ£° 수 μžˆμŠ΅λ‹ˆλ‹€. λ¨Όμ € 루프가 μ‹€ν–‰λ˜λŠ” λ™μ•ˆ μ–΄λ””μ„œλ“  break ꡬ문을 마주치게 되면 λ°˜λ³΅λ¬Έμ„ μ¦‰μ‹œ νƒˆμΆœν•©λ‹ˆλ‹€.

In [3]:
i = 10
while True:
    if i == 14:
        break
    i += 1 # i = i + 1 κ³Ό 같은 ν‘œν˜„ μž…λ‹ˆλ‹€.
    print(i)
11
12
13
14
In [4]:
for i in range(5):
    if i == 2:
        break
    print(i)
0
1

루프가 λŒλ‹€κ°€ continue ꡬ문을 λ§Œλ‚œλ‹€λ©΄ κ·Έ 루프λ₯Ό λ„˜μ–΄κ°€κ³  λ‹€μŒ λ£¨ν”„λ‘œ λ°”λ‘œ μ§„ν–‰ν•˜κ²Œ λ©λ‹ˆλ‹€.

In [5]:
i = 0
while i < 5:
    i += 1
    if i == 3:
        continue
    print(i)
1
2
4
5

μœ„μ˜ 좜λ ₯ κ²°κ³Όλ₯Ό μ‚΄νŽ΄λ³΄λ©΄ $3$이 μ—†μŒμ„ 확인할 수 μžˆμŠ΅λ‹ˆλ‹€. μ΄λŠ” iκ°€ 3이 λ˜μ—ˆμ„ λ•Œ if문을 톡해 쑰건에 걸리게 되고 contine ꡬ문을 λ§Œλ‚˜ iλ₯Ό 좜λ ₯ν•˜λŠ” 일 없이 λ°”λ‘œ λ‹€μŒ λ£¨ν”„λ‘œ λ„˜μ–΄κ°€κΈ° λ•Œλ¬Έμž…λ‹ˆλ‹€.

λ°˜λ³΅μ„ μœ„ν•΄ μ‚¬μš©λœ λ³€μˆ˜(μ˜ˆμ œμ—μ„œ i) λ˜λŠ” 반볡문 λ‚΄λΆ€μ—μ„œ μ •μ˜λœ λ³€μˆ˜λŠ” 반볡 루프가 λλ‚˜λ”λΌλ„ 값이 μ§€μ›Œμ§€μ§€ μ•Šκ³  루프가 끝날 λ•Œμ˜ 값을 μœ μ§€ν•©λ‹ˆλ‹€. λ‹€μŒ μ˜ˆμ œμ—μ„œ 루프가 끝났을 λ•Œ i값이 4둜 μœ μ§€λ¨μ„ 확인 ν•  수 있고 루프 λ‚΄λΆ€μ—μ„œ μ •μ˜λœ loop_string 값도 κ·ΈλŒ€λ‘œ μ‚΄μ•„μžˆμŒμ„ λ³Ό 수 μžˆμŠ΅λ‹ˆλ‹€.

In [6]:
for i in range(5):
    loop_string = 'I transcend the loop!'
    print ('I am eternal! I am {0} and I exist everywhere!'.format(i))

print ('I persist! My value is {0}'.format(i))
print (loop_string)
I am eternal! I am 0 and I exist everywhere!
I am eternal! I am 1 and I exist everywhere!
I am eternal! I am 2 and I exist everywhere!
I am eternal! I am 3 and I exist everywhere!
I am eternal! I am 4 and I exist everywhere!
I persist! My value is 4
I transcend the loop!

λ”•μ…”λ„ˆλ¦¬ μžλ£Œν˜•λ„ for문을 톡해 λ°˜λ³΅ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

In [17]:
my_dict = {'firstname' : 'Inigo', 'lastname' : 'Montoya', 'nemesis' : 'Rugen'}
In [8]:
for key in my_dict:
    print(key)
firstname
lastname
nemesis

μœ„μ™€ 같이 λ”•μ…”λ„ˆλ¦¬λ₯Ό in ν‚€μ›Œλ“œλ₯Ό 톡해 λ°˜λ³΅μ‹œν‚€λ©΄ μžλ™μ μœΌλ‘œ key 값이 λ°˜λ³΅λ˜μ–΄ μ§‘λ‹ˆλ‹€. value 값에 μ ‘κ·Όν•˜κ³ μž ν•œλ‹€λ©΄ λ‹€μŒκ³Ό 같이 접근이 κ°€λŠ₯ν•©λ‹ˆλ‹€.

In [9]:
for key in my_dict:
    print (my_dict[key])
Inigo
Montoya
Rugen

λ™μ‹œμ— key, value 값을 닀루고 μ‹Άλ‹€λ©΄ items()ν•¨μˆ˜λ₯Ό 톡해 ν•˜λ‚˜μ˜ λ£¨ν”„μ—μ„œ λ™μ‹œμ— key-value μŒμ— 접근이 κ°€λŠ₯ν•©λ‹ˆλ‹€.

In [19]:
for key, value in my_dict.items():
    print(key, ':', value)
firstname : Inigo
lastname : Montoya
nemesis : Rugen

items()ν•¨μˆ˜λŠ” key-value μŒμ„ νŠœν”Œλ‘œ λ§Œλ“€μ–΄ μ€λ‹ˆλ‹€. κ·Έ λ’€ for λ¬Έμ—μ„œ ν•΄λ‹Ή νŠœν”Œμ„ key, value λΌλŠ” λ³€μˆ˜μ— unpacks ν•΄ μ£Όκ³  반볡이 μ§„ν–‰λ˜λŠ”κ²ƒ μž…λ‹ˆλ‹€.

함수(Functions)

ν•¨μˆ˜(Function)은 계산을 ν•˜κ±°λ‚˜, 데이터λ₯Ό λ‚΄λ³΄λ‚΄κ±°λ‚˜, 당신이 μ›ν•˜λŠ” 것을 ν•  수 μžˆλŠ” μž¬μ‚¬μš©μ΄ κ°€λŠ₯ν•œ μ½”λ“œ λΈ”λŸ­μ„ μ˜λ―Έν•©λ‹ˆλ‹€. ν”„λ‘œκ·Έλž˜λ° μ–Έμ–΄λ₯Ό μ“°λŠ” 이유 쀑 ν•˜λ‚˜κ°€ 이런 ν•¨μˆ˜λ₯Ό λ§Œλ“€μ–΄λ‘κ³  μž¬μ‚¬μš©μ΄ κ°€λŠ₯ν•˜κΈ° λ•Œλ¬Έμž…λ‹ˆλ‹€. νŒŒμ΄μ¬μ— μžˆλŠ” λ‚΄μž₯ν•¨μˆ˜ 이외에도 μš°λ¦¬κ°€ μ›ν•˜λŠ” ν•¨μˆ˜λ₯Ό 자유둭게 λ§Œλ“€μ–΄μ„œ μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

In [20]:
def hello_world():
    """ Prints Hello, world! """
    print('Hello, world!')

hello_world()
Hello, world!
In [21]:
for i in range(5):
    hello_world()
Hello, world!
Hello, world!
Hello, world!
Hello, world!
Hello, world!

defλͺ…λ Ήμ–΄λ₯Ό 톡해 ν•¨μˆ˜λ₯Ό μ •μ˜(define) ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

def ν•¨μˆ˜μ΄λ¦„(νŒŒλΌλ―Έν„° λͺ©λ‘):

    ν•¨μˆ˜λ‚΄μš©

    # λ°˜ν™˜ 값이 μžˆλ‹€λ©΄ return을 톡해 값을 μ§€μ •ν•΄ μ€λ‹ˆλ‹€.
    return (λ°˜ν™˜κ°’)

처음 λ§Œλ“  hello_world() ν•¨μˆ˜λŠ” 호좜 되면 μ–Έμ œλ‚˜ λ¬Έμžμ—΄μ„ 좜λ ₯ν•©λ‹ˆλ‹€. λ”°λ‘œ λ°˜ν™˜ν•˜λŠ” 값은 μ—†μŠ΅λ‹ˆλ‹€. μ–΄λ–€ κ³„μ‚°λœ κ°’μ΄λ‚˜ λ°˜ν™˜κ°’μ΄ ν•„μš”ν•˜λ‹€λ©΄ κ·Έ 값을 ν•¨μˆ˜μ—μ„œ λ°˜ν™˜(return) ν•΄μ€˜μ•Ό ν•©λ‹ˆλ‹€. λ˜ν•œ ν•¨μˆ˜ λ‚΄λΆ€μ—μ„œ μ„ μ–Έλœ λ³€μˆ˜λŠ” 일반적으둜 ν•¨μˆ˜ λ°”κΉ₯μ—μ„œλŠ” μ‚¬μš©ν•  수 μ—†μŠ΅λ‹ˆλ‹€.

In [22]:
def see_the_scope():
    in_function_string = "I'm stuck in here!"

see_the_scope()
print(in_function_string)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-22-fb168f245f67> in <module>()
      3 
      4 see_the_scope()
----> 5 print(in_function_string)

NameError: name 'in_function_string' is not defined

λ³€μˆ˜μ˜ λ²”μœ„λŠ” ν•΄λ‹Ή λ³€μˆ˜κ°€ κ·Έ κ°’κ³Ό μ—°κ΄€λ˜μ–΄ μžˆλŠ” μ½”λ“œμ˜ λ²”μœ„λ₯Ό μ˜λ―Έν•©λ‹ˆλ‹€. νŒŒμ΄μ¬μ—μ„œ ν•¨μˆ˜μ˜ 경우 λ‹«νžŒ λ²”μœ„(enclosed scope)λ₯Ό κ°€μ§€κ²Œ λ©λ‹ˆλ‹€. μ΄λŠ” μœ„μ— μ½”λ“œμ—μ„œ 처럼 ν•¨μˆ˜ λ°–μ—μ„œλŠ” ν•¨μˆ˜ λ‚΄λΆ€μ—μ„œ λ§Œλ“€μ–΄μ§„ λ³€μˆ˜μ— 접근이 λΆˆκ°€λŠ₯함을 μ˜λ―Έν•©λ‹ˆλ‹€. ν•¨μˆ˜ λ‚΄λΆ€μ˜ 값을 λ°–μ—μ„œ ν™œμš©ν•˜κΈ° μœ„ν•΄μ„œλŠ” ν•¨μˆ˜ λ‚΄λΆ€μ—μ„œ λ°˜ν™˜(return)을 ν•΄μ£Όμ–΄μ•Ό ν•©λ‹ˆλ‹€. λ°˜ν™˜λœ 값을 ν•¨μˆ˜ λ°–μ—μ„œ λ°›λŠ”λ‹€λ©΄ 문제 없이 값을 전달받을 수 μžˆμŠ΅λ‹ˆλ‹€.

λ‹€μŒ μ˜ˆμ œμ—μ„œ 처럼 ν•¨μˆ˜ λ‚΄λΆ€μ—μ„œ μ„ μ–Έλœ λ³€μˆ˜λ₯Ό return ν•¨μœΌλ‘œμ¨ ν•¨μˆ˜ λ°”κΉ₯μ—μ„œ μ‚¬μš©ν•˜κ±°λ‚˜ 좜λ ₯ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

In [23]:
def free_the_scope():
    in_function_string = "Anything you can do I can do better!"
    return in_function_string
my_string = free_the_scope()
print(my_string)
Anything you can do I can do better!

ν•¨μˆ˜ λ°”κΉ₯μ—μ„œ λ‚΄λΆ€μ˜ λ³€μˆ˜λ₯Ό λ°›λŠ”κ²ƒμ²˜λŸΌ λ°˜λŒ€λ‘œ ν•¨μˆ˜ λ°”κΉ₯의 데이터λ₯Ό ν•¨μˆ˜λ‘œ 전달도 κ°€λŠ₯ν•©λ‹ˆλ‹€. μ΄λŠ” νŒŒλΌλ―Έν„°λ₯Ό μ„€μ •ν•¨μœΌλ‘œμ¨ κ°€λŠ₯ν•©λ‹ˆλ‹€.

In [24]:
def multiply_by_five(x):
    """ input에 5λ₯Ό κ³±ν•œκ°’μ„ λ°˜ν™˜ν•©λ‹ˆλ‹€. """
    return x * 5

n = 4
print(n)
print(multiply_by_five(n))
4
20

μœ„μ˜ μ˜ˆμ œμ—μ„œλŠ” xλΌλŠ” ν•˜λ‚˜μ˜ νŒŒλΌλ―Έν„°λ₯Ό μ‚¬μš©ν•˜μ˜€μ§€λ§Œ μ‰Όν‘œ(,)λ₯Ό 톡해 μ—¬λŸ¬κ°œμ˜ νŒŒλΌλ―Έν„°λ₯Ό μΆ”κ°€ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

In [25]:
def calculate_area(length, width):
    """ μ§μ‚¬κ°ν˜•μ˜ 넓이λ₯Ό κ³„μ‚°ν•˜μ—¬ λ°˜ν™˜ν•©λ‹ˆλ‹€. """
    return length * width
In [26]:
l = 5
w = 10
print('Area: ', calculate_area(l, w))
print('Length: ', l)
print('Width: ', w)
Area:  50
Length:  5
Width:  10

λ§Œμ•½ μž…λ ₯ 받을 νŒŒλΌλ―Έν„°μ˜ κ°―μˆ˜κ°€ μ •ν•΄μ§€μ§€ μ•Šμ•˜λ‹€λ©΄ λ³„ν‘œκΈ°ν˜Έ(*)λ₯Ό μ‚¬μš©ν•˜μ—¬ 이λ₯Ό μ²˜λ¦¬ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

In [27]:
def sum_values(*args):
    sum_val = 0
    for i in args:
        sum_val += i
    return sum_val
In [28]:
print(sum_values(1, 2, 3))
print(sum_values(10, 20, 30, 40, 50))
print(sum_values(4, 2, 5, 1, 10, 249, 25, 24, 13, 6, 4))
6
150
343

μœ„μ™€ 같이 λͺ‡κ°œμ˜ 값듀이 ν•¨μˆ˜λ‘œ 전달될지 λͺ¨λ₯Ό 경우 *args와 같은 λ°©μ‹μœΌλ‘œ μž…λ ₯을 받을 수 μžˆμŠ΅λ‹ˆλ‹€. λ³„ν‘œ(*) κΈ°ν˜ΈλŠ” '이 ν•¨μˆ˜κ°€ μž„μ˜μ˜ 개수둜 값듀을 받을 것이닀.' λΌλŠ” 것을 ν‘œμ‹œν•˜λŠ” 파이썬 λ¬Έλ²•μž…λ‹ˆλ‹€. μ΄λ ‡κ²Œ μž…λ ₯된 값듀은 νŠœν”Œ(tuples)의 ν˜•νƒœλ‘œ μ €μž₯λ©λ‹ˆλ‹€.

In [29]:
def test_args(*args):
    print(type(args))

test_args(1, 2, 3, 4, 5, 6)
<class 'tuple'>

μœ„μ—μ„œ μ“΄ *argsλΌλŠ” ν‘œν˜„μ€ κ΄€μŠ΅μ μΈ ν‘œν˜„μœΌλ‘œ *vars λ˜λŠ” *things와 같이 μž„μ˜λ‘œ 이름을 λ°”κΎΈμ–΄ μ‚¬μš©ν•˜μ—¬λ„ λ¬Έμ œκ°€ μ—†μŠ΅λ‹ˆλ‹€. λ˜ν•œ ν•¨μˆ˜ λ‚΄λΆ€μ—μ„œ argsλŠ” νŠœν”Œ νƒ€μž…μ„ κ°€μ§€κΈ° λ•Œλ¬Έμ— μœ„μ—μ„œ 배운 λ°©μ‹μœΌλ‘œ 각 λ‚΄μš©λ¬Όμ— 접근이 κ°€λŠ₯ν•©λ‹ˆλ‹€. λ‹€λ§Œ νŠœν”Œν˜•μ˜ νŠΉμ„±μƒ λ‚΄μš©λ¬Όμ˜ μˆ˜μ •μ€ λΆˆκ°€λŠ₯ ν•©λ‹ˆλ‹€.

μš°λ¦¬κ°€ λ§Œλ“  ν•¨μˆ˜λ“€μ€ μ–΄λ–€ 데이터 νƒ€μž…μ΄λ‚˜ λ°˜ν™˜μ΄ κ°€λŠ₯ν•©λ‹ˆλ‹€. ν•¨μˆ˜κ°€ boolνƒ€μž…μ„ λ°˜ν™˜ν•˜κ²Œ ν•œλ‹€λ©΄ 이λ₯Ό 쑰건(condition)으둜 μ‚¬μš©μ΄ κ°€λŠ₯ν•©λ‹ˆλ‹€. 예λ₯Ό λ“€μ–΄ μ•ŒνŒŒλ²³ λͺ¨μŒμ΄ λ“€μ–΄μžˆλŠ”μ§€ μ²΄ν¬ν•˜μ—¬ λͺ¨μŒμ΄ λ“€μ–΄μžˆλ‹€λ©΄ Trueλ₯Ό λ“€μ–΄μžˆμ§€ μ•Šλ‹€λ©΄ Falseλ₯Ό λ°˜ν™˜ν•˜λŠ” ν•¨μˆ˜λ₯Ό λ‹€μŒκ³Ό 같이 λ§Œλ“€ 수 μžˆμŠ΅λ‹ˆλ‹€.

In [30]:
def has_a_vowel(word):
    """ 
    단어에 λͺ¨μŒμ΄ λ“€μ–΄κ°€ μžˆλŠ”μ§€ ν™•μΈν•˜λŠ” ν•¨μˆ˜ μž…λ‹ˆλ‹€. 
    λͺ¨μŒμΈ a,e,i,o,u κ°€ λ“€μ–΄κ°€ μžˆλ‹€λ©΄ Trueλ₯Ό
    κ·Έλ ‡μ§€ μ•Šλ‹€λ©΄ Falseλ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.
    """
    vowel_list = ['a', 'e', 'i', 'o', 'u']
    
    for vowel in vowel_list:
        if vowel in word:
            return True
    # a,e,i,o,u κ°€ λͺ¨λ‘ μ—†λ‹€λ©΄ Falseκ°€ λ°˜ν™˜λ©λ‹ˆλ‹€.
    return False
In [33]:
my_word = 'PJP'
has_a_vowel(my_word)
Out[33]:
False
In [34]:
my_word = 'catnapping'
has_a_vowel(my_word)
Out[34]:
True
In [35]:
if has_a_vowel(my_word):
    print('How surprising, an english word contains a vowel.')
else:
    print('This is actually surprising.')
How surprising, an english word contains a vowel.
In [37]:
def point_maker(x, y):
    """ 
    2차원 μƒμ˜ x,y μ’Œν‘œλ₯Ό λ°˜ν™˜ν•˜λŠ” ν•¨μˆ˜μž…λ‹ˆλ‹€.
    사싀 νŠœν”Œκ³Ό κ°™μŠ΅λ‹ˆλ‹€.
    """
    return x, y

μœ„μ˜ ν•¨μˆ˜λŠ” 숫자의 μˆœμ„œμŒμ„ μž…λ ₯λ°›μ•„ $R^2$ κ³΅κ°„μ˜ ν•œ 점, (x,y)둜 λ°˜ν™˜ν•©λ‹ˆλ‹€.

In [38]:
a = point_maker(0, 10)
b = point_maker(5, 3)

def calculate_slope(point_a, point_b):
    """ 두 점을 μ§€λ‚˜λŠ” μ§μ„ μ˜ 기울기λ₯Ό κ³„μ‚°ν•˜μ—¬ λ°˜ν™˜ν•©λ‹ˆλ‹€. """
    return (point_b[1] - point_a[1])/(point_b[0] - point_a[0])

print("The slope between a and b is {0}".format(calculate_slope(a, b)))
The slope between a and b is -1.4

μœ„μ˜ ν•¨μˆ˜λŠ” 두 점을 μ§€λ‚˜λŠ” μ§μ„ μ˜ 기울기λ₯Ό κ³„μ‚°ν•˜μ—¬ λ°˜ν™˜ν•˜λŠ” ν•¨μˆ˜μž…λ‹ˆλ‹€.

이λ₯Ό μ΄μš©ν•˜μ—¬ λ‹€μŒκ³Ό 같이 두 점을 μ§€λ‚˜λŠ” μ§μ„ μ˜ 방정식을 좜λ ₯ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

In [40]:
print("""The slope-intercept form of the line between a and b, using point a, is: y - {0} = {2}(x - {1})""".format(a[1], a[0], calculate_slope(a, b)))
The slope-intercept form of the line between a and b, using point a, is: y - 10 = -1.4(x - 0)

μœ„μ—μ„œ κ³΅λΆ€ν•œ 자료ꡬ쑰의 νŠΉμ„±λ“€κ³Ό μ½”λ“œ μž‘μ„±μ— ν•„μš”ν•œ 문법(syntax)을 잘 μ§€ν‚€κΈ°λ§Œ ν•œλ‹€λ©΄ μ–΄λ–€ 계산이든 ν•΄μ„œ 값을 λ°˜ν™˜ν•˜λŠ” ν•¨μˆ˜λ₯Ό μ†μ‰½κ²Œ λ§Œλ“€ 수 μžˆμŠ΅λ‹ˆλ‹€. μ΄λŠ” λͺ¨λ“  ν”„λ‘œκ·Έλž˜λ° μ–Έμ–΄μ—μ„œ κ°€μž₯ μ€‘μš”ν•œ λΆ€λΆ„μ΄λ―€λ‘œ 잘 μˆ™μ§€ν•΄ λ‘μ‹œκΈΈ λ°”λΌκ² μŠ΅λ‹ˆλ‹€. κ°μ‚¬ν•©λ‹ˆλ‹€.