์ „์ฒด๋ณด๊ธฐ 182

Casting ์บ์ŠคํŒ…(ํ˜•๋ณ€ํ™˜, ์œ ํ˜•์ง€์ •)

Casting ๋ณ€์ˆ˜์— ์œ ํ˜•์„ ์ง€์ •ํ•˜๋ ค๋Š” ๊ฒฝ์šฐ ์บ์ŠคํŒ…์œผ๋กœ ๊ฐ€๋Šฅ. 1. ์ •์ˆ˜ํ˜• x = int(1) print(x) y = int(2.8) #์‹ค์ˆ˜๋ฅผ ์ •์ˆ˜๋กœ ์บ์ŠคํŒ…-> ์†Œ์ˆ˜์  ๋’ค์— ๊ทธ๋ƒฅ ์—†์–ด์ง print(y) z = int("3") #char->int print(z) print(type(z)) #class int print(x+z) 2. ์‹ค์ˆ˜ํ˜• x = float(1) print(x) z = float("3") print(z) 3. ๋ฌธ์žํ˜• x = str(1) y = str(2) print(x) print(x+y) #๋ฌธ์ž์—ด ์—ฐ๊ฒฐ 4. ์•„์Šคํ‚ค์ฝ”๋“œ ๋ณ€ํ™˜ a = ord('A') #ordinal์˜ ์•ฝ์–ด๋กœ '์ˆœ์„œ', '์„œ์ˆ˜'๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ํ•ด๋‹น ๋ฌธ์ž ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋ผ๋Š” ๋œป print(a) #65 b = chr(65) p..

input ๋ฐ์ดํ„ฐ ์ž…๋ ฅํ•จ์ˆ˜

input ๋ฐ์ดํ„ฐ ์ž…๋ ฅํ•จ์ˆ˜ name = input('์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์„ธ์š”. >>>') age = input('๋‚˜์ด๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”. >>>') print('์ž…๋ ฅ๋œ ์ด๋ฆ„์€ {}์ž…๋‹ˆ๋‹ค.'.format(name)) print('์ž…๋ ฅ๋œ ๋‚˜์ด๋Š” {}์ž…๋‹ˆ๋‹ค.'.format(age)) ๊ฐ„๋‹จํ•˜๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜๋‹ค.

ํŒŒ์ด์ฌ format ์‚ฌ์šฉ๋ฒ•

format {}๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์ข…๋ฅ˜์— ์ƒ๊ด€์—†์ด print๋ฌธ์œผ๋กœ ํ‘œํ˜„์ด ๊ฐ€๋Šฅ. print("์˜ฌํ•ด๋Š” {}๋…„ ์ž…๋‹ˆ๋‹ค.".format(2023)) print("์˜ฌํ•ด๋Š” {}๋…„, ๋‚ด๋…„์€ {}๋…„ ์ž…๋‹ˆ๋‹ค.".format(2023, 2024)) print("๋‚˜๋Š” {}์„ ๊ณต๋ถ€ํ•ฉ๋‹ˆ๋‹ค.".format('Python')) print("๋‚˜๋Š” {}๊ณผ {}๋ฅผ ํƒ‘๋‹ˆ๋‹ค.".format('์ง€ํ•˜์ฒ ','๋ฒ„์Šค')) address = ''' ์„œ์šธํŠน๋ณ„์‹œ ๊ฐ•๋‚จ๊ตฌ \t\tํ…Œํ—ค๋ž€๋กœ 123 ''' print('์ฃผ์†Œ : {addr}'.format(addr=address)) #์ถ•์•ฝ์–ด๋ฅผ ์ž˜ ์“ฐ๋ฉด ์ฝ”๋“œ๋„ ๊น”๋”ํ•ด์ง€๊ณ  ํŽธํ•˜๋‹ค!

print() ํ•จ์ˆ˜ ์‚ฌ์šฉ๋ฒ•

print() ํ•จ์ˆ˜ ์‚ฌ์šฉ๋ฒ• sep : ์ถœ๋ ฅํ•  value์˜ ๊ตฌ๋ถ„ ๋ฌธ์ž end : value ์ถœ๋ ฅ ํ›„ ์ถœ๋ ฅํ•  ๋ฌธ์ž (๊ธฐ๋ณธ๊ฐ’) '\n') file : ์ถœ๋ ฅ ๋ฐฉํ–ฅ ์ง€์ • 1. ',' ์‚ฌ์šฉ print('์žฌ๋ฏธ์žˆ๋Š”','ํŒŒ์ด์ฌ') #์ž๋™ ๋„์–ด์“ฐ๊ธฐ, ๋ฌธ์ž ์—ฐ๊ฒฐ 2. sep ์‚ฌ์šฉ print('Python', 'Java', 'C', sep=',') #sep๋ผ๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ 3. end ์‚ฌ์šฉ print("์•ˆ๋…•", end='') #๋””ํดํŠธ๋Š” end='\n' print("ํ•˜์„ธ์š”") 4. ๋ฒˆ์™ธ fos ์‚ฌ์šฉ open file fos = open('sample.py', mode='wt') #open ํ•จ์ˆ˜ sample์ด๋ผ๋Š” file์„ ์ƒ์„ฑ์‹œํ‚ด, mode write text print('print("Hello World")', file=fos)..

