(Python 2-2๊ฐ) Function and Console I/O
210802
ํจ์
์ด๋ค ์ผ์ ์ํํ๋ ์ฝ๋์ ๋ฉ์ด๋ฆฌ
๋ฐ๋ณต์ ์ธ ์ํ์ ํ ๋ 1ํ๋ง ์์ฑํ๊ณ ๋ฐ๋ณต ํธ์ธจ ํ ์ ์๋ค
์ฝ๋๋ฅผ ๋ ผ๋ฆฌ์ ์ธ ๋จ์๋ก ๋ถ๋ฆฌํ๋ค
์บก์ํ : ์ธํฐํ์ด์ค๋ง ์๋ฉด ํ์ธ์ ์ฝ๋๋ฅผ ์ฌ์ฉํ ์ ์๋ค
๋ฌธ๋ฒ
def ํจ์์ด๋ฆ (parameter1, ...):
์ํ๋ฌธ #1
์ํ๋ฌธ #2
return ๋ฐํ๊ฐ
์์
def cal_add(a, b):
result = a+b
return result
Parameter vs Argument
Parameter : ํจ์์ ์ ๋ ฅ ๊ฐ ์ธํฐํ์ด์ค
def f(x): ์์์ x
Argument : ์ค์ Parameter์ ๋์ ๋ ๊ฐ
f(2) ์์์ 2
์ฝ์์ฐฝ ์ ์ถ๋ ฅ
Input() : ์ฝ์์ฐฝ์์ ๋ฌธ์์ด์ ์ ๋ ฅ ๋ฐ๋ ํจ์
print() : ์ฝ์์ฐฝ์์ ์ถ๋ ฅ์ ๋ด๋น
Print formating
๊ธฐ๋ณธ์ ์ธ ์ถ๋ ฅ ์ธ์ 3๊ฐ์ง ์ถ๋ ฅ ํ์์ ์ง์ ๊ฐ๋ฅํ๋ค
print(1,2,3)
print("a" + " " + "b" + " " + "c")
print("%d %d %d" % (1,2,3))
print("{} {} {}".format("a","b","c"))
print(f"value is {value})
old-school formatting
์ผ๋ฐ์ ์ผ๋ก %-format๊ณผ str.format() ํจ์๋ฅผ ์ฌ์ฉํจ
print('%s %s' % ('one', 'two'))
print('{} {}'.format('one', 'two'))
print('%d %d' % (1, 2))
print('{} {}'.format(1, 2))
% - format
print("I eat %d apples." % 3)
print("I eat %s apples." % "five")
number = 3; day="three"
print("I ate %d apples. I was sick for %s days." % (number, day))
print("Product: %s, Price per unit: %f." % ("Apple", 5.243))
%s ๋ฌธ์์ด (String)
%c ๋ฌธ์ 1๊ฐ(character)
%d ์ ์ (Integer)
%f ๋ถ๋์์ (floating-point)
%o 8์ง์
%x 16์ง์
%% Literal % (๋ฌธ์ % ์์ฒด)
str.format
age = 36; name='Sungchul Choi'
print("Iโm {0} years old.".format(age))
print("My name is {0} and {1} years old.".format(name,age))
print("Product: {0}, Price per unit: {1:.3f}.".format("Apple", 5.243))
naming
print("Product: %(name)10s, Price per unit: %(price)10.5f." %
{"name":"Apple", "price":5.243})
print("Product: {name:>10s}, Price per unit:
{price:10.5f}.".format(name="Apple", price=5.243))
f-string
name = "Sungchul"
age = 39
print(f"Hello, {name}. You are {age}.")
print(f'{name:20}')
print(f'{name:>20}')
print(f'{name:*<20}')
print(f'{name:*>20}')
print(f'{name:*^20}')
Hello, Sungchul. You are 39.
Sungchul
Sungchul
Sungchul************
************Sungchul
******Sungchul******
Last updated
Was this helpful?