#!/usr/bin/env python # coding: utf-8 # # Chapter 12 Lists and Functions # # In[1]: # 01 리스트(Lists) 접근하기 n = [1, 3, 5] # Add your code below print (n[1]) # In[2]: # 02 리스트 요소(List element) 수정 n = [1, 3, 5] # Do your multiplication here n[1] *= 5 print (n) # In[3]: # 03 리스트(List)에 추가하기 (Appending) n = [1, 3, 5] # Append the number 4 here n.append(4) print (n) # In[4]: # 04 리스트(lists)에서 요소 제거 n = [1, 3, 5] # Remove the first item in the list here n.pop(0) print (n) # In[5]: # 05 함수(Function)의 기능(Functionality) 변경하기 number = 5 def my_function(x): return x * 3 print (my_function(number) ) # In[6]: # 06 하나 이상의 인자(argument) m = 5 n = 13 # Add add_function here! def add_function(x, y): return x + y print (add_function(m, n) ) # In[7]: # 07 임의 갯수의 인자(argument) m = 5 n = 13 # Add add_function here! def add_function(*nums): return sum(nums) print (add_function(m, n) ) # In[8]: # 08 함수(Function) 안의 문자열 n = "Hello" # Your function here! def string_function(s): return s + "world" print (string_function(n)) # In[9]: # 09 리스트(Lists)를 함수(Function)에 전달하기 def list_function(x): return x n = [3, 5, 7] print (list_function(n)) # In[10]: # 10 함수 내의 리스트로부터 요소 사용하기 def list_function(x): return x[1] n = [3, 5, 7] print (list_function(n)) # In[11]: # 11 함수 내의 리스트에 들어있는 요소 변경하기 def list_function(x): x[1] += 3 return x n = [3, 5, 7] print (list_function(n)) # In[12]: # 12 함수 내의 리스트 조작 n = [3, 5, 7] # Add your function here def list_extender(lst): lst.append(9) return lst print (list_extender(n)) # In[13]: # 13 함수 내의 리스트를 요소별로 출력하기 n = [3, 5, 7] def print_list(x): for i in range(0, len(x)): print (x[i]) print_list(n) # In[14]: # 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) ) # In[15]: # 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 # In[16]: # 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)) # In[17]: # 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)) # In[18]: # 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)) # In[19]: # 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)) # In[20]: # 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))