escape ์ด์Šค์ผ€์ดํ”„ ๋ฌธ์ž

์ด์Šค์ผ€์ดํ”„ ๋ฌธ์ž \n -> ์ค„๋ฐ”๊ฟˆ \t -> ํƒญ \b -> ๋ฐฑ์ŠคํŽ˜์ด์Šค \\ -> \ \' -> ์ž‘์€ ๋”ฐ์˜ดํ‘œ \" -> ํฐ ๋”ฐ์˜ดํ‘œ \r -> ์ค„ ๋ฐ”๊ฟˆ, ์ปค์„œ ์•ž์œผ๋กœ ์ด๋™ \f -> ์ค„ ๋ฐ”๊ฟˆ, ์ปค์„œ๋ฅผ ๋‹ค์Œ ์ค„๋กœ ์ด๋™ \a -> ๋ฒจ์†Œ๋ฆฌ \v -> ์ˆ˜์งํƒญ print('Hello \'World\'') print("Hello \"World\"") print('*\n**\n***') print('์ด๋ฆ„\t์—ฐ๋ฝ์ฒ˜') print('์ œ์‹œ์นด\t02-124-5678')

ํŒŒ์ด์ฌ mutable vs. immutable ๋ฎคํ„ฐ๋ธ” ๋Œ€ ์ด๋ฎคํ„ฐ๋ธ” ๊ฐ„๋‹จํ•œ ์ฐจ์ด

mutable - ๋ฉ”๋ชจ๋ฆฌ ๊ฐ’ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ ๊ฐ์ฒด ๋ฆฌ์ŠคํŠธ(list), ์„ธํŠธ(set), ๋”•์…”๋„ˆ๋ฆฌ(dict) me = [1,2,3] print(id(me)) #2986748083968 me.append(4) print(id(me)) #2986748083968 ์ฃผ์†Œ๊ฐ’์ด ๊ฐ™๋‹ค immutable - ๋ฉ”๋ชจ๋ฆฌ ๊ฐ’ ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€ ์ •์ˆ˜(int), ์‹ค์ˆ˜(float), ๋ฌธ์ž์—ด(str), ํŠœํ”Œ(tuple) me = 10 print(id(me)) #1878060722768 me += 1 # me = me + 1 print(id(me)) #1878060722800 ์ฃผ์†Œ๊ฐ’์ด ๋‹ค๋ฅด๋‹ค # ํŠœํ”Œ ํ…Œ์ŠคํŠธ thistuple = ("ํ”ผ์นด์ธ„", "๋ผ์ด์ธ„", "ํŒŒ์ด๋ฆฌ") print(id(thistuple)) thiscast = list(thistuple..

Python dict ์ด์ •๋ฆฌ(dictionary)

Dictionary Key : value๋กœ ์ด๋ฃจ์–ด์ ธ ์Œ์œผ๋กœ ๋ฐ์ดํ„ฐ ๊ฐ’์„ ์ €์žฅํ•˜๋Š”๋ฐ ์‚ฌ์šฉ 1. dictionary ์„ ์–ธ thisdict = { "brand":"๋‚˜์ดํ‚ค", "model":"max", "year":1990 } print(thisdict) ''' ํ‚ค ์ด๋ฆ„์„ ์‚ฌ์šฉํ•˜์—ฌ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋‹ค. ''' 2. ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ thisdict = { "brand":"๋‚˜์ดํ‚ค", "model":"max", "year":1990 } print(thisdict["brand"]) #ํ‚ค ๊ฐ’ ์‚ฌ์šฉํ•ด์„œ value ๊ฐ€์ ธ์˜ค๊ธฐ print(thisdict.get("model")) #get๋ฉ”์†Œ๋“œ ์‚ฌ์šฉํ•ด์„œ value ๊ฐ€์ ธ์˜ค๊ธฐ 3. ํ‚ค ๋ชฉ๋ก ๊ฐ€์ ธ์˜ค๊ธฐ print(thisdict.keys()) 4. ์ถ”๊ฐ€ํ•˜๊ธฐ1 thisdict = { "brand..

Python Set ์ด์ •๋ฆฌ

