ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด/Python

ํŒŒ์ด์ฌ ์ž…์ถœ๋ ฅ(ํ‘œ์ค€, ํŒŒ์ผ ๋“ฑ)์˜ ๋ชจ๋“  ๊ฒƒ

์„œ์•„๋ž‘๐Ÿ˜ 2023. 12. 5. 00:51

 

๋“ค์–ด๊ฐ€๋ฉฐ

์ž…์ถœ๋ ฅ์˜ ์ข…๋ฅ˜ ๋ฐ ํ‘œ์ค€ ์ž…์ถœ๋ ฅ, ํŒŒ์ผ ์ž…์ถœ๋ ฅ์— ๋Œ€ํ•œ ๊ธฐ๋ณธ์ ์ธ ์„ค๋ช…์€ ์—ฌ๊ธฐ๋ฅผ ์ฐธ๊ณ ํ•˜์‹œ๋ฉด ๋„์›€์ด ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

[C++] 13-1. ์ž…์ถœ๋ ฅ(feat. ํ‘œ์ค€์ž…์ถœ๋ ฅ๊ณผ ํŒŒ์ผ์ž…์ถœ๋ ฅ)

โœ… ์ŠคํŠธ๋ฆผ ์ŠคํŠธ๋ฆผ์€ ํ•˜๋‚˜์˜ ํ๋ฆ„์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค. ์ˆ˜๋„๊ผญ์ง€๋ฅผ ํ‹€์–ด๋†“๊ณ  ๋„ค๋ชจ๋‚œ ํ˜ธ์Šค๋ฅผ ๋ผ์šฐ๋ฉด ๋ฌผ์ด ๋„ค๋ชจ ๋ชจ์–‘์œผ๋กœ ๋‚˜์˜ค๊ณ , ๋™๊ทธ๋ž€ ํ˜ธ์Šค๋ฅผ ๋ผ์šฐ๋ฉด ๋™๊ทธ๋ž—๊ฒŒ ๋‚˜์˜จ๋‹ค. ์šด์˜์ฒด์ œ๊ฐ€ ๋ฌผ์„ ๊ณต๊ธ‰

ground90.tistory.com

 

 

 

ํ‘œ์ค€ ์ž…์ถœ๋ ฅ

input

>>>> a = input("์ž…๋ ฅํ•˜์„ธ์š”: ")    # ์ž…๋ ฅ
์ž…๋ ฅํ•˜์„ธ์š”: abc

>>>> print(a)
'abc'

 

output

# ํฐ๋”ฐ์˜ดํ‘œ ์—ฌ๋Ÿฌ๊ฐœ print
>>>> print("Let" "it" "be")
'Letitbe'

>>>> print("Let"+"it"+"be")
'Letitbe'

# ๋ฌธ์ž์—ด ๋„์–ด์“ฐ๊ธฐ๋Š” ์ฝค๋งˆ
>>>> print("Let","it","be")
'Let it be'

# ํ•œ์ค„์— ์ด์–ด์“ฐ๊ธฐ
>>>> for i in range(10):
	print(i, end=' ')

0 1 2 3 4 5 6 7 8 9 >>>>

 

ํŒŒ์ผ ์ž…์ถœ๋ ฅ

 

ํŒŒ์ผ ์“ฐ๊ธฐ(open → write → close)

f = open("abc.txt", 'w')
for i in range(1, 11):
	f.write(f"{i}๋ฒˆ์งธ ์ค„์ž…๋‹ˆ๋‹ค.\\n")

f.close()

 

ํŒŒ์ผ ์ฝ๊ธฐ(readline, readlines, read)

# readline
f = open("abc.txt", 'r')
while True:
	line = f.readline()
	if not line: break
	print(line)

f.close()

# readlines
f = open("abc.txt", 'r')
lines = f.readlines()
for line in lines:
	print(line)

f.close()

# read
f = open("abc.txt", 'r')
data = f.read()
print(data)
f.close()

 

with๋ฌธ๊ณผ ๊ฐ™์ด์“ฐ๊ธฐ

with๋ฌธ์€ ํŒŒ์ผ์„ ์—ด๊ณ  ์ž๋™์œผ๋กœ ๋‹ซ๋Š” ๊ฒƒ ๊นŒ์ง€ ๊ฐ™์ด ํ•ด์ค๋‹ˆ๋‹ค.

with open("abc.txt", "w") as f:
	for i in range(1, 11):
		f.write(f"{i}๋ฒˆ์งธ ์ค„์ž…๋‹ˆ๋‹ค.\\n")

 

Sys ๋ชจ๋“ˆ(ํ”„๋กœ๊ทธ๋žจ ์ธ์ž)

import sys

args = sys.argv[1:]
for i in args:
	print(i)

 


 

์ž…์ถœ๋ ฅ์— ๊ด€ํ•œ ๊ฐ„๋‹จํ•œ ์†Œ๊ฐœ์ž…๋‹ˆ๋‹ค. ์‘์šฉ๋ฒ„์ „์€ ์ถ”ํ›„์— ์—…๋กœ๋“œํ•  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค:)