# 01 리스트(Lists) 접근하기
n = [1, 3, 5]
# Add your code below
print (n[1])
3
# 02 리스트 요소(List element) 수정
n = [1, 3, 5]
# Do your multiplication here
n[1] *= 5
print (n)
[1, 15, 5]
# 03 리스트(List)에 추가하기 (Appending)
n = [1, 3, 5]
# Append the number 4 here
n.append(4)
print (n)
[1, 3, 5, 4]
# 04 리스트(lists)에서 요소 제거
n = [1, 3, 5]
# Remove the first item in the list here
n.pop(0)
print (n)
[3, 5]
# 05 함수(Function)의 기능(Functionality) 변경하기
number = 5
def my_function(x):
return x * 3
print (my_function(number) )
15
# 06 하나 이상의 인자(argument)
m = 5
n = 13
# Add add_function here!
def add_function(x, y):
return x + y
print (add_function(m, n) )
18
# 07 임의 갯수의 인자(argument)
m = 5
n = 13
# Add add_function here!
def add_function(*nums):
return sum(nums)
print (add_function(m, n) )
18
# 08 함수(Function) 안의 문자열
n = "Hello"
# Your function here!
def string_function(s):
return s + "world"
print (string_function(n))
Helloworld
# 09 리스트(Lists)를 함수(Function)에 전달하기
def list_function(x):
return x
n = [3, 5, 7]
print (list_function(n))
[3, 5, 7]
# 10 함수 내의 리스트로부터 요소 사용하기
def list_function(x):
return x[1]
n = [3, 5, 7]
print (list_function(n))
5
# 11 함수 내의 리스트에 들어있는 요소 변경하기
def list_function(x):
x[1] += 3
return x
n = [3, 5, 7]
print (list_function(n))
[3, 8, 7]
# 12 함수 내의 리스트 조작
n = [3, 5, 7]
# Add your function here
def list_extender(lst):
lst.append(9)
return lst
print (list_extender(n))
[3, 5, 7, 9]
# 13 함수 내의 리스트를 요소별로 출력하기
n = [3, 5, 7]
def print_list(x):
for i in range(0, len(x)):
print (x[i])
print_list(n)
3 5 7
# 14 함수 내의 리스트에 들어있는 각각의 요소를 변경하기
n = [3, 5, 7]
def double_list(x):
for i in range(0, len(x)):
x[i] = x[i] * 2
return x
# Don't forget to return your new list!
print (double_list(n) )
[6, 10, 14]
# 15 함수에 레인지(Range) 전달하기
def my_function(x):
for i in range(0, len(x)):
x[i] = x[i] * 2
return x
print (my_function(range(0,3))) # Add your range between the parentheses!
# range type is an immutable sequence
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-15-6a6d208587c2> in <module>() 6 return x 7 ----> 8 print (my_function(range(0,3))) # Add your range between the parentheses! 9 10 # range type is an immutable sequence <ipython-input-15-6a6d208587c2> in my_function(x) 3 def my_function(x): 4 for i in range(0, len(x)): ----> 5 x[i] = x[i] * 2 6 return x 7 TypeError: 'range' object does not support item assignment
# 16 함수 내의 임의 크기의 리스트에 들어있는 요소의 갯수 세기
n = [3, 5, 7]
def total(lst):
ans = 0.0
for i in range(len(lst)):
ans += lst[i]
"""
# or you can use this for loop
for e in lst:
ans += e
"""
return ans
print (total(n))
15.0
# 17 함수 내의 리스트에 문자열 사용하기
n = ["Michael", "Lieberman"]
# Add your function here
def join_strings(lst):
string = ""
for i in range(len(lst)):
string += lst[i]
return string
print (join_strings(n))
MichaelLieberman
# 18 함수에서 두 개의 인자에 두 개의 리스트 사용하기
m = [1, 2, 3]
n = [4, 5, 6]
# Add your code here!
def join_lists(x, y):
for i in range(len(y)):
x.append(y[i])
return x
print (join_lists(m, n))
[1, 2, 3, 4, 5, 6]
# 19 함수에서 임의 개수의 리스트 사용하기
m = [1, 2, 3]
n = [4, 5, 6]
o = [7, 8, 9]
# Update the below function to take
# an arbitrary number of arguments
def join_lists(*lst):
for i in range(1, len(lst)):
for j in range(len(lst[i])):
lst[0].append(lst[i][j])
return lst[0]
print (join_lists(m, n, o))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
# 20 함수 안에서 여러 리스트로 이루어진 하나의 리스트 사용하기
n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
# Add your function here
def flatten(multi_list):
lst = []
for l in multi_list:
for e in l:
lst.append(e)
return lst
print (flatten(n))
[1, 2, 3, 4, 5, 6, 7, 8, 9]