이 장에서는 다음을 배우게 된다.
pycon
▶ Understanding the Python view of data ▶ Using operators to assign, modify, and compare data ▶ Organizing code using functions ▶ Interacting with the user
Operators are the basis for both control and management of data within applications
연산자의 종류는 다음과 같다.
pycon
✓ Unary ✓ Arithmetic ✓ Relational ✓ Logical ✓ Bitwise ✓ Assignment ✓ Membership ✓ Identity
함수를 정의해 보자.
def Hello():
print("This is my first Python function!")
Hello()
함수 호출은 정의한 함수 그대로 호출하면 된다.
다음을 출력해 보자. 함수라는것은 반복적인 구문이나 어떤 상황을 전달했을때 다양하게 출력할 수 있는 것을 만드는 것이다.
def Hello2( Greeting ):
print(Greeting)
Hello2("This is the second function.")
Hello2("It takes an argument.")
아래 예제는 값이 지정되어 전달되거나 임의로 전달되었을때 합을 출력하는 함수이다.
def AddIt(Value1, Value2):
print(Value1, " + ", Value2, " = ", (Value1 + Value2))
AddIt(2, 3)
AddIt(Value2 = 3, Value1 = 2)
아래 예제는 기본값이 주어지지 않았을경우 출력하는 예제이다.
def Hello3(Greeting = "No Value Supplied"):
print(Greeting)
Hello3()
Hello3("This is a string.")
Hello3(5)
Hello3(2 + 7)
함수로부터의 리턴값은 다양하게 나타낼수 있다 .
pycon
✓ Values: Any value is acceptable. You can return numbers, such as 1 or 2.5; strings, such as “Hello There!”; or Boolean values, such as True or False. ✓ Variables: The content of any variable works just as well as a direct value. The caller receives whatever data is stored in the variable. ✓ Expressions: Many developers use expressions as a shortcut. For example, you can simply return A + B rather than perform the calculation, place the result in a variable, and then return the variable to the caller. Using the expression is faster and accomplishes the same task. ✓ Results from other functions: You can actually return data from another function as part of the return of your function.
함수의 리턴값을 비교하는 예제를 실행해 보자.
def DoAdd(Value1, Value2):
return Value1 + Value2
print("The sum of 3 + 4 is ", DoAdd(3, 4))
print("3 + 4 equals 2 + 5 is ", (DoAdd(3, 4) == DoAdd(2, 5)))
사용자가 입력을 할 수 있는 것을 제공하는 기능이다. 사용자가 입력한값을 가지고 처리하게 되는것이다.
다음 예제를 출력해 보자.
Name = input("Tell me your name: ")
print("Hello ", Name)
ANumber = float(input("Type a number: "))
print("You typed: ", ANumber)
14.format_a_string_of_names_like_bart_lisa_and_maggie.py ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~