[codecademy] 7.LISTS AND FUNCTIONS

Posted by 알 수 없는 사용자
2015. 11. 7. 20:44 프로그래밍/Python

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 15/18

Iterating over a list in a function

리스트를 통해 두가지 방법으로 반복문을 사용할수 있다.

1. for item in list:


2. 인덱스를 통한 반복문


2번째 방법은 리스트의 값을 일일히 변경할수 있다.

문제

Create a function that returns the sum of a list of numbers.

1. On line 3, define a function called total that accepts one argument called numbers. It will be a list.
2. Inside the function, create a variable called result and set it to zero.
3. Using one of the two methods above, iterate through the numbers list.
4. For each number, add it to result.
5. Finally, return result.

Create a function called total that adds up all the elements of an arbitrary list and returns that count, using the existing code as a hint. Use a for loop so it can be used for any size list.



Lists and Functions 16/18
Using strings in lists in functions

15번에서 배운 방법을 문자열에도 적용시켜보자.

문제

Create a function that concatenates strings.

1. Define a function called join_strings accepts an argument called words. It will be a list.
2. Inside the function, create a variable called result and set it to "", an empty string.
3. Iterate through the words list and append each word to result.
4. Finally, return the result.

Don't add spaces between the joined strings!

Lists and Functions 17/18

Using two lists as two arguments in a function

리스트 두개를 매개변수로 받아서 합칠수 있다.


문제
Create a function that joins two lists together.

1. On line 4, define a function called join_lists that has two arguments, x and y. They will both be lists.
2. Inside that function, return the result of concatenating x and y together.


Lists and Functions 18/18

Using a list of lists in a function

마지막으로 하나의 리스트에 여러개의 리스트가 들어가있을때 함수에서 사용하는 방법이다.

 


문제
Create a function called flatten that takes a single list and concatenates all the sublists that are part of it into a single list.

1. On line 3, define a function called flatten with one argument called lists.
2. Make a new, empty list called results.
3. Iterate through lists. Call the looping variable numbers.
4. Iterate through numbers.
5. For each number, .append() it to results.
6. Finally, return results from your function.