In [5]:
def sum(*args):
    total = 0
    for arg in args:
        total += arg
    return total

In [6]:
sum(1,2,3,4)


Out[6]:
10

In [7]:
def greeting(name, why, course):
    print("{name} | {course} | {why}".format(name = name, course = course, why=why))

In [8]:
values = ("jy", "DataScientist", "study")

In [9]:
# unpack / unpacking
greeting(*values)


jy | study | DataScientist

In [ ]:
# unnamed -> *args (tuple)
# named -> **kwargs (dict)

In [10]:
def information(**kwargs):
    for key, value in kwargs.items():
        print ("(key) -> (value)".format(key = key, value = value))

In [13]:
information = {"name" : "jy", "email" : "jiyong@"}

In [14]:
print information


{'name': 'jy', 'email': 'jiyong@'}

In [28]:
# Lambda

In [30]:
increment_lambda = lambda x : x+1

In [32]:
increment_lambda(23)


Out[32]:
24

In [35]:
(lambda x,y : x+y)(1,2)


Out[35]:
3

In [ ]:
# [ 1, 2, 3] -> [1, 4, 9]

In [36]:
def change(numbers):
    list = []
    for number in numbers:
        list.append(number**2)
    return list

In [37]:
change([1,2,3])


Out[37]:
[1, 4, 9]

In [38]:
def square(x):
    return x**2

In [39]:
list(map(square, [1,2,3]))


Out[39]:
[1, 4, 9]

In [40]:
list(map(lambda x: x**2, [1,2,3,4]))


Out[40]:
[1, 4, 9, 16]

In [43]:
map(lambda x : x**2, [1,2,3,4])


Out[43]:
[1, 4, 9, 16]

In [44]:
numbers = [1, 2, 3, 4, 5, 6, 7, 8]

In [48]:
list(filter(lambda x : x>5, numbers))


Out[48]:
[6, 7, 8]

In [ ]:
# map -> 모든 elements -> 새로운 List
# filter -> 모든 elements => True인 Element 만 새로운 List

In [ ]:
# Reduce -> 줄이고, 하나만 남기는

In [50]:
functools.reduce(lambda x,y : x+y, [10, 20, 30, 40])


Out[50]:
100

In [53]:
def max(numbers):
    max_number = numbers[0]
    for number in numbers:
        if number > max_number:
            max_number = number
            
    return max_number

In [54]:
max([1,14,7,8,4])


Out[54]:
14

In [69]:
reduce(lambda x,y : x if x<y else y, [1,14,7,8,4])


Out[69]:
1

In [70]:
awesome_list = [1,2, "jy", {}, [], 4,5]

In [76]:
list(map(lambda x : x**2, list(filter(lambda x: isinstance(x, int), awesome_list))))


Out[76]:
[1, 4, 16, 25]

In [81]:
# 1 - 100 까지 숫자 중에서 짝수
[i **2 for i in range(1, 101) if i%2 == 0]


Out[81]:
[4,
 16,
 36,
 64,
 100,
 144,
 196,
 256,
 324,
 400,
 484,
 576,
 676,
 784,
 900,
 1024,
 1156,
 1296,
 1444,
 1600,
 1764,
 1936,
 2116,
 2304,
 2500,
 2704,
 2916,
 3136,
 3364,
 3600,
 3844,
 4096,
 4356,
 4624,
 4900,
 5184,
 5476,
 5776,
 6084,
 6400,
 6724,
 7056,
 7396,
 7744,
 8100,
 8464,
 8836,
 9216,
 9604,
 10000]

In [92]:
MAX = 1000

not_prime_list = [
    j
    for i in range(2, int(MAX **0.5)+1)
    for j in range(i**2, MAX, i) 
]
prime_list = [
    i
    for i in range(2, MAX +1)
    if i not in not_prime_list

]
prime_list


Out[92]:
[2,
 3,
 5,
 7,
 11,
 13,
 17,
 19,
 23,
 29,
 31,
 37,
 41,
 43,
 47,
 53,
 59,
 61,
 67,
 71,
 73,
 79,
 83,
 89,
 97,
 101,
 103,
 107,
 109,
 113,
 127,
 131,
 137,
 139,
 149,
 151,
 157,
 163,
 167,
 173,
 179,
 181,
 191,
 193,
 197,
 199,
 211,
 223,
 227,
 229,
 233,
 239,
 241,
 251,
 257,
 263,
 269,
 271,
 277,
 281,
 283,
 293,
 307,
 311,
 313,
 317,
 331,
 337,
 347,
 349,
 353,
 359,
 367,
 373,
 379,
 383,
 389,
 397,
 401,
 409,
 419,
 421,
 431,
 433,
 439,
 443,
 449,
 457,
 461,
 463,
 467,
 479,
 487,
 491,
 499,
 503,
 509,
 521,
 523,
 541,
 547,
 557,
 563,
 569,
 571,
 577,
 587,
 593,
 599,
 601,
 607,
 613,
 617,
 619,
 631,
 641,
 643,
 647,
 653,
 659,
 661,
 673,
 677,
 683,
 691,
 701,
 709,
 719,
 727,
 733,
 739,
 743,
 751,
 757,
 761,
 769,
 773,
 787,
 797,
 809,
 811,
 821,
 823,
 827,
 829,
 839,
 853,
 857,
 859,
 863,
 877,
 881,
 883,
 887,
 907,
 911,
 919,
 929,
 937,
 941,
 947,
 953,
 967,
 971,
 977,
 983,
 991,
 997,
 1000]