Set ์ˆœ์„œ๊ฐ€ ์—†๋‹ค. ์ธ๋ฑ์‹ฑ ๋˜์ง€ ์•Š๋Š” ์ปฌ๋ ‰์…˜ ์ค‘๋ณต๊ฐ’ ์—†๋‹ค. 1. ํ˜•์‹ thisset = {"ํ”ผ์นด์ธ„", "๋ผ์ด์ธ„", "ํŒŒ์ด๋ฆฌ"} #์‹คํ–‰ํ•  ๋•Œ๋งˆ๋‹ค ์ˆœ์„œ๊ฐ€ ๋ณ€๊ฒฝ. ๋‚ด๋ถ€์ ์œผ๋กœ๋Š” ์ˆœ์„œ๋ฅผ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š์Œ. print(thisset) #ํ•ญ๋ชฉ ๊ฐ€์ ธ์˜ค๊ธฐ for x in thisset: #thisset ๊ธธ์ด๋งŒํผ ๋ฐ˜๋ณต print(x) # ๊ฐ’์ด ์žˆ๋Š”์ง€ ํ™•์ธ thisset = {"ํ”ผ์นด์ธ„", "๋ผ์ด์ธ„", "ํŒŒ์ด๋ฆฌ"} print("ํ”ผ์นด์ธ„" in thisset) print("๊ผฌ๋ถ€๊ธฐ" in thisset) 2. ํ•ญ๋ชฉ ์ถ”๊ฐ€ํ•˜๊ธฐ thisset.add("๊ผฌ๋ถ€๊ธฐ") print(thisset) #๋‹ค๋ฅธ Set ํ•ญ๋ชฉ ์ถ”๊ฐ€ thisset1 = {"ํ”ผ์นด์ธ„", "๋ผ์ด์ธ„", "ํŒŒ์ด๋ฆฌ"} thisset2 = {"๊ผฌ๋ถ€๊ธฐ", "์ž ๋งŒ๋ณด", "๋ฎค์ธ "} thiss..

๋ณ€์ˆ˜(variable)

๋ณ€์ˆ˜(variable) ์–ด๋–ค ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”๋ชจ๋ฆฌ ์ €์žฅ์†Œ. 1. ๋ณ€์ˆ˜๋ช… ๊ทœ์น™ (1) ์˜๋ฌธ, ํ•œ๊ธ€, ์ˆซ์ž, ๋ฐ‘์ค„(_)๋กœ ๊ตฌ์„ฑ๋œ๋‹ค. (2) ํŠน์ˆ˜๋ฌธ์ž(!@#$%^&*()_+ ๋“ฑ) ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค!! (3) ๋Œ€๋ฌธ์ž์™€ ์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•œ๋‹ค. (4) ๋ณ€์ˆ˜๋ช…์˜ ์ฒซ ๊ธ€์ž๋ฅผ ์ˆซ์ž๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค!! (5) ํ‚ค์›Œ๋“œ(list, dict, if, for, and ๋“ฑ)๋Š” ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค!! 2. ๋ณ€์ˆ˜๋ช… ← ๊ฐ’ name = 'Alice' #๋ฌธ์ž์—ด, '',"" ์ƒ๊ด€ ์—†์Œ print(name) age =25 print(age) addresds = '''์šฐํŽธ๋ฒˆํ˜ธ 12345 ์„œ์šธ์‹œ ์˜๋“ฑํฌ๊ตฌ ์—ฌ์˜๋„๋™ 123-45''' #์—ฌ๋Ÿฌ ์ค„ ์ž…๋ ฅํ•  ๋•Œ๋Š” ์ด๋ ‡๊ฒŒ ์”€ print(addresds) 3. ์ž˜๋ชป๋œ ๋ณ€์ˆ˜๋ช… ์˜ˆ์‹œ # 2myba..

๋ฌธ์ž์—ด(String)

๋ฌธ์ž์—ด(String) ํ•˜๋‚˜ ์ด์ƒ ์—ฐ์†๋œ ๋ฌธ์ž(character)๋“ค์˜ ๋‚˜์—ด. ํŒŒ์ด์ฌ์—์„œ ๋ฌธ์ž์—ด ํฐ๋”ฐ์˜ดํ‘œ(" ") ๋˜๋Š” ์ž‘์€ ๋”ฐ์˜ดํ‘œ(' ') ์‚ฌ์ด์— ์œ„์น˜. 1. 'hello'์™€ "hello" ๋™์ผ print('hello' == "hello") # ๊ฐ’์ด ๊ฐ™์œผ๋ฉด True ๋ฐ˜ํ™˜ 2. ๋ณ€์ˆ˜์— ๋ฌธ์ž์—ด ํ• ๋‹น a(๋ณ€์ˆ˜) = "Hello"(๋ฌธ์ž์—ด) print(a) 3. ์—ฌ๋Ÿฌ ์ค„ ๋ฌธ์ž์—ด ์„ธ ๊ฐœ์˜ ๋”ฐ์˜ดํ‘œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ณ€์ˆ˜์— ์—ฌ๋Ÿฌ ์ค„ ๋ฌธ์ž์—ด ํ• ๋‹น a = """ํ”ผ์นด์ธ„, ๋ผ์ด์ธ„, ํŒŒ์ด๋ฆฌ, ๊ผฌ๋ถ€๊ธฐ ๋ฒ„ํ„ฐํ”Œ, ์•ผ๋„๋ž€, ํ”ผ์กดํˆฌ, ๋˜๊ฐ€์Šค """ print(a) 4. ๋ฌธ์ž ๋ฐฐ์—ด => ๋ฌธ์ž์—ด ๋ฌธ์ž์—ด ์ธ๋ฑ์‹ฑ(indexing) h e l l o