๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
4๏ธโƒฃ Python/Python ์ •๋ฆฌ

[Python] ์ž…์ถœ๋ ฅ ์ •๋ฆฌ

by seolhee2750 2021. 11. 15.

python์ด ์ด์ œ ์ต์ˆ™ํ•ด์ง€๋ ค๊ณ  ํ•˜๋Š”์ค‘,,!์ด๋ผ

๋” ํ™•์‹คํ•˜๊ฒŒ ๊ณต๋ถ€ํ•˜๊ธฐ ์œ„ํ•ด ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜๋“ค์ด๋‚˜

์–ธ์–ด ์‚ฌ์šฉ ๋ฐฉ๋ฒ•, ๋ฐ˜ํ™˜ ํƒ€์ž… ๋“ฑ์„ ์˜ˆ์ œ๋กœ ์ •๋ฆฌํ•ด์„œ ์—…๋กœ๋“œํ•ด๋ณด๋ ค๊ณ  ํ•œ๋‹ค.

์ž…์ถœ๋ ฅ๋ถ€ํ„ฐ ์‹œ์ž‘โ—๏ธ

 

๐Ÿ“Œ ์ž…๋ ฅ

"""
input ํ•จ์ˆ˜๋Š” ์ž…๋ ฅ๋˜๋Š” ๋ชจ๋“  ๊ฒƒ์„ ๋ฌธ์ž์—ด๋กœ ์ทจ๊ธ‰!
"""

a = input()
print(a) # in: 1 -> out: 1

b = int(input())
print(b) # in: 1 -> out: 1

c = input("์ž…๋ ฅ: ") # ํ”„๋กฌํ”„ํŠธ ๋„์›Œ์„œ ์ž…๋ ฅ ๋ฐ›๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅ


"""
split ํ•จ์ˆ˜ : ๊ณต๋ฐฑ ๋“ฑ ์–ด๋–ค ์ž…๋ ฅ์„ ๊ธฐ์ค€์œผ๋กœ ๋‚˜๋ˆ ์„œ ์ž…๋ ฅ๋ฐ›์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜
=> ๋ฌธ์ž์—ด.split([[sep=]'๊ตฌ๋ถ„์ž'], [[maxsplit=]๋ถ„ํ•  ํšŸ์ˆ˜]) ํ˜•ํƒœ๋กœ ์‚ฌ์šฉ!
    
    (1) ๊ตฌ๋ถ„์ž ์ƒ๋žต ์‹œ ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ๋‚˜๋ˆ„์–ด์คŒ 
        (๊ตฌ๋ถ„์ž ์ƒ๋žต ์‹œ, maxsplit ํ‚ค์›Œ๋“œ๋Š” ์ƒ๋žต ๋ถˆ๊ฐ€! -> ๋ฌธ์ž์—ด.split(maxsplit=1) ์ด๋ ‡๊ฒŒ ์‚ฌ์šฉํ•ด์ค˜์•ผ ํ•จ
        ๋ฌธ์ž์—ด.split(1) ์ด๋Ÿฐ ์‹์œผ๋กœ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด, 1์„ ๊ตฌ๋ถ„์ž๋กœ ์ธ์‹ํ•˜๊ฒŒ ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Œ)
    (2) ๋ถ„ํ•  ํšŸ์ˆ˜ ์ƒ๋žต ์‹œ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๋ถ„ํ• ์„ ์‹œํ–‰ํ•จ
    (3) ๋ฌธ์ž์—ด์—๋งŒ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๊ณ , input ํ•จ์ˆ˜ ๋˜ํ•œ ๋ฌธ์ž์—ด ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— input().split() ์ด๋Ÿฐ ์‹์œผ๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅ!
"""

d, e = map(int, input().split())
print(d, e) # in: 1 2 -> out: 1 2

f, g = map(str, input().split())
print(f, g) # in: 1 2 -> out: 1 2

input_list1 = input().split()
print(input_list1) # in: 1 2 -> ['1', '2']

input_list2 = list(map(lambda x: int(x), input().split()))
print(input_list2) # in: 1 2 -> out: [1, 2]

input_list3 = input().split(sep=' ', maxsplit=2)
print(input_list3) # in: 1 2 3 4 -> out: ['1', '2', '3 4']
# maxsplit์„ ์ •ํ•ด์ฃผ๋ฉด ํ•ด๋‹นํ•˜๋Š” ํšŸ์ˆ˜ ๊นŒ์ง€๋งŒ ์•ž์—์„œ๋ถ€ํ„ฐ ๋ถ„ํ• ํ•ด์ค€๋‹ค.

 

 

๐Ÿ“Œ ์ถœ๋ ฅ

"""
๋ณ€์ˆ˜๋‚˜ ์ƒ์ˆ˜ ๊ฐ’์„ ๋ฌธ์ž์—ด๊ณผ ํ•จ๊ป˜ ์ถœ๋ ฅํ•˜๊ณ  ์‹ถ์„ ๋• ๊ทธ๋ƒฅ ๋”ํ•ด์„œ! ์ถœ๋ ฅํ•˜๋ฉด ์‰ฝ๋‹ค.
"""

x = 1
y = 2
print("x ์ขŒํ‘œ: " + str(x) + ", y ์ขŒํ‘œ: " + str(y)) # x ์ขŒํ‘œ: 1, y ์ขŒํ‘œ: 2


"""
string formatting์„ ํ™œ์šฉํ•˜๋ฉด ๋” ๊น”๋”ํ•˜๊ฒŒ๋„ ํ‘œํ˜„ ๊ฐ€๋Šฅ!
"""

a = 123
b = 456
print(f"a๋Š” {123}, b๋Š” {456}") # a๋Š” 123, b๋Š” 456

name = "์„คํฌ"
num = 140
score = 4.0
print("%s๋Š” %dํ•™์ ์„ ๋“ค์—ˆ๊ณ , ํ‰๊ท  ํ•™์ ์€ %f" %(name, num, score)) # ์„คํฌ๋Š” 140ํ•™์ ์„ ๋“ค์—ˆ๊ณ , ํ‰๊ท  ํ•™์ ์€ 4.000000

 

๋Œ“๊ธ€