

๐ฝ ๋นํธ ์ฐ์ฐ ํ ํ ๋น

์ด์ง์๋ก ๋ฐ๋ ์ํ์์ ์ฌ์ฉํ๋ ์ฐ์ฐ๊ธฐํธbin() : 2์ง์๋ก ๋ฐ๊พธ๋ ํจ์
> a=116
print(bin(a)) #0b1110100
> x = 60 # 60์ ์ด์ง์: 0011 1100
y = 13 # 13์ ์ด์ง์: 0000 1101
<<AND ์ฐ์ฐ>>
print(x & y) #12
์ผ์ค ๋ฐ์ดํ("""/''')๋ก ๋๋ฌ์ธ์.def add(a, b):
"""
๋ ์ซ์๋ฅผ ๋ํ๋ ํจ์์
๋๋ค. =># ํจ์์ ์- ์ฃผ์
๋งค๊ฐ๋ณ์:
a (int, float): ์ฒซ ๋ฒ์งธ ์ซ์
b (int, float): ๋ ๋ฒ์งธ ์ซ์
๋ฐํ๊ฐ:
int, float: ๋ ์ซ์์ ํฉ
"""
return a + b
print(add(3, 5)) #8
๊ฐ] ๋ฌธ์์ด ์ถ๋ ฅ
( .__doc__) print(add.__doc__)
#๋ ์ซ์๋ฅผ ๋ํ๋ ํจ์์
๋๋ค.
๋งค๊ฐ๋ณ์:
a (int, float): ์ฒซ ๋ฒ์งธ ์ซ์
b (int, float): ๋ ๋ฒ์งธ ์ซ์
๋ฐํ๊ฐ:
int, float: ๋ ์ซ์์ ํฉ
help( )help(add)
#Help on function add in module __main__:
add(a, b)
๋ ์ซ์๋ฅผ ๋ํ๋ ํจ์์
๋๋ค.
๋งค๊ฐ๋ณ์:
a (int, float): ์ฒซ ๋ฒ์งธ ์ซ์
b (int, float): ๋ ๋ฒ์งธ ์ซ์
๋ฐํ๊ฐ:
int, float: ๋ ์ซ์์ ํฉ
์ธ๋ฑ์ฑ & ์ฌ๋ผ์ด์ฑ โโ "๐ฌPython(1) ํ์ธ "
์ฐ๊ฒฐ & ๋ฐ๋ณต
+hello = "HELLO"
world = "WORLD"
print(hello + world) # HELLOWORLD
"๊ตฌ๋ถ์".join(list)words = ["Hello", "World", "with", "Python"]
<<๊ตฌ๋ถ์: ๊ณต๋ฐฑ>>
result = " ".join(words)
print(result) #Hello World with Python
<<๊ตฌ๋ถ์: *>>
resutl = "*".join(words)
print(resutl) #Hello*World*with*Python
*hello = "Hello"
print(hello * 3) #HelloHelloHello
* ๋ฉ์๋.method
x = "Hello World"
len() ํจ์ : ๋ฌธ์์ด ๊ธธ์ดprint(len(x)) # 11
.lower() : ๋ฌธ์์ด -> ์๋ฌธ์print(x.lower()) # hello world
.upper() : ๋ฌธ์์ด -> ๋๋ฌธ์print(x.upper()) # HELLO WORLD
.strip() : ๋ฌธ์์ด ์์ชฝ ๊ณต๋ฐฑ ์ ๊ฑฐx = " Hello Python "
print(x.strip()) # Hello Python
.replace(old, new) : old๋ฅผ new๋ก ๋ณ๊ฒฝโถ "๊ต์ฒด/์์ "x = "Hello Python!"
print(x.replace("Python", "World")) # Hello World!
.split(๊ตฌ๋ถ์) : ๊ตฌ๋ถ์(delimiter)๋ก ๋ถ๋ฆฌ<<๊ณต๋ฐฑ๊ธฐ์ค ๋ถ๋ฆฌ>>
> x = "Hello world with Python!"
print(str.split()) # ['Hello', 'world', 'with', 'Python!']
<<๊ตฌ๋ถ์ (, ๋๋ /) ๊ธฐ์ค ๋ถ๋ฆฌ>>
> str2 = "Hello,Python,Bye"
str3 = "Hello/Python, world"
s2 = str2.split(",")
print(s2) #['Hello','Python','Bye']
s3 = str3.split("/")
print(s3) #['Hello', 'Python,Bye']

.count(sub-string) : IN ๋ฌธ์์ด(str) -> sub-string ๊ฐ์ ๋ฐํy = "Hello Python!"
print(y.count("l")) #2
.find(sub-string) .index(sub-string)์์น ๋ฐํ>y = "Hello Python!"
print(y.find("Python")) #6
print(y.find("Java")) #-1 (์ฐพ๋ ๋ฌธ์์ด์ด ์์ ๊ฒฝ์ฐ)
>print(y.index("Java")) #ValueError: substring not found

in ์ฐ์ฐ์ -> ๋ฌธ์์ด ํฌํจ ์ฌ๋ถ ํ๋จy = "Hello Python!"
print("Python" in y) # True
print("Java" in y) # False
: ๊ณ ์ ๋ ๋ฌธ์์ดX, ๋ณ์์ ๋ฐ๋ผ ๋ค๋ฅธ ๋ฌธ์์ด์ ์ถ๋ ฅํ ๋ ์ฌ์ฉ.
-Ex) str"{์ด์ฉ์}๋ ํ์ํฉ๋๋ค!" ๋ผ๋ ํฌ๋งท์ค์ -> ํ์์, (์ด์ฉ์) ๊ฐ๋ง ์ง์ -> ํฌ๋งท์ ๋ง๋ ๋ฌธ์์ด ์ถ๋ ฅ.
: ๋ณ์์ ์๋ฆฌ์ % ์๋ฃํ์ ์
๋ ฅํ์ฌ ์ด๋ฅผ ์นํ
-์ถ๋ ฅ ๋ฐ์ดํฐ๋ฅผ tuple ํํ๋ก ์ ๋ฌ

name = "ํ๊ธธ๋"
age = 24
weight = 65.5
<<์ ๋ฐ์ดํฐ(ํ๋กํ์ผ)๋ฅผ ์๋ ํ์์ผ๋ก ์ถ๋ ฅ>>
#์ด๋ฆ [ํ๊ธธ๋]
#๋์ด [24]
#์ฒด์ค [65.5]
profile = "์ด๋ฆ [%s]\n๋์ด [%d]\n์ฒด์ค [%.1f]" % (name, age, weight)
โณโ๏ธ % (name, age, weight): ๋ฌธ์์ด์์
ํฌํจ๋ ํฌ๋งท์ง์ ์(%s, %d, %.1f)์ ํด๋น๋๋ ๊ฐ์ ์ค์ ๋ณ์๋ก ๋์ฒด์ํจ.
:ํํ ํํ๋ก ๊ฐ๋ค์ ๊ทธ๋ฃนํํ์ฌ ์ ๋ฌ
print(profile)
<<๋๋น ์ง์ >>
#์ด๋ฆ [ ํ๊ธธ๋]
#๋์ด [ 24]
#์ฒด์ค [ 65.5]
profile = "์ด๋ฆ [%10s]\n๋์ด [%10d]\n์ฒด์ค [%10.1f]" %(name, age, weight)
print(profile)
<<์ผ์ชฝ ์ ๋ ฌ>> " - "
#์ด๋ฆ [ํ๊ธธ๋ ]
#๋์ด [24 ]
#์ฒด์ค [65.5 ]
profile = "์ด๋ฆ [%-10s]\n๋์ด [%-10d]\n์ฒด์ค [%-10.1f]" % (name, age, weight)
print(profile)
<<0์ผ๋ก ์ฑ์ฐ๊ธฐ>>
#๋์ด [0000000024]
#์ฒด์ค [00000065.5]
profile = "๋์ด [%010d]\n์ฒด์ค [%010.1f]" % (age, weight)
print(profile)
: ๋ฌธ์์ด(str) ๋ด, ์ค๊ดํธ๋ฅผ ์ด์ฉํ์ฌ ์ถ๋ ฅ๋ ๋ฐ์ดํฐ์ ์์น๋ฅผ ์ง์
-์ถ๋ ฅ์ ์ฌ์ฉํ ๋ฐ์ดํฐ๋ format() ๋ฉ์๋์ ์ธ์๋ก ์ ๋ฌ

name = "Python"
age = 30
message = "Hello, {}! Your age is {}.".format(name, age)
print(message) #Hello, Python! Your age is 30.
<<ํจ์'s ๋งคํ๊ฐ ์์น ๋ฒํธ์ง์ >>
message = "Hello, {1}! Your age is {0}.".format(age, name)
print(message) # Hello, Python! Your age is 30.
<<๋งคํ๊ฐ ์์น ์ด๋ฆ์ง์ >>
message = "Hello, {xname}! Your age is {xage}.".format(xname=name, xage=age)
print(message) # Hello, Python! Your age is 30.
<<๋ถ๋์์์ -> n๋ฒ์งธ ์์์ ์๋ฆฌ๊น์ง ์ถ๋ ฅ>>
pi = 3.1415926535
format_string = "์์ฃผ์จ์ {:.2f}์
๋๋ค.".format(pi)
print(format_string) # ์์ฃผ์จ์ 3.14์
๋๋ค.
<<์์์ ์๋ฆฟ์ ์ง์ โ ์๋ฆฟ์ ์ค์ & ๊ณต๋ฐฑ 0>>
format_string = "์์ฃผ์จ์ {:010.2f}์
๋๋ค.".format(pi)
print(format_string) # ์์ฃผ์จ์ 0000003.14์
๋๋ค.
<<๋ฌธ์์ด ์ ๋ ฌ ๋ฐ ๋๋น์ง์ >>
name = "HONG"
age = 23
address = "Seoul"
align_left = "|{:<10}|{:<10}|{:<10}|".format(name, age, address)
align_right = "|{:>10}|{:>10}|{:>10}|".format(name, age, address)
align_center = "|{:^10}|{:^10}|{:^10}|".format(name, age, address)
print(align_left) # |HONG |23 |Seoul |
print(align_right) # | HONG| 23| Seoul|
print(align_center) # | HONG | 23 | Seoul |

: ๋ฌธ์์ด ์์ f ๋๋ F๋ฅผ ์ฝ์
-> ์ค๊ดํธ ์, ๋ณ์(๋๋ ํํ์)๋ฅผ ์ง์ ์ฝ์
*ํ์ด์ฌ 3.6์์ ๋์
name = "Python"
age = 30
print(f"Hello, {name}! You are {age} years old.") # Hello, Python! You are 30 years old.
<<๋ณ์ ๋ฟ ์๋๋ผ ํํ์๋ ๊ฐ๋ฅ>>
print(f"Next year, your will be {age + 1} years old.")
#Next year, your will be 31 years old.
<<๋ถ๋์์์ -> n๋ฒ์งธ ์์์ ์๋ฆฌ๊น์ง ์ถ๋ ฅ>>
pi = 3.141592653589793
print(f"์์ฃผ์จ = {pi:.2f}") # ์์ฃผ์จ = 3.14
<<์ ๋ ฌ>>
print(f"{'hello':10}python") # hello python
print(f"{'hello':<10}python") # hello python
print(f"{'hello':>10}python") # hellopython
print(f"{'hello':^10}python") # hello python
๐ฝ
<<๊ณต๋ฐฑ ์ฑ์ฐ๊ธฐ>>
print(f"{'hello':*<10}python") # hello*****python
print(f"{'hello':*>10}python") # *****hellopython
print(f"{'hello':*^10}python") # **hello***python