2020/python (codecademy)

Introduction to Functions

bNayC 2020. 4. 14. 23:22

What is a Function?

print("Welcome to Engrossing Grocers.")

print("Our special is mandarin oranges.")

print("Have fun shopping!")

 

 

Every time a customer enters, we call these three lines of code. Even if only 3 or 4 customers come in, that’s a lot of lines of code required.In Python, we can make this process easier by assigning these lines of code to a function. We’ll name this function greet_customer. In order to call a function, we use the syntax function_name(). The parentheses(괄호) are important! They make the code inside the function run. In this example, the function call looks like:

 

greet_customer()

 

Repeated code is generally more error prone and harder to understand, so it’s a good goal to reduce the amount of it.

 

이건 view solution을 봐야겠엉...
아아. 

cf) def는 '다음과 같이 정의하겠다~' 라는거 

 

Write a Function

To write a function, you must have a heading and an indented(들여쓰기) block of code. The heading starts with the keyword def and the name of the function, followed by parentheses, and a colon. The indented block of code performs some sort of operation. This syntax looks like:

 

The keyword def tells Python that we are defining a function. This function is called greet_customer.

Everything that is indented after the : is what is run when greet_customer() is called.

 

So every time we call greet_customer(), the three print statements run.

 

def 익숙해지기! 

   def 난 다음과 같이 명명할거야~

   def loading_screen 로딩_스크린이란 이름으로 다음과 같이 불러내려해~

   def loading_screen ():

      print ("") 

 

Whitespace

 If we wanted to write another line outside of greet_customer(), we would have to unindent the new line:

When we call greet_customer, the message"Cleanup on Aisle 6" is not printed, as it is not part of the function.

 

Parameters

The special of the day will not always be mandarin oranges, it will change every day. What if we wanted to call these three print statements again, except with a variable special? We can use parameters, which are variables that you can pass into the function when you call it.

 

In the definition heading for greet_customer(), the special_item is referred to as a formal parameter. This variable name is a placeholder for the name of the item that is the grocery’s special today. Now, when we call greet_customer, we have to provide a special_item:

 

greet_customer("peanut butter")

 

위에랑 차이를 잘 봐두기! 

Multiple Parameters

진짜 유용하겠지!

The variables grocery_store and special_item must now both be provided to the function upon calling it:

greet_customer("Stu's Staples", "papayas")

 

무슨 뜻이니 ㅠㅠ 얘도 view를 볼게...

def mult_x_add_y(number, x, y):

 

이게 답

 

부르라며ㅠㅠ 

 

인내심 테스트중... view 볼래... 이러니까 하나 하는데 3시간도 걸리지

 

내 위에위에 거랑 뭐가 다르지....

Keyword Arguments

Whichever value is put into greet_customer() first is assigned to grocery_store, and whichever value is put in second is assigned to special_item. These are called positional arguments because their assignments depend on their positions in the function call.

 

We can also pass these arguments as keyword arguments, where we explicitly refer to what each argument is assigned to in the function call.

 

더 알아볼거. (이해 다 하다가는 오늘 새벽 6시까지 절대 못 끝낼듯...... 대신 문제는 어디까지 내가 했는지 다 캡쳐해둘테니, 복습 시 뭐에서 몰라서 안됐었는지 차근히 보자~~) 

 

download도 같이 불러져 나왔균

 

파라미터 더하라며 ㅠㅠㅜ
like this! 

 

str 하라며 ㅠㅠ
질문을 뭐 저렇게 써놓은거.... 

Call create_spreadsheet() with title set to "Applications" and row_count set to 10.

 

-> 질문. 이건 왜 title로 불러내라고 했는데 download 써줬는지 모르겠다. 

 

Returns

 When there is a result from a function that can be stored in a variable, it is called a returned function value. We use the keyword return to do this.

 

이거로만 지금 30분째......

우선 pass. view point 로 넘어갔으니 다시 해야함 (양심)

 

Multiple Return Values

 

Scope

If we try to run this code, we will get a NameError, telling us that 'special_item' is not defined. The variable special_item has only been defined inside the space of a function, so it does not exist outside the function.

 

We call the part of a program where special_item can be accessed its scope. The scope of special_item is only the create_special_string function.

 

Variables defined outside the scope of a function may be accessible inside the body of the function:

 

There is no error here. header_string can be used inside the create_special_string function because the scope of header_string is the whole file. This file would produce:

 

Our special is grapes.

 

outside에 해줬잖아....

 

header_string 다시 해주기....

 

10문제 푸는데 1시간 22분 걸렸다아아아아 ㅠㅠㅠㅠㅠ