
Introduction to Python II (Python Basics II)
Introduction to Python II (Python Basics)¶
by JunPyo Park
Part of the Alpha Square Lecture Series:
νμ΄μ¬ κΈ°μ΄μ μλ£κ΅¬μ‘°, λ¬Έμμ΄, 쑰건문, λ°λ³΅λ¬Έ κ·Έλ¦¬κ³ ν¨μμ λν΄μ κ°λ¨νκ² μ λ¦¬ν΄ λ³΄μμ΅λλ€. μ£ΌνΌν° λ ΈνΈλΆ νκ²½μμ μμ±ν λ¬Έμμ΄κΈ° λλ¬Έμ μ£ΌνΌν° λ ΈνΈλΆμ μΌκ³ λ°λΌμ μ€μ΅ν΄ 보μλ©΄ μ½κ² μ΄ν΄νμ€μ μμ΅λλ€.
문자열 처리하기¶
μμμ μ΄ν΄ 보μλ―μ΄ string
νμ
μ ν
μ€νΈλ₯Ό νννλλ° μ£Όλ‘ μ¬μ©λ©λλ€. μ¬κΈ°μλ λͺκ°μ§ λ΄μ₯ν¨μλ€μ μ΄ν΄λ³΄κ³ νμμ λ§κ² λ¬Έμμ΄μ λ€λ£° μ μλ λ°©λ²λ€μ μμ보λλ‘ νκ² μ΅λλ€.
first_string = 'I am '
second_string = 'a boy'
third_string = first_string + second_string
third_string
μ€νΈλ§ μλ£ν λν μΈλ±μ±μ΄ λμ΄ μκΈ° λλ¬Έμ(ordered) 리μ€νΈ μ¬λΌμ΄μ± κΈ°λ²λ€μ λκ°μ΄ νμ©ν μ μμ΅λλ€.
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]) # λ¬Έμμ΄ μμμΌλ‘ μΆλ ₯
λ΄μ₯λ κ°μ²΄(built-in objects)λ€μ κ°μ²΄λ€κ³Ό μ°κ²°λ νΉλ³ν ν¨μλ€μ κ°μ§ μ μμ΅λλ€. μ΄λ₯Ό λ©μλ(methods)
λΌκ³ ν©λλ€. λ§μΉ¨ν(.
)λ₯Ό μ¬μ©νμ¬ λ©μλμ μ κ·Όν μ μμ΅λλ€. λμ€μ λ€λ₯Έ κ°μ’μμ μ΄μλν΄ μμΈν λ€λ£¨λλ‘ νκ² μ΅λλ€.
- count() λ©μλμ νμ©νμ¬ λ€μκ³Ό κ°μ΄ νΉμ λ¬Έμμ΄μ΄ λͺ λ² λ±μ₯νλμ§ μ μ μμ΅λλ€.
print('Count of the letter i in Supercalifragilisticexpialidocious: ',
my_string.count('i'))
print('Count of "li" in the same word: ',
my_string.count('li'))
- find() λ©μλλ₯Ό νμ©νμ¬ νΉμ κ°μ΄ μ μΌ μ²μ λμ€λ κ³³μ μμΉ(μΈλ±μ€)λ₯Ό μ°Ύμ μ μμ΅λλ€.
print('The first time i appears is at index: ', my_string.find('i'))
- replace() λ©μλλ₯Ό νμ©νμ¬ λ¬Έμμ΄μ λΆλΆμ μΌλ‘ μΉνν μ μμ΅λλ€.
print("All i's are now a's: ", my_string.replace('i', 'a'))
print("It's raining cats and dogs".replace('dogs', 'more cats'))
- upper() κ³Ό lower() λ©μλλ₯Ό νμ©νμ¬ λ¬Έμμ΄μ λλ¬Έμ/μλ¬Έμ λ‘ λ³κ²½ κ°λ₯ν©λλ€.
my_string = "I can't hear you"
print(my_string.upper())
my_string = "I said HELLO"
print(my_string.lower())
문자열 포맷팅(String Formatting)¶
format()
λ©μλλ₯Ό μ¬μ©νμ¬ λ¬Έμμ΄μ ν¬λ§·ν
ν μ μμ΅λλ€.
my_string = "{0} {1}".format('Marco', 'Polo')
print(my_string)
my_string = "{1} {0}".format('Marco', 'Polo')
print(my_string)
μ€κ΄νΈ({}
)λ₯Ό μ¬μ©νμ¬ λ¬Έμμ΄μ΄ μ±μμ§ κ³³μμ λνλΌ μ μκ³ μμ λ€μ΄κ°λ μ«μλ₯Ό ν΅ν΄ λͺ λ²μ§Έλ‘ μ±μμ§λ κ³³μΈμ§ νκΈ°κ° κ°λ₯ν©λλ€.
format()
κ³Ό κ΄λ ¨λ μμΈν μ€λͺ
μ 곡μ Documentation μ μ°Έκ³ ν΄ μ£ΌμΈμ.
ν¬λ§·ν
μ λ€λ₯Έ λ°©λ²μΌλ‘λ λ€μκ³Ό κ°μ΄ %
λ₯Ό μ¬μ©νλ λ°©λ²μ΄ μμ΅λλ€.
print('insert %s here' % 'value')
print('There are %s cats in my %s' % (13, 'apartment'))
μ¬κΈ°μ %s
λ νμ΄μ¬ μμμ μ§μ λ κ°μ λ¬Έμμ΄λ‘ λ³ννμ¬ μ²λ¦¬νλΌλ μλ―Έ μ
λλ€. formatting documentationμ νμΈνμ¬ μΆκ°μ μΈ μμ λ€κ³Ό κΈ°νΈλ€μ νμΈ ν μ μμ΅λλ€.
λ
Όλ¦¬ μ°μ°μλ boolean
κ°λ€μ μ²λ¦¬νλ μ°μ°μλ€μ μλ―Έν©λλ€. bool
νμ
μ μ°Έ(True)
κ³Ό κ±°μ§(False)
μ€ νλμ κ°(λλ $1$μ΄λ $0$)λ§ κ°μ§ μ μμ΅λλ€.
νμ°½μμ 'λͺ
μ 'λ μ°Έκ³Ό κ±°μ§μ ꡬλΆν μ μλ λ¬Έμ₯ μ΄λΌκ³ λ°°μ μ΅λλ€. νμ΄μ¬μμλ μ΄λ° μ°Έκ³Ό κ±°μ§μ λͺ
νν ꡬλΆλλ ꡬ문(statement)μ ==
(κ°λ€), !=
(κ°μ§ μλ€), <
(μλ€), >
(ν¬λ€), <=
(μκ±°λ κ°λ€), κ·Έλ¦¬κ³ >=
(ν¬κ±°λ κ°λ€) μ κ°μ κΈ°νΈλ₯Ό ν΅ν΄ νν ν μ μμ΅λλ€.
print(5==5)
print(5>5)
μ΄λ λ¬Όλ‘ λ³μμλ μ μ©μ΄ κ°λ₯ν©λλ€.
m = 2
n = 23
print(m < n)
μ°Έμ΄λ κ±°μ§ κ°μ κ°λ κ°μ΄ λκ° μμ λ, or
,and
,not
κ³Ό κ°μ λ
Όλ¦¬ μ°μ°μ(logical operator)λ₯Ό μ μν μ μμ΅λλ€.
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))
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 |
print(((2 < 3) and (3 > 0)) or ((5 > 6) and not (4 < 2)))
μμ κ²°κ³Όκ° μΆλ ₯λλ κ³Όμ μ μ΄ν΄λ³΄λ©΄ λ€μκ³Ό κ°μ΅λλ€.
- (
True and True
)or
(False and not False
) True or (False and True
)True
orFalse
True
진위값(Truthiness)¶
bool()
ν¨μλ μ΄λ€ κ°μ μ°Έ/κ±°μ§ ννλ‘ λ°κΎΈμ΄μ£Όλ ν¨μ μ
λλ€. κ·Έλ λ€λ©΄ μ΄λ€ κ²½μ°μ μ°Έ(True)
μ΄ λμ€κ³ μ΄λ€ κ²½μ°μ κ±°μ§(False)
μ΄ λμ€κ² λ κΉμ? μΌλ°μ μΌλ‘ string, tuple, dictionaries, listsμ κ²½μ° λ΄μ©λ¬Όμ΄ νλλΌλ λ€μ΄μλ€λ©΄ μ°Έ κ°μ΄ λ°νλκ³ λΉμ΄ μλ€λ©΄ κ±°μ§ κ°μ΄ λ°νλ©λλ€.
bool('')
bool('I like Python!')
bool([])
bool([1,2,3])
bool(1)
bool(0)
bool(3.141592)
μ«μμ κ²½μ° 0μ΄λ©΄ False, κ·Έ μ΄μΈμλ True κ° λμ΅λλ€.
If 구문¶
νΉμ 쑰건μ λ§μ‘±ν λ μ΄λ€ μ½λλ₯Ό μ€νμν€κ³ μΆλ€λ©΄ If ꡬ문μ μ¬μ©νμ¬ μ΄λ₯Ό ꡬνν μ μμ΅λλ€.
λ€μκ³Ό κ°μ μ½λμμ if
λ¬Έ μμ conditionμ΄ μ°Έ μ΄λΌλ©΄ μμμλ μ½λκ° μ€νλ©λλ€. λ§μ½ conditionμ΄ κ±°μ§μ΄λΌλ©΄ ifλ¬Έ λ΄λΆμ μ½λλ μ€ν΅λκ³ else
κ΅¬λ¬Έμ΄ μλ κ³³μΌλ‘ κ°μ λλ¨Έμ§ μ½λκ° μ€νλκ² λ©λλ€. λ§μ½ else
κ΅¬λ¬Έμ΄ μλ€λ©΄ μ무 μΌλ μΌμ΄λμ§ μμ΅λλ€. μ΄ λ conditionμ μμμ λ°°μ΄ λ
Όλ¦¬ μ°μ°μλ₯Ό ν΅ν΄ ννλκ±°λ μ§μκ°μ ν΅ν΄ μ€μ ν μ μμ΅λλ€. if ꡬ문μ μ½λ‘ (:
)κ³Ό indented text(4-μ€νμ΄μ€ λλ tab)μ ν΅ν΄ μ μ ν μ μμ΅λλ€.
# if ꡬ문μ κΈ°μ΄μ μΈ λΌλ μ
λλ€.
# μ¬κΈ°μ "Condition"μ κ³΅λ°±μ΄ μλκΈ° λλ¬Έμ νμ True μ
λλ€.
# μ¬κΈ°μλ if λ¬Έμ΄ μ¬μ©λλ ννλ₯Ό μ΅νμλ©΄ λ©λλ€.
if "Condition":
# "Condition"μ κ³΅λ°±μ΄ μλ λ¬Έμμ΄μ΄κΈ° λλ¬Έμ True κ°μ κ°μ§κ² λκ³
# μ΄ μμ μμ μ΄κ³³μ indent λμ΄μ§ μ½λλ νμ μ€νλ©λλ€.
print(True)
else:
# λ§μ½ conditionμ΄ κ±°μ§μ΄λΌλ©΄ μ΄κ³³μ μ½λκ° μ²μ λΈλ‘μ μ½λ λμ μ€νλκ² λ©λλ€.
print(False)
# μ΄ μμ μμ elseμ μλ μ½λλ μ λ μ€νλμ§ μμ΅λλ€.
# μλνλ©΄ "Condition"μ μ§μκ°μ΄ True μ΄κΈ° λλ¬Έμ
λλ€.
i = 4
if i == 5:
print('The variable i has a value of 5')
μ μμ λ₯Ό μ΄ν΄λ³΄λ©΄ i κ°μ΄ 4μ΄κΈ° λλ¬Έμ i==5 λΌλ λͺ μ (statement)λ κ±°μ§ κ°μ κ°μ§κ² λκ³ μλ¬΄λ° λ¬Έκ΅¬λ μΆλ ₯μ΄ λμ§ μμ΅λλ€. μμ μ½λμ elseλ¬Έμ μΆκ°ν¨μΌλ‘μ¨ μλμ κ°μ΄ μ€νλλ λΆλΆμ λ§λ€ μ μμ΅λλ€.
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')
if λ¬Έμμ μ‘°κ±΄μ΄ κ±°μ§μ΄λΌ λΉ μ Έλμμ λ λ λ€λ₯Έ 쑰건μ νμΈν΄ λ³Ό μ μλλ‘ elif
ꡬ문μ 첨κ°ν μ μμ΅λλ€. elif
λ "else if"μ μ€μλ§ μ
λλ€. νκ°μ if ꡬ문μ μ¬λ¬κ°μ elif
ꡬ문λ€μ ν¬ν¨ν μ μμ΅λλ€.
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")
if ꡬ문μ λ€μκ³Ό κ°μ΄ μ€μ²©νμ¬ μ¬μ©ν μ μμ΅λλ€.
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.')
μμμ 곡λΆν λ Όλ¦¬ μ°μ°μ(Logical Operator)λ₯Ό μ¬μ©νμ¬ μ¬λ¬κ°μ 쑰건μ λ¬Άμ΄μ νλμ ifλ¬Έμ μ§μ΄λ£μ μ μμ΅λλ€.
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))
λ¬Έμμ΄ λ³μμλ μ μ©μ΄ κ°λ₯ν©λλ€.
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.')
λ€μκ³Ό κ°μ΄ λ¬Έμμ΄μλ <
λλ >
μ κ°μ λΉκ΅ μ°μ°μλ₯Ό μ μ©ν μ μμ΅λλ€.
'apple' < 'banana'
'cat' < 'apple'
μμΈν μ€λͺ μ λ¬Έμμ΄μ μ¬μ μ μμ νλͺ©μ μ°Έκ³ ν΄ μ£ΌμΈμ.
λͺλͺ λ΄μ₯ ν¨μλ€μ bool
νμ
μ λ°μ΄ν°λ₯Ό λ°νν©λλ€. λ°λΌμ μ΄λ€μ ifλ¬Έμ 쑰건μΌλ‘ λ°λ‘ μ¬μ©μ΄ κ°λ₯ν©λλ€. μ¬μ©μκ° λ§λ ν¨μκ° bool
νμ
μ λ°μ΄ν°λ₯Ό λ°ννλ€λ©΄ μ΄ λν ifλ¬Έμ 쑰건μΌλ‘ μ¬μ©μ΄ κ°λ₯ν©λλ€. μ΄μ λν μμΈν μ΄μΌκΈ°λ λ€μμ λ€λ£¨λλ‘ νκ² μ΅λλ€.
μμμ μ κΉ μ΄ν΄λ΄€λ in
ν€μλ λν if ꡬ문μ μ¬μ©μ΄ κ°λ₯ν©λλ€.
if 'a' in my_string or 'e' in my_string:
print('Those are my favorite vowels!')
반복문(Loop Structures)¶
λ°λ³΅λ¬Έμ νλ‘κ·Έλλ°μμ κ°μ₯ μ€μν ννΈμ€ νλ μ
λλ€. for
λ¬Έκ³Ό while
λ¬Έμ λ΄λΆμ μ½λ λΈλμ λ°λ³΅μ μΌλ‘ μ€ννλλ‘ ν΄μ€λλ€. while
λ¬Έμ κ²½μ° νμΆμ‘°κ±΄μ΄ μ°Έ κ°μ΄λΌλ©΄ κ³μ μ½λκ° μ€νλ©λλ€. μ΄λ€ μμ μ νμΆμ‘°κ±΄μ΄ κ±°μ§μ΄ λλ€λ©΄ κ·Έ λ€μ λ°λ³΅ μ λ°λ³΅λ¬Έμ λΉ μ Έλκ°κ² λ©λλ€. for
λ¬Έμ κ²½μ° μ΄λ€ μμ΄(sequence)μ κ°μ λ°λΌμ λ°λ³΅λ¬Έμ΄ μ§νλκ³ μμ΄μ΄ λλκ² λλ©΄ λ°λ³΅λ¬Έλ μ’
λ£ λ©λλ€.
i = 5
while i > 0: # 0μ΄ Falseμ΄κΈ° λλ¬Έμ μ΄ λΆλΆμ 'while i:' λΌκ³ νκΈ°κ° κ°λ₯ν©λλ€.
i -= 1
print('I am looping! {0} more to go!'.format(i))
while
λ¬Έμ μ¬μ©ν λμλ νμΆμ‘°κ±΄μ λ€μ΄κ° κ°μ΄ 루νκ° λ°λ³΅λ¨μ λ°λΌ λ³νν¨μ νμ νμΈν΄μΌ ν©λλ€. κ·Έλ μ§ μμΌλ©΄ νμΆμ‘°κ±΄μ΄ νμ μ°Έμ΄ λκ³ λ¬΄ν 루νμ λΉ μ§κ² λ©λλ€. μμ μμ μμ i
λΌλ λ³μλ₯Ό νμΆμ‘°κ±΄μ μ¬μ©νμκ³ i
κ°μ 루ν λ΄λΆμμ i -= 1
(i = i - 1
μ μ€μ λͺ
λ Ήμ΄)μ΄λΌλ λͺ
λ Ήμ΄λ₯Ό ν΅ν΄ λ³ννκ² λ©λλ€. 루νκ° λ°λ³΅λλ©΄μ i
κ°μ μ μ μ€μ΄λ€κ² λκ³ 0μ΄ λλ μκ°μ΄ μκ² λ©λλ€. κ·Έ λ€μ 루νκ° μμνκΈ° μ νμΆμ‘°κ±΄μ΄ Falseκ° λκ³ λ£¨νμμ νμΆνκ² λ©λλ€. κ·Έλμ μΆλ ₯λ κ²°κ³Όλ₯Ό 보μλ©΄ 0 κΉμ§ νλ¦°νΈκ° λμ΄ μμ΅λλ€.
for
λ¬Έμ μ§μ λ νμλ§νΌλ§ λ°λ³΅μ μ€νν©λλ€. μλμ κ°μ΄ range()
ν¨μλ₯Ό μ¬μ©ν κ²½μ° i
κ°μ΄ range(5)
μ ν΄λΉνλ [0,1,2,3,4,5]
λ΄λΆμ κ°λ€μ κ°μ§λ©΄μ λ°λ³΅μ΄ μ€νλ©λλ€.
for i in range(5):
print('I am looping! I have looped {0} times!'.format(i + 1))
μμμ in
ν€μλλ₯Ό ν΅ν΄ ν΄λΉ λ°μ΄ν°κ° μλ£κ΅¬μ‘°μ λ€μ΄μλμ§ μ¬λΆλ₯Ό νμ
νλ€κ³ λ°°μ μ΅λλ€. νμ§λ§ μ΄ in
ν€μλλ μμ κ°μ΄ for
λ¬Έμ ꡬμ±νλλ°λ μ¬μ©λ©λλ€. in
ν€μλ λ€μλ 리μ€νΈ, νν, μ§ν©(set)κ³Ό κ°μ λ€λ₯Έ μλ£κ΅¬μ‘°λ€λ μ¬ μ μμ΅λλ€.
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)
for
μ while
λ¬Έ κ°μ λ°λ³΅λ¬Έμ ꡬμ±ν λ break
μ continue
ꡬ문μ νμ©νμ¬ μμ½κ² λ°λ³΅λ¬Έμ λ€λ£° μ μμ΅λλ€. λ¨Όμ 루νκ° μ€νλλ λμ μ΄λμλ break
ꡬ문μ λ§μ£ΌμΉκ² λλ©΄ λ°λ³΅λ¬Έμ μ¦μ νμΆν©λλ€.
i = 10
while True:
if i == 14:
break
i += 1 # i = i + 1 κ³Ό κ°μ νν μ
λλ€.
print(i)
for i in range(5):
if i == 2:
break
print(i)
루νκ° λλ€κ° continue
ꡬ문μ λ§λλ€λ©΄ κ·Έ 루νλ₯Ό λμ΄κ°κ³ λ€μ 루νλ‘ λ°λ‘ μ§ννκ² λ©λλ€.
i = 0
while i < 5:
i += 1
if i == 3:
continue
print(i)
μμ μΆλ ₯ κ²°κ³Όλ₯Ό μ΄ν΄λ³΄λ©΄ $3$μ΄ μμμ νμΈν μ μμ΅λλ€. μ΄λ i
κ° 3μ΄ λμμ λ ifλ¬Έμ ν΅ν΄ 쑰건μ κ±Έλ¦¬κ² λκ³ contine ꡬ문μ λ§λ i
λ₯Ό μΆλ ₯νλ μΌ μμ΄ λ°λ‘ λ€μ 루νλ‘ λμ΄κ°κΈ° λλ¬Έμ
λλ€.
λ°λ³΅μ μν΄ μ¬μ©λ λ³μ(μμ μμ i
) λλ λ°λ³΅λ¬Έ λ΄λΆμμ μ μλ λ³μλ λ°λ³΅ 루νκ° λλλλΌλ κ°μ΄ μ§μμ§μ§ μκ³ λ£¨νκ° λλ λμ κ°μ μ μ§ν©λλ€. λ€μ μμ μμ 루νκ° λλ¬μ λ i
κ°μ΄ 4λ‘ μ μ§λ¨μ νμΈ ν μ μκ³ λ£¨ν λ΄λΆμμ μ μλ loop_string
κ°λ κ·Έλλ‘ μ΄μμμμ λ³Ό μ μμ΅λλ€.
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)
λμ
λ리 μλ£νλ for
λ¬Έμ ν΅ν΄ λ°λ³΅ν μ μμ΅λλ€.
my_dict = {'firstname' : 'Inigo', 'lastname' : 'Montoya', 'nemesis' : 'Rugen'}
for key in my_dict:
print(key)
μμ κ°μ΄ λμ
λ리λ₯Ό in
ν€μλλ₯Ό ν΅ν΄ λ°λ³΅μν€λ©΄ μλμ μΌλ‘ key κ°μ΄ λ°λ³΅λμ΄ μ§λλ€. value κ°μ μ κ·Όνκ³ μ νλ€λ©΄ λ€μκ³Ό κ°μ΄ μ κ·Όμ΄ κ°λ₯ν©λλ€.
for key in my_dict:
print (my_dict[key])
λμμ key, value κ°μ λ€λ£¨κ³ μΆλ€λ©΄ items()
ν¨μλ₯Ό ν΅ν΄ νλμ 루νμμ λμμ key-value μμ μ κ·Όμ΄ κ°λ₯ν©λλ€.
for key, value in my_dict.items():
print(key, ':', value)
items()
ν¨μλ key-value μμ ννλ‘ λ§λ€μ΄ μ€λλ€. κ·Έ λ€ for λ¬Έμμ ν΄λΉ ννμ key, value λΌλ λ³μμ unpacks ν΄ μ£Όκ³ λ°λ³΅μ΄ μ§νλλκ² μ
λλ€.
함수(Functions)¶
ν¨μ(Function)μ κ³μ°μ νκ±°λ, λ°μ΄ν°λ₯Ό λ΄λ³΄λ΄κ±°λ, λΉμ μ΄ μνλ κ²μ ν μ μλ μ¬μ¬μ©μ΄ κ°λ₯ν μ½λ λΈλμ μλ―Έν©λλ€. νλ‘κ·Έλλ° μΈμ΄λ₯Ό μ°λ μ΄μ μ€ νλκ° μ΄λ° ν¨μλ₯Ό λ§λ€μ΄λκ³ μ¬μ¬μ©μ΄ κ°λ₯νκΈ° λλ¬Έμ λλ€. νμ΄μ¬μ μλ λ΄μ₯ν¨μ μ΄μΈμλ μ°λ¦¬κ° μνλ ν¨μλ₯Ό μμ λ‘κ² λ§λ€μ΄μ μ¬μ©ν μ μμ΅λλ€.
def hello_world():
""" Prints Hello, world! """
print('Hello, world!')
hello_world()
for i in range(5):
hello_world()
def
λͺ
λ Ήμ΄λ₯Ό ν΅ν΄ ν¨μλ₯Ό μ μ(define) ν μ μμ΅λλ€.
def ν¨μμ΄λ¦(νλΌλ―Έν° λͺ©λ‘):
ν¨μλ΄μ©
# λ°ν κ°μ΄ μλ€λ©΄ returnμ ν΅ν΄ κ°μ μ§μ ν΄ μ€λλ€.
return (λ°νκ°)
μ²μ λ§λ hello_world()
ν¨μλ νΈμΆ λλ©΄ μΈμ λ λ¬Έμμ΄μ μΆλ ₯ν©λλ€. λ°λ‘ λ°ννλ κ°μ μμ΅λλ€. μ΄λ€ κ³μ°λ κ°μ΄λ λ°νκ°μ΄ νμνλ€λ©΄ κ·Έ κ°μ ν¨μμμ λ°ν(return)
ν΄μ€μΌ ν©λλ€. λν ν¨μ λ΄λΆμμ μ μΈλ λ³μλ μΌλ°μ μΌλ‘ ν¨μ λ°κΉ₯μμλ μ¬μ©ν μ μμ΅λλ€.
def see_the_scope():
in_function_string = "I'm stuck in here!"
see_the_scope()
print(in_function_string)
λ³μμ λ²μλ ν΄λΉ λ³μκ° κ·Έ κ°κ³Ό μ°κ΄λμ΄ μλ μ½λμ λ²μλ₯Ό μλ―Έν©λλ€. νμ΄μ¬μμ ν¨μμ κ²½μ° λ«ν λ²μ(enclosed scope)λ₯Ό κ°μ§κ² λ©λλ€. μ΄λ μμ μ½λμμ μ²λΌ ν¨μ λ°μμλ ν¨μ λ΄λΆμμ λ§λ€μ΄μ§ λ³μμ μ κ·Όμ΄ λΆκ°λ₯ν¨μ μλ―Έν©λλ€. ν¨μ λ΄λΆμ κ°μ λ°μμ νμ©νκΈ° μν΄μλ ν¨μ λ΄λΆμμ λ°ν(return)μ ν΄μ£Όμ΄μΌ ν©λλ€. λ°νλ κ°μ ν¨μ λ°μμ λ°λλ€λ©΄ λ¬Έμ μμ΄ κ°μ μ λ¬λ°μ μ μμ΅λλ€.
λ€μ μμ μμ μ²λΌ ν¨μ λ΄λΆμμ μ μΈλ λ³μλ₯Ό return ν¨μΌλ‘μ¨ ν¨μ λ°κΉ₯μμ μ¬μ©νκ±°λ μΆλ ₯ν μ μμ΅λλ€.
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)
ν¨μ λ°κΉ₯μμ λ΄λΆμ λ³μλ₯Ό λ°λκ²μ²λΌ λ°λλ‘ ν¨μ λ°κΉ₯μ λ°μ΄ν°λ₯Ό ν¨μλ‘ μ λ¬λ κ°λ₯ν©λλ€. μ΄λ νλΌλ―Έν°
λ₯Ό μ€μ ν¨μΌλ‘μ¨ κ°λ₯ν©λλ€.
def multiply_by_five(x):
""" inputμ 5λ₯Ό κ³±νκ°μ λ°νν©λλ€. """
return x * 5
n = 4
print(n)
print(multiply_by_five(n))
μμ μμ μμλ x
λΌλ νλμ νλΌλ―Έν°λ₯Ό μ¬μ©νμμ§λ§ μΌν(,
)λ₯Ό ν΅ν΄ μ¬λ¬κ°μ νλΌλ―Έν°λ₯Ό μΆκ°ν μ μμ΅λλ€.
def calculate_area(length, width):
""" μ§μ¬κ°νμ λμ΄λ₯Ό κ³μ°νμ¬ λ°νν©λλ€. """
return length * width
l = 5
w = 10
print('Area: ', calculate_area(l, w))
print('Length: ', l)
print('Width: ', w)
λ§μ½ μ
λ ₯ λ°μ νλΌλ―Έν°μ κ°―μκ° μ ν΄μ§μ§ μμλ€λ©΄ λ³νκΈ°νΈ(*
)λ₯Ό μ¬μ©νμ¬ μ΄λ₯Ό μ²λ¦¬ν μ μμ΅λλ€.
def sum_values(*args):
sum_val = 0
for i in args:
sum_val += i
return sum_val
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))
μμ κ°μ΄ λͺκ°μ κ°λ€μ΄ ν¨μλ‘ μ λ¬λ μ§ λͺ¨λ₯Ό κ²½μ° *args
μ κ°μ λ°©μμΌλ‘ μ
λ ₯μ λ°μ μ μμ΅λλ€. λ³ν(*
) κΈ°νΈλ 'μ΄ ν¨μκ° μμμ κ°μλ‘ κ°λ€μ λ°μ κ²μ΄λ€.' λΌλ κ²μ νμνλ νμ΄μ¬ λ¬Έλ²μ
λλ€. μ΄λ κ² μ
λ ₯λ κ°λ€μ νν(tuples)μ ννλ‘ μ μ₯λ©λλ€.
def test_args(*args):
print(type(args))
test_args(1, 2, 3, 4, 5, 6)
μμμ μ΄ *args
λΌλ ννμ κ΄μ΅μ μΈ ννμΌλ‘ *vars
λλ *things
μ κ°μ΄ μμλ‘ μ΄λ¦μ λ°κΎΈμ΄ μ¬μ©νμ¬λ λ¬Έμ κ° μμ΅λλ€. λν ν¨μ λ΄λΆμμ args
λ νν νμ
μ κ°μ§κΈ° λλ¬Έμ μμμ λ°°μ΄ λ°©μμΌλ‘ κ° λ΄μ©λ¬Όμ μ κ·Όμ΄ κ°λ₯ν©λλ€. λ€λ§ νννμ νΉμ±μ λ΄μ©λ¬Όμ μμ μ λΆκ°λ₯ ν©λλ€.
μ°λ¦¬κ° λ§λ ν¨μλ€μ μ΄λ€ λ°μ΄ν° νμ
μ΄λ λ°νμ΄ κ°λ₯ν©λλ€. ν¨μκ° bool
νμ
μ λ°ννκ² νλ€λ©΄ μ΄λ₯Ό 쑰건(condition)μΌλ‘ μ¬μ©μ΄ κ°λ₯ν©λλ€. μλ₯Ό λ€μ΄ μνλ²³ λͺ¨μμ΄ λ€μ΄μλμ§ μ²΄ν¬νμ¬ λͺ¨μμ΄ λ€μ΄μλ€λ©΄ True
λ₯Ό λ€μ΄μμ§ μλ€λ©΄ False
λ₯Ό λ°ννλ ν¨μλ₯Ό λ€μκ³Ό κ°μ΄ λ§λ€ μ μμ΅λλ€.
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
my_word = 'PJP'
has_a_vowel(my_word)
my_word = 'catnapping'
has_a_vowel(my_word)
if has_a_vowel(my_word):
print('How surprising, an english word contains a vowel.')
else:
print('This is actually surprising.')
def point_maker(x, y):
"""
2μ°¨μ μμ x,y μ’νλ₯Ό λ°ννλ ν¨μμ
λλ€.
μ¬μ€ ννκ³Ό κ°μ΅λλ€.
"""
return x, y
μμ ν¨μλ μ«μμ μμμμ μ λ ₯λ°μ $R^2$ 곡κ°μ ν μ , (x,y)λ‘ λ°νν©λλ€.
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)))
μμ ν¨μλ λ μ μ μ§λλ μ§μ μ κΈ°μΈκΈ°λ₯Ό κ³μ°νμ¬ λ°ννλ ν¨μμ λλ€.
μ΄λ₯Ό μ΄μ©νμ¬ λ€μκ³Ό κ°μ΄ λ μ μ μ§λλ μ§μ μ λ°©μ μμ μΆλ ₯ν μ μμ΅λλ€.
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)))
μμμ 곡λΆν μλ£κ΅¬μ‘°μ νΉμ±λ€κ³Ό μ½λ μμ±μ νμν λ¬Έλ²(syntax)μ μ μ§ν€κΈ°λ§ νλ€λ©΄ μ΄λ€ κ³μ°μ΄λ ν΄μ κ°μ λ°ννλ ν¨μλ₯Ό μμ½κ² λ§λ€ μ μμ΅λλ€. μ΄λ λͺ¨λ νλ‘κ·Έλλ° μΈμ΄μμ κ°μ₯ μ€μν λΆλΆμ΄λ―λ‘ μ μμ§ν΄ λμκΈΈ λ°λΌκ² μ΅λλ€. κ°μ¬ν©λλ€.