A A
[Data Mining] Crash_Course in Python Part.1

๊ณต๋ฐฑ ์„œ์‹ ์ง€์ •

  • ๋งŽ์€ ์–ธ์–ด๋“ค์ด ์ฝ”๋“œ ๋ธ”๋ก๋“ค์˜ ๊ฒฝ๊ณ„๋ฅผ ์ •ํ•˜๊ธฐ ์œ„ํ•ด ๊ด„ํ˜ธ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์€ ์ด๊ฑธ indentation(' : ') ์ด๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค.
for i in [1, 2, 3, 4, 5]:
    print(i)
    for j in [1, 2, 3, 4, 5]:
        print(j)
        print(i + j)
    print(i)
print("done looping")
1
1
2
2
3
3
4
4
5
5
6
1
2
1
3
2
4
3
5
4
6
5
7
2
3
1
4
2
5
3
6
4
7
5
8
3
4
1
5
2
6
3
7
4
8
5
9
4
5
1
6
2
7
3
8
4
9
5
10
5
done looping

 

  • ๊ด„ํ˜ธ ๋ฐ ๊ด„ํ˜ธ ์•ˆ์— ๊ณต๋ฐฑ์„ ๋ฌด์‹œํ•˜๋Š” ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค.
long_winded_computation = (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 +
                           13 + 14 + 15 + 16 + 17 + 18 + 19 + 20)

 

  • ์ผ๋ฐ˜์ ์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์ฝ๊ธฐ ์‰ฝ๊ฒŒ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ ์ด๋Ÿฌํ•œ ํ˜•์‹์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
list_of_lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
easier_to_read_list_of_lists = [ [1, 2, 3],
                                 [4, 5, 6],
                                 [7, 8, 9] ]

 

  • ๋˜ํ•œ Backslash(๋ฐฑ์Šฌ๋ž˜์‹œ)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์žฅ์ด ๋‹ค์Œ ํ–‰์œผ๋กœ ๊ณ„์†๋จ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.
two_plus_three = 2 + \\
                 3

Modules

  • ๊ธฐ๋Šฅ์ด ํฌํ•จ๋œ ๋ชจ๋“ˆ ๊ฐ€์ ธ์˜ค๊ธฐ
  • ์ •๊ทœ ํ‘œํ˜„์‹ ๋ชจ๋“ˆ ๊ฐ€์ ธ์˜ค๊ธฐ: ์ •๊ทœ ํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ํ•จ์ˆ˜ ๋ฐ ์ƒ์ˆ˜๋ฅผ ํฌํ•จํ•˜๋Š” ๋ชจ๋“ˆ์ด ์žˆ์Šต๋‹ˆ๋‹ค.
import re
my_regex = re.compile("[0-9]+", re.I)
  • alias๋„ ์‚ฌ์šฉํ• ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
import re as regex
my_regex = regex.compile("[0-9]+", regex.I)
  • ๋˜ํ•œ ์ด๋ ‡๊ฒŒ ๋ช…์‹œ์ ์œผ๋กœ ๊ฐ€์ ธ์™€์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
match = 10
from re import *     # uh oh, re has a match function
print(match)          # "<function re.match>"

# Result: <function match at 0x7cce84681a20>

Arithmetic

  • ์—ฌ๊ธฐ์„œ ์šฐ๋ฆฌ๊ฐ€ ๊ธฐ์–ตํ•ด์•ผ ํ•  ๊ฒƒ์€ ๋ถ„๋ฅ˜ & ์ž”๋ฅ˜ ์ •๋ฆฌ์ž…๋‹ˆ๋‹ค.
n = d * q + r
  • ๐‘‘๋Š” ์•ฝ์ˆ˜, ๐‘ž ๋Š” ๋ชซ, ๐‘Ÿ๋Š” ๋‚˜๋จธ์ง€๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
  • ๐‘ž๊ฐ€ ์–‘์ˆ˜์ผ ๋•Œ , 0≤๐‘Ÿ<๐‘ž, ๐‘ž๊ฐ€ ์Œ์ˆ˜์ผ ๋•Œ ๐‘ž<๐‘Ÿ≤0 ์ž…๋‹ˆ๋‹ค.
