[codecademy] 7.LISTS AND FUNCTIONS
LISTS AND FUNCTIONS
Lists and Functions 1/18
List accessing
리스트의 정보를 가져오고 바꿀수있다.
문제
Please add the code to print out the second element in the list.
Lists and Functions 2/18
List element modification
리스트의 값을 바꾸는법을 알아보자.(복습)
문제
On line 3, multiply the second element of the n list by 5
Overwrite the second element with that result.
Make sure to print the list when you are done!
Lists and Functions 3/18
Appending to a list
.append() 함수를 이용하여 리스트의 마지막 부분에 값을 추가할수 있다.
문제
Append the number 4 to the end of the list n.
Lists and Functions 4/18
Removing elements from lists
리스트에서 값을 삭제시키는 방법은 3가지가 있다.
1. n.pop(index) : 해당인덱스에 있는 값을 제거하고 그값을 리턴한다.
2. n.remove(item) : 리스트에서 item에 해당하는 값을 삭제한다.( 매개변수가 인덱스가 아닌것에 주의 )
3. del(n[1]) : .pop와 같이 인덱스 값으로 삭제하지만 리턴값이 없다.
문제
Remove the first item from the list n using either .pop(), .remove(), or del.
Lists and Functions 5/18
Changing the functionality of a function
1~4번 문제들의 내용을 함수안에서 구현할 수 있다.
문제
Change the function so the given argument is multiplied by 3 and returned.
Lists and Functions 6/18
More than one argument
하나 이상의 매개변수를 갖는 함수를 복습해보자
문제
Define a function called add_function that has 2 parameters x and y and adds them together.
Lists and Functions 7/18
Strings in functions
문자열을 매개변수로 하는 함수를 복습해보자
문제
Write a function called string_function that takes in a string argument (s) and then returns that argument concatenated with the word 'world'.
Don't add a space before world!
Lists and Functions 8/18
Passing a list to a function
함수의 매개변수로 여러가지 변수형을 사용할수 있고, 리스트도 같은 방법으로 함수의 매개변수로 쓸 수 있다.
문제없음
Lists and Functions 9/18
Using an element from a list in a function
함수의 매개변수로 리스트를 사용하는 방법이다.
문제
Change line 2 so that list_function returns only the item stored in index one of x, rather than the entire x list.
Lists and Functions 10/18
Modifying an element of a list in a function
함수안에서 리스트의 값을 바꾸는것도 함수 밖에서 리스트의 값을 바꾸는방법이랑 같다.
문제
Change list_function so that:
Add 3 to the item at index one of the list.
Store the result back into index one.
Return the list.
Lists and Functions 11/18
List manipulation in functions
리스트에 값을 추가하는 append()도 함수안에서 사용할수 있다.
문제
1. Define a function called list_extender that has one parameter lst.
2. Inside the function, append the number 9 to lst.
3. Then return the modified list.
Lists and Functions 12/18
Printing out a list item by item in a function
renge()함수를 이용하여 함수안에서 리스트의 값들을 출력할수 있다
문제
1. Define a function called print_list that has one argument called x.
2. Inside that function, print out each element one by one. Use the existing code as a scaffold.
3. Then call your function with the argument n.
Lists and Functions 13/18
Modifying each element in a list in a function
리스트의 요소들을 각각 변경할수 있다. len()은 리스트의 길이를 반환한다.
문제
Create a function called double_list that takes a single argument x (which will be a list) and multiplies each element by 2 and returns that list.
Use the existing code as a scaffold.
Lists and Functions 14/18
Passing a range into a function
range()함수는 해당범위를 리스트로 만들어 준다.
range(start,stop,step)
start 부터 시작해서 stop 까지 step 만큼 건너뛰며 리스트로 만든다.
문제
On line 6, replace the ____ with a range() that returns a list containing [0, 1, 2].
Lists and Functions 17/18
Lists and Functions 18/18
'프로그래밍 > Python' 카테고리의 다른 글
[codecademy] 6. Student Becomes the Teacher (788) | 2015.11.07 |
---|---|
[codecademy] 4. FUNCTIONS (314) | 2015.09.05 |
[codecademy] 5.LISTS & DICTIONARIES (319) | 2015.08.28 |
[codecademy] 3.CONDITIONALS AND CONTROL FLOW (301) | 2015.08.10 |
Python 시리얼 출력(serial 통신) (4) | 2015.07.24 |