print(2 ** 10)       # 1024
print(2 ** 0.5)      # 1.414...
print(2 ** -0.5)     #
print(5 / 2)         # 2.5
print(5 % 3)         # 2
print(5 // 3)        # 1
print((-5) % 3)      # 1
print((-5) // 3)     # -2
print(5 % (-3))      # -1
print((-5) // (-3))  # 1
print((-5) % (-3))   # -2
print(7.2 // 3.5)    # 2.0
print(7.2 % 3.5)     # 0.2

 

Function

  • ํ•จ์ˆ˜๋Š” 0 ์ด์ƒ์˜ ์ž…๋ ฅ์„ ๋ฐ›๊ณ  ํ•ด๋‹น ์ถœ๋ ฅ์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ทœ์น™์ž…๋‹ˆ๋‹ค.
# This
# is
# a
# comment
# CTRL + / toggles comment/uncomment

# for PEP on docstring, refer to <https://www.python.org/dev/peps/pep-0257/#abstract>
def double(x):
    """this is where you put an optional docstring
    that explains what the function does.
    for example, this function multiplies its input by 2"""
    return x * 2

double(2)
help(double)

Help on function double in module __main__:

double(x)
    this is where you put an optional docstring
    that explains what the function does.
    for example, this function multiplies its input by 2

 

  • Python ํ•จ์ˆ˜๋Š” first-class์ด๋ฏ€๋กœ ๋‹ค๋ฅธ ์ธ์ˆ˜์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜๊ณ  ํ•จ์ˆ˜์— ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    • Function(ํ•จ์ˆ˜)๋ฅผ ๋ณ€์ˆ˜์ฒ˜๋Ÿผ Assign ํ•ฉ๋‹ˆ๋‹ค.
def apply_to_one(f):
    """calls the function f with 1 as its argument(์ธ์ž๋กœ ์ „๋‹ฌ)"""
    return f(1)

my_double = double
x = apply_to_one(my_double)

print(x)

 

  • Lambda ํ•จ์ˆ˜: ์งง์€ ์ต๋ช… ํ•จ์ˆ˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
    • ์ด๋ฆ„ ์—†๋Š” Function, ์งง์€ Function๋„ ํฌํ•จ๋ฉ๋‹ˆ๋‹ค.
y = apply_to_one(lambda x: x + 4)
print(y)

# Result: 5
another_double = lambda x: 2 * x
def another_double(x): return 2 * x   # more readable
add = lambda x, y : x + y
add(1,2)

# Result: 3

 

  • ํ•จ์ˆ˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” default ์ธ์ˆ˜๋„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
def my_print(message="my default message"): # message ๊ฐ’์ด ์•„๋‹Œ, ๊ทธ๋ƒฅ ๊ทธ๋Œ€๋กœ print
    print(message)

my_print("hello")  # prints 'hello'
my_print()         # prints 'my default message'
def subtract(a=0, b=0):
    return a - b

subtract(10, 5) # returns 5
subtract(0, 5)  # returns -5
subtract(b=5)   # same as previous
subtract(b=5, a=20)

# Result: 15

String

  • ๋ฌธ์ž์—ด์€ ๋‹จ์ผ ๋˜๋Š” ์ด์ค‘ ๋”ฐ์˜ดํ‘œ๋กœ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
single_quoted_string = 'data science'
double_quoted_string = "data science"
tab_string = "\\t"   # represents the tab character
len(tab_string)     # is 1

# Result: 1

 

  • ์‚ผ์ค‘ ์ด์ค‘ quotes์„ ์‚ฌ์šฉํ•œ ๋‹ค์ค‘ ์ค„ ๋ฌธ์ž์—ด์˜ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค.
multi_line_string = """This is the first line.
and this is the second line
and this is the third line"""

multi_line_string

# Result: 'This is the first line. \\nand this is the second line \\nand this is the third line'

Exceptions

  • ๋ญ”๊ฐ€ ์ž˜๋ชป๋˜๋ฉด Python์€ ์˜ˆ์™ธ๋ฅผ ์ œ๊ธฐํ•ฉ๋‹ˆ๋‹ค.
try:
    print(0 / 0) # ZeroDivisionError๋กœ ๋ฐ”๋กœ ์ด๋™
except ZeroDivisionError:
    print("cannot divide by zero")
    
# Result: cannot divide by zero

Lists

  • List๋Š” ํŒŒ์ด์ฌ์—์„œ ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค.
integer_list = [1, 2, 3]
heterogeneous_list = ["string", 0.1, True]
list_of_lists = [ integer_list, heterogeneous_list, [] ]

list_length = len(integer_list)    # eqauals 3, ๋ฆฌ์ŠคํŠธ์˜ ๊ธธ์ด ๋ฐ˜ํ™˜ (3๊ฐœ)
list_sum    = sum(integer_list)    # equals 6, ๋ฆฌ์ŠคํŠธ์— ์žˆ๋Š” ๋ชจ๋“  ์š”์†Œ์˜ ํ•ฉ์„ ๊ณ„์‚ฐ (1+2+3 = 6)
  • integer_list = [1, 2, 3] - ์ •์ˆ˜ํ˜• List๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
  • heterogeneous_list = ["string", 0.1, True] - ์„œ๋กœ ๋‹ค๋ฅธ type์˜ List๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
  • list_of_lists = [ integer_list, heterogeneous_list, [] ] - ๋ฆฌ์ŠคํŠธ๋ฅผ ์š”์†Œ๋กœ ๊ฐ€์ง€๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.

 

  • ๊ด„ํ˜ธ๊ฐ€ ์žˆ๋Š” ๋ชฉ๋ก์˜ n๋ฒˆ์งธ ์š”์†Œ๋ฅผ ๊ฐ€์ ธ์˜ค๊ฑฐ๋‚˜ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
x = list(range(10))      # is the list [0, 1, ..., 9]
zero = x[0]        # equals 0, lists are 0-indexed
one = x[1]         # equals 1
nine = x[-1]       # equals 9, 'Pythonic' for last   element
eight = x[-2]      # equals 8, 'Pythonic' for next-to-last element
x[0] = -1          # now x is [-1, 1, 2, 3, ..., 9]

 

  • ๋Œ€๊ด„ํ˜ธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ List(๋ชฉ๋ก)์„ "Slice"ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.
first_three = x[:3]               # [-1, 1, 2], -1 ~ 2
three_to_end = x[3:]              # [3, 4, ..., 9], 3๋ถ€ํ„ฐ ์‹œ์ž‘
one_to_four = x[1:5]              # [1, 2, 3, 4], 1๋ถ€ํ„ฐ 4๊นŒ์ง€ ์žˆ๋Š” ๊ฐ’ ์ถœ๋ ฅ
last_three = x[-3:]               # [7, 8, 9], -3๋ถ€ํ„ฐ (๋ฐ˜๋Œ€, ์—ญ์ˆœ)
without_first_and_last = x[1:-1]  # [1, 2, ..., 8] ์•ž์—์„œ 1๋ฒˆ, ๋’ค์—์„œ 1๋ฒˆ ์‚ฌ์ด ์ถœ๋ ฅ
copy_of_x = x[:]                  # [-1, 1, 2, ..., 9] ๋ณต์‚ฌํ•ด์„œ ์ถœ๋ ฅ
print(x[::2]) # ๋ฆฌ์ŠคํŠธ ์œ„์น˜ ์ˆซ์ž 2๋ฒˆ ์•ž์— ๊ฐ’ ์ถœ๋ ฅ, ๋ฐ˜๋ณต
print(x[::-1]) # ๋ฐ˜๋Œ€๋กœ ์ถœ๋ ฅ
# Result

[-1, 2, 4, 6, 8] 
[9, 8, 7, 6, 5, 4, 3, 2, 1, -1]

 

  • in ์—ฐ์‚ฐ์ž๋ฅผ ํ†ตํ•ด ๋ชฉ๋ก ๋ฉค๋ฒ„์‹ญ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
1 in [1, 2, 3]  # True
0 in [1, 2, 3]  # False

# Result: False

 

  • ๋งŒ์•ฝ List๋ฅผ ํ•จ๊ป˜ ์—ฐ๊ฒฐ ํ•˜๋ ค๋ฉด?
x = [1, 2, 3]
x.extend([4, 5, 6])  # x is now [1,2,3,4,5,6] ๋ฆฌ์ŠคํŠธ ํ™•์žฅ
x = [1, 2, 3]
y = x + [4, 5, 6]  # y is [1, 2, 3, 4, 5, 6]; x is unchanged. x๋Š” ๋ณ€ํ•จ์ด ์—†๋‹ค.

 

  • ๋งŒ์•ฝ ํ•œ๋ฒˆ์— ํ•˜๋‚˜์˜ List๋ฅผ ์ถ”๊ฐ€ํ•˜๋ ค๋ฉด?
x = [1, 2, 3]
x.append(0)  # x is now [1, 2, 3, 0]
y = x[-1]    # equals 0 (์—ญ์ˆœ ์ถœ๋ ฅ)
z = len(x)   # equals 4 (List ๊ธธ์ด ์ถœ๋ ฅ)

 

  • ๋งŒ์•ฝ ๋ฆฌ์ŠคํŠธ๋ฅผ ํ•ด์ œํ•˜๋ ค๋ฉด?
x, y = [1, 2]  # now x is 1, y is 2
_, y = [1, 2] # now y == 2, didn't care about the first element.

# '_'๋Š” ์‹ค์ œ๋กœ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฐ’์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

Tuples

  • ํŠœํ”Œ์€ List์˜ ๋ถˆ๋ณ€์˜ ์‚ฌ์ดŒ์ž…๋‹ˆ๋‹ค.
  • ‘ , ‘ ์ด ์žˆ์œผ๋ฉด ํŠœํ”Œ์ž…๋‹ˆ๋‹ค.
  • ๋งŒ์•ฝ ์ƒ๋Œ€์—๊ฒŒ data๋ฅผ ์ค„๋•Œ ๋ณ€ํ˜•์„ ์›ํ•˜์ง€ ์•Š์œผ๋ฉด? → ํŠœํ”Œ๋กœ Listํ™” ํ•˜๋ฉด ์•ˆ๋ฉ๋‹ˆ๋‹ค.
my_list = [1, 2] # ๋ฆฌ์ŠคํŠธ, ๊ฐ’ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ
my_tuple = (1, 2) # ํŠœํ”Œ, ๊ฐ’ ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€
other_tuple = 3, 4 # ํŠœํ”Œ์„ ์ƒ์„ฑํ•˜๋Š” ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•. ๊ฐ’๋“ค์„ ์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ํŠœํ”Œ ์ƒ์„ฑ ๊ฐ€๋Šฅ
my_list[1] = 3   # my_list is now [1, 3]

try:
    my_tuple[1] = 3 # ํŠœํ”Œ์˜ 2๋ฒˆ์งธ ์š”์†Œ๋ฅผ 3์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋ ค๊ณ  ํ•จ. ํŠœํ”Œ์€ ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€๋Šฅ ํ•ด์„œ ์—ฐ์‚ฐ๋ถˆ๊ฐ€
except TypeError:
    print("cannot modify a tuple")
    
# Result: cannot modify a tuple

 

  • ํŠœํ”Œ์€ ํ•จ์ˆ˜์—์„œ ์—ฌ๋Ÿฌ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํŽธ๋ฆฌํ•œ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.
def sum_and_product(x, y):
    return (x + y),(x * y)

sp = sum_and_product(2, 3)    # equals (5, 6)
# ํ•จ์ˆ˜์—์„œ ๋ฐ˜ํ™˜๋œ ํŠœํ”Œ์€ (5, 6)์ด๋ฏ€๋กœ sp๋Š” (5, 6)

s, p = sum_and_product(5, 10) # s is 15, p is 50, s = x + y, p = x * y
# ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๋ฐ˜ํ™˜๋œ ํŠœํ”Œ์„ s์™€ p ๋ณ€์ˆ˜์— ๊ฐ๊ฐ ์ €์žฅ
# ํ•จ์ˆ˜์—์„œ ๋ฐ˜ํ™˜๋œ ํŠœํ”Œ์€ (15, 50)์ด๋ฏ€๋กœ s๋Š” 15์ด๊ณ  p๋Š” 50์ด ๋ฉ๋‹ˆ๋‹ค.

 

  • Tuple(๋ฐ ๋ชฉ๋ก)์€ multiple assignment:์—๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
x, y = 1, 2   # now x is 1, y is 2
x, y = y, x   # Pythonic way to swap variables; now x is 2, y is 1

Dictionaries

  • ๊ฐ’๊ณผ ํ‚ค๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ๋˜ ๋‹ค๋ฅธ ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค.
  • ์ฃผ์–ด์ง„ ํ‚ค์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์„ ๋น ๋ฅด๊ฒŒ ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
empty_dict = {}                      # Pythonic
empty_dict2 = dict()                 # less Pythonic
grades = { "Joel" : 80, "Tim" : 95 } # dictionary literal. Joel: Key, Tim: Value

 

  • ๋Œ€๊ด„ํ˜ธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ‚ค ๊ฐ’์„ ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
joels_grade = grades["Joel"]         # equals 80
grades = { "Joel" : 80, "Tim" : 95, "Tim" : 94 } # dictionary literal
grades

# Result: {'Joel': 80, 'Tim': 94}, Key ๊ฐ’์ด ์ค‘๋ณต์ด ๋˜๋ฉด ๋งˆ์ง€๋ง‰์— ์ง€์ •๋œ ๊ฐ’์ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
len(grades) # 2
grades.keys() # dict_keys(['Joel', 'Tim'])
grades.values() # dict_values([80, 94])
grades["Tim"] # 94

 

  • ๋งŒ์•ฝ KeyError,์‚ฌ์ „์— ์—†๋Š” ํ‚ค๋ฅผ ์š”์ฒญํ•˜๋Š” ๊ฒฝ์šฐ์—” ์ด๋ ‡๊ฒŒ ๋‚˜์˜ต๋‹ˆ๋‹ค.
try:
    kates_grade = grades["Kate"]
except KeyError:
    print("no grade for Kate!")
    
# no grade for Kate! 

 

  • ์ด๋ ‡๊ฒŒ ํ‚ค์˜ ์กด์žฌ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
joel_has_grade = "Joel" in grades    # True
kate_has_grade = "Kate" in grades    # False

 

  • ์‚ฌ์ „์—๋Š” ์‚ฌ์ „์— ์—†๋Š” ํ‚ค๋ฅผ ๊ฒ€์ƒ‰ํ•  ๋•Œ ์˜ˆ์™ธ๋ฅผ ๋†’์ด๋Š” ๋Œ€์‹  ๊ธฐ๋ณธ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” get ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค:
    • ๋งŒ์•ฝ ๊ธฐ๋ณธ๊ฐ’์„ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ๊ธฐ๋ณธ๊ฐ’์€ None์œผ๋กœ ๋‚˜์˜ต๋‹ˆ๋‹ค.
joels_grade = grades.get("Joel", 0)    # equals 80, ๋งŒ์•ฝ Key(Joel)์ด ์—†์œผ๋ฉด ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ 0 ์‚ฌ์šฉ
kates_grade = grades.get("Kate", 0)    # equals 0, Kate ํ‚ค๋Š” ๋”•์…”๋„ˆ๋ฆฌ์— ์—†์œผ๋ฏ€๋กœ ๊ธฐ๋ณธ๊ฐ’์ธ 0์„ ๋ฐ˜ํ™˜
no_ones_grade = grades.get("No One")   # default default is None, ํ‚ค๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ None์„ ์‚ฌ์šฉ
no_ones_grade == None # no_ones_grade์— ํ• ๋‹น๋œ ๊ฐ’์ด None์ธ์ง€๋ฅผ ํ™•์ธ. 

# Result: True

 

  • ๋™์ผํ•œ ๋Œ€๊ด„ํ˜ธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Key-Value ์Œ์„ ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค.
grades["Tim"] = 99          # replaces the old value
grades["Kate"] = 100        # adds a third entry
num_students = len(grades)  # equals 3

 

  • ์ด๋ ‡๊ฒŒ ์ •ํ˜• ๋ฐ์ดํ„ฐ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ Dictionary๋ฅผ ์ž์ฃผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
tweet = {
    "user" : "joelgrus",
    "text" : "Data Science is Awesome",
    "retweet_count" : 100,
    "hashtags" : ["#data", "#science", "#datascience", "#awesome", "#yolo"]
}

 

 

  • Iteration ์œผ๋กœ ๋ชจ๋‘ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
tweet_keys   = tweet.keys()     # ๋”•์…”๋„ˆ๋ฆฌ์˜ ๋ชจ๋“  key๋ฅผ ๊ฐ€์ ธ์™€์„œ ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜ (key-์ด๋ฆ„)
tweet_values = tweet.values()   # ๋”•์…”๋„ˆ๋ฆฌ์˜ ๋ชจ๋“  value๋“ค์„ ๊ฐ€์ ธ์™€์„œ ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜ (text)
tweet_items  = tweet.items()    # ๋”•์…”๋„ˆ๋ฆฌ์˜ ๋ชจ๋“  (ํ‚ค, ๊ฐ’) ์Œ์„ ๊ฐ€์ ธ์™€์„œ ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜

"user" in tweet_keys            # True, but uses a slow list in
# tweet_keys๋ผ๋Š” ๋ณ€์ˆ˜์— ์ €์žฅ๋œ ๋ฆฌ์ŠคํŠธ์—์„œ "user"๋ฅผ ์ฐพ๋Š” ์ž‘์—…์„ ์ˆ˜ํ–‰

"user" in tweet                 # more Pythonic, uses faster dict in
# ๋”•์…”๋„ˆ๋ฆฌ์˜ ํ‚ค๋ฅผ ๋” ํšจ์œจ์ ์œผ๋กœ ํ™•์ธ

"joelgrus" in tweet_values      # True
# tweet_values์—๋Š” ๋”•์…”๋„ˆ๋ฆฌ์˜ ๊ฐ’๋“ค์ด ๋“ค์–ด ์žˆ์œผ๋ฏ€๋กœ, ์ด ์ฝ”๋“œ๋Š” "joelgrus"๋ผ๋Š” ๊ฐ’์ด tweet ๋”•์…”๋„ˆ๋ฆฌ์˜ ๊ฐ’ ์ค‘์— ์žˆ๋Š”์ง€๋ฅผ ํ™•์ธ

# Result: True
tweet_values

# Result: dict_values(['joelgrus', 'Data Science is Awesome', 100, ['#data', '#science', '#datascience', '#awesome', '#yolo']])

 

  • WordCount Example: Key๊ฐ€ ๋‹จ์–ด์ด๊ณ  Value๊ฐ€ ์นด์šดํŠธ์ธ ์‚ฌ์ „์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
document = ['I', 'am', 'a', 'boy', 'I', 'love', 'you']

 

  • First Approach
word_counts = {}  # ๋‹จ์–ด์˜ ๋“ฑ์žฅ ํšŸ์ˆ˜๋ฅผ ์ €์žฅํ•  ๋นˆ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
for word in document:  # ๋ฌธ์„œ(document)์—์„œ ๊ฐ ๋‹จ์–ด(word)๋ฅผ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.
    if word in word_counts:  # ํ˜„์žฌ ๊ฐ€์ ธ์˜จ ๋‹จ์–ด๊ฐ€ ์ด๋ฏธ word_counts ๋”•์…”๋„ˆ๋ฆฌ์— ์žˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
        word_counts[word] += 1  # ๋งŒ์•ฝ ๋‹จ์–ด๊ฐ€ ์ด๋ฏธ ๋”•์…”๋„ˆ๋ฆฌ์— ์žˆ๋‹ค๋ฉด ํ•ด๋‹น ๋‹จ์–ด์˜ ๋“ฑ์žฅ ํšŸ์ˆ˜๋ฅผ 1 ์ฆ๊ฐ€์‹œํ‚ต๋‹ˆ๋‹ค.
    else:
        word_counts[word] = 1  # ๋งŒ์•ฝ ๋‹จ์–ด๊ฐ€ ๋”•์…”๋„ˆ๋ฆฌ์— ์—†๋‹ค๋ฉด ํ•ด๋‹น ๋‹จ์–ด๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ๋“ฑ์žฅ ํšŸ์ˆ˜๋ฅผ 1๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

  • Second Approach
word_counts = {}  # ๋‹จ์–ด์˜ ๋“ฑ์žฅ ํšŸ์ˆ˜๋ฅผ ์ €์žฅํ•  ๋นˆ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
for word in document:  # ๋ฌธ์„œ(document)์—์„œ ๊ฐ ๋‹จ์–ด(word)๋ฅผ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.
    try:
        word_counts[word] += 1  # ๋‹จ์–ด๊ฐ€ ์ด๋ฏธ ๋”•์…”๋„ˆ๋ฆฌ์— ์žˆ๋Š” ๊ฒฝ์šฐ ๋“ฑ์žฅ ํšŸ์ˆ˜๋ฅผ 1 ์ฆ๊ฐ€์‹œํ‚ต๋‹ˆ๋‹ค.
    except KeyError:  # KeyError๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฒฝ์šฐ (๋‹จ์–ด๊ฐ€ ๋”•์…”๋„ˆ๋ฆฌ์— ์—†๋Š” ๊ฒฝ์šฐ)
        word_counts[word] = 1  # ์ƒˆ๋กœ์šด ๋‹จ์–ด๋กœ ์ถ”๊ฐ€ํ•˜๊ณ  ๋“ฑ์žฅ ํšŸ์ˆ˜๋ฅผ 1๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
  • Third Approach
word_counts = {}  # ๋‹จ์–ด์˜ ๋“ฑ์žฅ ํšŸ์ˆ˜๋ฅผ ์ €์žฅํ•  ๋นˆ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
for word in document:  # ๋ฌธ์„œ(document)์—์„œ ๊ฐ ๋‹จ์–ด(word)๋ฅผ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.
    previous_count = word_counts.get(word, 0)  # ํ•ด๋‹น ๋‹จ์–ด์˜ ์ด์ „ ๋“ฑ์žฅ ํšŸ์ˆ˜๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. ์—†์œผ๋ฉด ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ 0์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
    word_counts[word] = previous_count + 1  # ์ด์ „ ๋“ฑ์žฅ ํšŸ์ˆ˜์— 1์„ ๋”ํ•˜์—ฌ ํ˜„์žฌ ๋‹จ์–ด์˜ ๋“ฑ์žฅ ํšŸ์ˆ˜๋ฅผ ์—…๋ฐ์ดํŠธํ•ฉ๋‹ˆ๋‹ค.

word_counts

# Result: {'I': 2, 'am': 1, 'a': 1, 'boy': 1, 'love': 1, 'you': 1}

Defaultdict

  • ๊ธฐ๋ณธ ๋”•์…˜์€ ํฌํ•จ๋˜์ง€ ์•Š์€ ํ‚ค๋ฅผ ๊ฒ€์ƒ‰ํ•˜๋ ค๊ณ  ํ•  ๋•Œ ์ƒ์„ฑํ•  ๋•Œ ์ œ๊ณตํ•œ zero-argument ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋จผ์ € ํ•ด๋‹น ํ‚ค์— ๋Œ€ํ•œ ๊ฐ’์„ ์ถ”๊ฐ€ํ•œ๋‹ค๋Š” ์ ์„ ์ œ์™ธํ•˜๊ณ  ์ผ๋ฐ˜ Dictionary์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.
from collections import defaultdict

word_counts = defaultdict(int)    # int()์€ 0์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ 0์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
# word_counts = defaultdict(lambda: 100)    # lambda ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ 100์„ ๋ฐ˜ํ™˜ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

for word in document:  # ๋ฌธ์„œ(document)์—์„œ ๊ฐ ๋‹จ์–ด(word)๋ฅผ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.
    word_counts[word] += 1  # ํ•ด๋‹น ๋‹จ์–ด์˜ ๋“ฑ์žฅ ํšŸ์ˆ˜๋ฅผ 1 ์ฆ๊ฐ€์‹œํ‚ต๋‹ˆ๋‹ค.

print(word_counts)  # ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

# defaultdict(<class 'int'>, {'I': 2, 'am': 1, 'a': 1, 'boy': 1, 'love': 1, 'you': 1})
int() # 0
# defaultdict๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ๋นˆ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ฐ€์ง€๋Š” ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ดˆ๊ธฐํ™”ํ•ฉ๋‹ˆ๋‹ค.
dd_list = defaultdict(list)

# ํ‚ค 2์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์ธ ๋ฆฌ์ŠคํŠธ์— 1์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
# ์ด์ „์— ์กด์žฌํ•˜์ง€ ์•Š๋Š” ํ‚ค์˜€์œผ๋ฏ€๋กœ ๋นˆ ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ƒ์„ฑ๋œ ํ›„์— 1์ด ์ถ”๊ฐ€๋ฉ๋‹ˆ๋‹ค.
dd_list[2].append(1)

# defaultdict๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ๋นˆ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๊ฐ€์ง€๋Š” ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ดˆ๊ธฐํ™”ํ•ฉ๋‹ˆ๋‹ค.
dd_dict = defaultdict(dict)

# ํ‚ค "Joel"์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์ธ ๋”•์…”๋„ˆ๋ฆฌ์˜ "City" ํ‚ค์— "Seattle"์„ ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค.
# ์ด์ „์— ์กด์žฌํ•˜์ง€ ์•Š๋Š” ํ‚ค์˜€์œผ๋ฏ€๋กœ ๋นˆ ๋”•์…”๋„ˆ๋ฆฌ๊ฐ€ ์ƒ์„ฑ๋œ ํ›„์— "City" ํ‚ค๊ฐ€ ์ถ”๊ฐ€๋˜๊ณ  "Seattle"์ด ํ• ๋‹น๋ฉ๋‹ˆ๋‹ค.
dd_dict["Joel"]["City"] = "Seattle"

# defaultdict๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ [0, 0] ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ฐ€์ง€๋Š” ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ดˆ๊ธฐํ™”ํ•ฉ๋‹ˆ๋‹ค.
dd_pair = defaultdict(lambda: [0, 0])

# ํ‚ค 2์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์ธ ๋ฆฌ์ŠคํŠธ์˜ ์ธ๋ฑ์Šค 1์— 1์„ ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค.
# ์ด์ „์— ์กด์žฌํ•˜์ง€ ์•Š๋Š” ํ‚ค์˜€์œผ๋ฏ€๋กœ [0, 0] ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ƒ์„ฑ๋œ ํ›„์— ํ•ด๋‹น ์œ„์น˜์— 1์ด ํ• ๋‹น๋ฉ๋‹ˆ๋‹ค.
dd_pair[2][1] = 1

Counter

์นด์šดํ„ฐ๋Š” ์ผ๋ จ์˜ ๊ฐ’์„ ๊ธฐ๋ณธ dict(int)์™€ ๊ฐ™์€ ๊ฐœ์ฒด ๋งคํ•‘ ํ‚ค๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ์นด์šดํŠธํ•ฉ๋‹ˆ๋‹ค.
  • histograms๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐ ์ฃผ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
from collections import Counter

# Counter๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ ๋‚ด์˜ ๊ฐ ์š”์†Œ์˜ ๋“ฑ์žฅ ํšŸ์ˆ˜๋ฅผ ์„ธ์–ด c์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
c = Counter([0, 1, 2, 0])  # c๋Š” (๊ธฐ๋ณธ์ ์œผ๋กœ) { 0 : 2, 1 : 1, 2 : 1 }
# Counter๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์„œ(document)์—์„œ ๊ฐ ๋‹จ์–ด์˜ ๋“ฑ์žฅ ํšŸ์ˆ˜๋ฅผ ์„ธ๊ณ  word_counts์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
word_counts = Counter(document)

# ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
print(word_counts)

# Counter({'I': 2, 'am': 1, 'a': 1, 'boy': 1, 'love': 1, 'you': 1})
  • ๋ฌธ์„œ(document)์—์„œ ๊ฐ ๋‹จ์–ด์˜ ๋“ฑ์žฅ ํšŸ์ˆ˜๋ฅผ ์„ธ์–ด ๋”•์…”๋„ˆ๋ฆฌ ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜
  • word_counts ๋ณ€์ˆ˜์—๋Š” ๋ฌธ์„œ ๋‚ด์˜ ๊ฐ ๋‹จ์–ด์˜ ๋“ฑ์žฅ ํšŸ์ˆ˜๊ฐ€ ์ €์žฅ

 

  • help ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜์—ฌ main ํŽ˜์ด์ง€ ๋ณด๊ธฐ
help(word_counts)
# Counter์˜ most_common() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋นˆ๋ฒˆํ•˜๊ฒŒ ๋“ฑ์žฅํ•˜๋Š” ๋‹จ์–ด ์ƒ์œ„ 10๊ฐœ, ๋“ฑ์žฅ ํšŸ์ˆ˜๋ฅผ ์ถœ๋ ฅ.
for word, count in word_counts.most_common(10):
    print(word, count)
I 2
am 1
a 1
boy 1
love 1
you 1

Sets

๋˜ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋Š” set์ด๋ฉฐ, ์ด๋Š” distinct ์š”์†Œ์˜ ์ง‘ํ•ฉ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.
s = set()
s.add(1)      # s is now { 1 }
s.add(2)      # s is now { 1, 2 }
s.add(2)      # s is still { 1, 2 }
x = len(s)    # equals 2
y = 2 in s    # equals True
z = 3 in s    # equals False
  • ๊ตฌ์„ฑ์› ์ž๊ฒฉ ํ…Œ์ŠคํŠธ์˜ ๊ฒฝ์šฐ ๋ชฉ๋ก๋ณด๋‹ค set๊ฐ€ ๋” ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค
  • in์€ set์—์„œ ๋งค์šฐ ๋น ๋ฅธ ์ž‘์—…์ž…๋‹ˆ๋‹ค.
hundreds_of_other_words = []
stopwords_list = ["a", "an", "at"] + hundreds_of_other_words + ["yet", "you"]

# ๋ฆฌ์ŠคํŠธ๋ฅผ ์ด์šฉํ•˜์—ฌ "zip"์ด stopwords์— ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. 
# ๋ฆฌ์ŠคํŠธ๋ฅผ ์ด์šฉํ•œ ๋ฉค๋ฒ„์‹ญ ๊ฒ€์‚ฌ๋Š” ๋ชจ๋“  ์š”์†Œ๋ฅผ ํ™•์ธํ•ด์•ผ ํ•˜๋ฏ€๋กœ ๋Š๋ฆฝ๋‹ˆ๋‹ค.
"zip" in stopwords_list  # False

# ์ง‘ํ•ฉ์„ ์ด์šฉํ•˜์—ฌ "zip"์ด stopwords์— ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
# ์ง‘ํ•ฉ์„ ์ด์šฉํ•œ ๋ฉค๋ฒ„์‹ญ ๊ฒ€์‚ฌ๋Š” ๋งค์šฐ ๋น ๋ฅด๋ฉฐ ํ•ด์‹ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฉค๋ฒ„๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค.
stopwords_set = set(stopwords_list)
"zip" in stopwords_set  # False

  • ๋งŒ์•ฝ Collection์—์„œ distinct ํ•ญ๋ชฉ์„ ์ฐพ์œผ๋ ค๋ฉด?
item_list = [1, 2, 3, 1, 2, 3]
num_items = len(item_list)          # 6
item_set = set(item_list)           # {1, 2, 3}
num_distinct_items = len(item_set)  # 3
distinct_item_list = list(item_set) # [1, 2, 3]

Control Flow

if 1 > 2:
    message = "if only 1 were greater than two..."
elif 1 > 3:
    message = "elif stands for 'else if'"
else:
    message = "when all else fails use else (if you want to)"
  • ์ผ์ง์„ ์ƒ์˜ ์‚ผ์›ํ˜• else
parity = "even" if x % 2 == 0 else "odd"
  • while statement:
x = 0
while x < 10:
    print(x, "is less than 10")
    x += 1
0 is less than 10
1 is less than 10
2 is less than 10
3 is less than 10
4 is less than 10
5 is less than 10
6 is less than 10
7 is less than 10
8 is less than 10
9 is less than 10
  • for statement.
for x in range(10):
    print(x, "is less than 10")
0 is less than 10
1 is less than 10
2 is less than 10
3 is less than 10
4 is less than 10
5 is less than 10
6 is less than 10
7 is less than 10
8 is less than 10
9 is less than 10
  • continue & break statement
for x in range(10):
    if x == 3:
        continue   # go immediately to the next iteration
    if x == 5:
        break      # quit the loop entirely
    print(x)
0
1
2
4

Truthiness

one_is_less_than_two = 1 < 2       # equals True, ์ฐธ - True ํ• ๋‹น
true_equals_false = True == False  # equals False, True & False๊ฐ€ ๊ฐ™์€์ง€ ๋น„๊ต. ๋‹ค๋ฅด๋‹ˆ๊นŒ False
  • python์€ None ๊ฐ’์„ ์‚ฌ์šฉํ•˜์—ฌ ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฐ’์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.
x = None
print(x == None)   # prints True, but is not Pythonic
print(x is None)   # prints True, and is Pythonic

# True
# True
  • ๋‹ค์Œ์€ ๋ชจ๋‘ "Falsy"์ž…๋‹ˆ๋‹ค.
False
None
[] : (an empty list)
{} : (an empty dict)
""
set()
0
0.0
s = 'abc'
if s: # if s:๋Š” ๋ฌธ์ž์—ด s๊ฐ€ ๋น„์–ด์žˆ์ง€ ์•Š์œผ๋ฉด True๋ฅผ ๋ฐ˜ํ™˜
    first_char = s[0]
else: # ๋งŒ์•ฝ ๋ฌธ์ž์—ด์ด ๋น„์–ด์žˆ์œผ๋ฉด ๋นˆ ๋ฌธ์ž์—ด("")์ด first_char ๋ณ€์ˆ˜์— ํ• ๋‹น
    first_char = ""
# and ์—ฐ์‚ฐ์ž๋Š” ์ฒซ ๋ฒˆ์งธ ํ”ผ์—ฐ์‚ฐ์ž์ธ s๊ฐ€ ์ฐธ์ธ์ง€ ํ™•์ธ,
# ๊ทธ ๊ฒฐ๊ณผ์— ๋”ฐ๋ผ ๋‘ ๋ฒˆ์งธ ํ”ผ์—ฐ์‚ฐ์ž์ธ s[0]์„ ๋ฐ˜ํ™˜.
# ๋งŒ์•ฝ s๊ฐ€ ๋น„์–ด์žˆ์ง€ ์•Š์œผ๋ฉด s[0]์ด ๋ฐ˜ํ™˜๋˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด s ์ž์ฒด๊ฐ€ ๋ฐ˜ํ™˜

first_char = s and s[0]   # A simpler way of doing the same
x = None
safe_x = x or 0    # ์ฒซ ๋ฒˆ์งธ ํ”ผ์—ฐ์‚ฐ์ž์ธ x๋ฅผ ํ™•์ธํ•˜๊ณ , ๋งŒ์•ฝ x๊ฐ€ ๊ฑฐ์ง“ ๊ฐ’์ด๋ผ๋ฉด(์—ฌ๊ธฐ์„œ๋Š” None) ๋‘ ๋ฒˆ์งธ ํ”ผ์—ฐ์‚ฐ์ž์ธ 0์ด ๋ฐ˜ํ™˜
safe_x

# safe_x์—๋Š” x์˜ ๊ฐ’์ด ํ• ๋‹น๋˜์ง€๋งŒ, x๊ฐ€ None์ด๋ฉด 0์ด ํ• ๋‹น
# Result: 0
  • Python์—๋Š” ๋ชฉ๋ก์„ ๊ฐ€์ ธ ๋ชจ๋“  ์š”์†Œ๊ฐ€ ์ฐธ์ผ ๋•Œ True๋ฅผ ์ •ํ™•ํžˆ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ชจ๋“  ํ•จ์ˆ˜์™€ ์ ์–ด๋„ ํ•˜๋‚˜์˜ ์š”์†Œ๊ฐ€ ์ฐธ์ผ ๋•Œ True๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ชจ๋“  ํ•จ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
all([True, 1, { 3 }])    # True
all([True, 1, {}])       # False, {} is falsy
any([True, 1, {}])       # True, True is truthy, ์ฃผ์–ด์ง„ iterable์˜ ์š”์†Œ ์ค‘ ํ•˜๋‚˜๋ผ๋„ ์ฐธ์ด๋ฉด True๋ฅผ ๋ฐ˜ํ™˜
all([])                  # True, no falsy elements in the list, ๊ฑฐ์ง“ ์š”์†Œ๊ฐ€ ์—†์œผ๋ฏ€๋กœ ์ฐธ
any([])                  # False, no truthy elements in the list, ์ฐธ์ธ ์š”์†Œ๊ฐ€ ์—†์œผ๋ฏ€๋กœ False
all([] + [True, True]) == all([]) and all([True, True])

# True
  • ๋นˆ ๋ฆฌ์ŠคํŠธ []์™€ [True, True]๋ฅผ ๊ฒฐํ•ฉํ•˜๋ฉด [True, True]์ด ๋˜๊ณ , all() ํ•จ์ˆ˜๋Š” ์ด ๋ฆฌ์ŠคํŠธ์˜ ๋ชจ๋“  ์š”์†Œ๊ฐ€ ์ฐธ์ด์–ด์•ผ ํ•˜๋ฏ€๋กœ ๊ฒฐ๊ณผ๋Š” True๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.
  • all([])๋Š” ๋นˆ ๋ฆฌ์ŠคํŠธ์ด๋ฏ€๋กœ ๋ชจ๋“  ์š”์†Œ๊ฐ€ ์ฐธ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ฒซ ๋ฒˆ์งธ ๋ถ€๋ถ„์€ True๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  all([True, True])๋„ True์ž…๋‹ˆ๋‹ค.
any([] + [True, False]) == any([]) or any([True, False])

# True, ๋นˆ ๋ฆฌ์ŠคํŠธ์™€ [True, False]๋ฅผ ์—ฐ๊ฒฐํ•œ ๋ฆฌ์ŠคํŠธ์—๋Š” ์ ์–ด๋„ ํ•˜๋‚˜์˜ True๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ
  • ๋นˆ ๋ฆฌ์ŠคํŠธ []์™€ [True, False]๋ฅผ ์—ฐ๊ฒฐํ•˜์—ฌ [True, False]๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
    • ๊ทธ๋Ÿฐ ๋‹ค์Œ any() ํ•จ์ˆ˜๋Š” ์ด ๋ฆฌ์ŠคํŠธ์˜ ์š”์†Œ ์ค‘ ํ•˜๋‚˜๋ผ๋„ True์ธ์ง€ ํ™•์ธํ•˜์ง€๋งŒ, ๋‘ ์š”์†Œ ๋ชจ๋‘ False์ด๋ฏ€๋กœ ๊ฒฐ๊ณผ๋Š” False ์ž…๋‹ˆ๋‹ค.
  • any([]): ์ด ์‹์€ ๋นˆ ๋ฆฌ์ŠคํŠธ์—๋Š” ์š”์†Œ๊ฐ€ ์—†์œผ๋ฏ€๋กœ False๋กœ ํ‰๊ฐ€
  • any([True, False]): ์ด ์‹์€ ์ตœ์†Œํ•œ ํ•˜๋‚˜์˜ ์š”์†Œ (True)๊ฐ€ True์ž„์„ ํ™•์ธํ•˜๊ธฐ ๋•Œ๋ฌธ์— True๋กœ ํ‰๊ฐ€๋ฉ๋‹ˆ๋‹ค.