print/inputํจ์
print() : ํ์ค์ถ๋ ฅ์ฅ์น. ์ฝ์ ์ถ๋ ฅ.
input() : ํ์ค์
๋ ฅ์ฅ์น. ํค๋ณด๋ ์
๋ ฅ.
ํ ๋ณํ
1. print()
help(print) #๋์๋ง
1) ๊ธฐ๋ณธ ์ธ์
print(10, 20, 30) #10 20 30 -> sep=' ' (ํ ์นธ์ ๊ณต๋ฐฑ) print('010','1234','1234', sep='-') #010-1234-1234
end ์์ฑ
print(10,20, sep=',', end=',') print(30)
2) % ์์๋ฌธ์
ํ์) print('%์์๋ฌธ์'%๊ฐ)
num1 = 10 num2 = 20 tot = num1 + num2 print('%d + %d = %d'%(num1, num2, tot)) #10 + 20 = 30 print('์ด๋ฆ์ %s ์ด๊ณ , ๋์ด๋ %d ์ด๋ค.'%('ํ๊ธธ๋', 35)) #์ด๋ฆ์ ํ๊ธธ๋ ์ด๊ณ , ๋์ด๋ 35 ์ด๋ค. print('์์ฃผ์จ = %8.3f'%(3.14159)) #์์ฃผ์จ = 3.142
50%
print('์ ์ฒด ์ฐฌ์ฑ๋ฅ ์ %d%% ์ด๋ค.'%50) #์ ์ฒด ์ฐฌ์ฑ๋ฅ ์ 50% ์ด๋ค.
3) format()ํจ์ ์ด์ฉ
ํ์) print({์์}.format(๊ฐ))
print('์ด๋ฆ์ {}์ด๊ณ , ๋์ด๋ {}์ด๋ค.'.format('ํ๊ธธ๋', 35))
{์์น:์์}
print('์ ์ํ = {0:d}, {1:5d}, ์ฐ๋ด:{2:3,d}'.format(123,1234,3500)) #์ ์ํ = 123, 1234, ์ฐ๋ด:3,500 print('์์ฃผ์จ = {0:.3f}, {1:8.3f}'.format(3.14159, 3.14159)) #์์ฃผ์จ = 3.142, 3.142
์ถ์ฝํ format
name = 'ํ๊ธธ๋'; age=35
sql = 'select * form emp where name = '{}' and age = {}'.format(name, age)
sql = f"select * from emp where name = '{name}' and age = {age}" print(sql) #select * from emp where name = 'ํ๊ธธ๋' and age = 35
2. input() : ํ์ค์
๋ ฅ์ฅ์น
ํค๋ณด๋(๋ฌธ์์ด) -> ์ ์ํ ๋ณํ
a = int(input('์ ์ ์
๋ ฅ : ')) #20 b = int(input('์ ์ ์
๋ ฅ : ')) #30 print(a+b) 2030 -> 50
ํค๋ณด๋(๋ฌธ์์ด) -> ์ค์ํ ๋ณํ
x = float(input('์ค์ ์
๋ ฅ : ')) #12.5 y = float(input('์ค์ ์
๋ ฅ : ')) #25.34 print(x+y) #37.84
3) ํ ๋ณํ (Casting)
pirnt(int(24.5)) #์ค์ -> ์ ์ print(int('120')*2) #๋ฌธ์์ด -> ์ ์ int('no123') #ValueError print(2 + '100') #TypeError print(2 + int('100')) #102 print('๋์ด : ' + str(35)) #๋์ด : 35 -> ๊ฒฐํฉ์ฐ์ฐ์ print('๋์ด :', 35) #๋์ด : 35
booLeanํ -> ์ ์
print(int(True)) #1 print(int(False)) #0
๋ฌธ์์ด
๋ฌธ์์ด(String) ํน์ง
์์ ์๋ ๋ฌธ์๋ค์ ์งํฉ
์์ธ ์ฌ์ฉ ๊ฐ๋ฅ ([0]๋ถํฐ ์์ํ๋ฏ๋ก n-1๋ฒ์งธ๋ฅผ ์์งํ ๊ฒ)
indexing(ํน์ ๋ฌธ์์ด ์์น ๋ฐํ)/slicing(๋ฌธ์์ด ์ชผ๊ฐ๊ธฐ) ๊ฐ๋ฅ
์์ ์์ ๋ถ๊ฐ๋ฅ
1. ๋ฌธ์์ด ์ ํ
lineStr = "this is one line string" #ํ ์ค ๋ฌธ์์ด print(type(lineStr)) #<class 'str'>
* class : ๊ฐ์ฒด ์์ฑ ๋๊ตฌ
* object(๊ฐ์ฒด) : class์ ์ํด ๋ง๋ค์ด์ง ๊ณผ์ ๋ฌผ
์ฌ๋ฌ ์ค ๋ฌธ์์ด
multiLine = """This is multi line string""" print(type(multiLine)) print(multiLine) ''' This is multi line string '''
\n : ์ค๋ฐ๊ฟ ์ ์ด๋ฌธ์
multiLine2 = "This \nis multi line \nstring" print(multiLine2) ''' This is multi line string '''
SQL๋ฌธ
query = '''select * from emp where deptno = 1001 order by sal desc''' print(query)
indexing / slicing ๊ฐ๋ฅ
print(lineStr)
์ผ์ชฝ ๊ธฐ์ค ์์ธ : 0
lineStr[0] #t lineStr[0:4] #this lineStr[:4] #this
์ค๋ฅธ์ชฝ ๊ธฐ์ค ์์ธ : -1
lineStr[-1] #g lineStr[-6:] #string lineStr[-11:-7] #line
slicing : new object
subStr = lineStr[:4] print(subStr) #this
๊ฐ์ฒด ์ฃผ์
id(subStr) #2857529890416 id(lineStr) #2857529890416
3. ๋ฌธ์์ด ์ฐ์ฐ : +, *
print('python' + ' program') #python program ๊ฒฐํฉ์ฐ์ฐ์ print('-' * 5) #-----
4. ๋ฌธ์์ด ์ฒ๋ฆฌ ํจ์
type(lineStr) #str len(lineStr) #23
ํ์) ๊ฐ์ฒด.ํจ์()
testStr = "My name is hong!" print(testStr) #My name is hong! type(testStr) #str
1) ๋ฌธ์ ์ฐพ๊ธฐ
testStr.find('z') #-1 ์์ธ ๋ฐํ. ๋ฌธ์ ์์ testStr.find('m') #5 ์์ธ ๋ฐํ testStr.find('n') #3 ์ต์ด๋ก ๋ฐ๊ฒฌ๋ ์์ธ ๋ฐํ testStr.count('n') #2 ๊ฐ์ ๋ฐํ
2) ๋ฌธ์ ์ ํ ๊ตฌ๋ถ
testStr.isalpha() #False ์ํ๋ฒณ์ธ์ง ์๋์ง ํ๋จ testStr.isascii() #True ์๋ฌธ์/ํน์๋ฌธ์/์ซ์ testStr.isalnum() #False ์ซ์ testStr.islower() #False
3) ๋์๋ฌธ ์ฒ๋ฆฌ
testStr.upper() #'my name is hong!' ์ผ๊ด ๋๋ฌธ์ ๋ณ๊ฒฝ testStr.lower() #'my name is hong!' ์ผ๊ด ์๋ฌธ์ ๋ณ๊ฒฝ print(testStr) testStr2 = testStr.lower() print(testStr2)
4) ์ ๋์ด ํ๋จ -> T/F ๋ฐํ
testStr.startswith('My') #True testStr.startswith('Me') #False
5) ๋จ์ด ๊ต์ฒด
testStr = testStr.replace('!','') #๋๋ํ ์ญ์ testStr #'My name is hong' testStr = testStr.replace(' ','') #๊ณต๋ฐฑ ์ญ์ testStr #'Mynameishong'
5. ๋ฌธ์์ด ๋ถ๋ฆฌ(split) & ๊ฒฐํฉ(join)
๋ฌธ๋จ <-> ๋ฌธ์ฅ ๋ถ๋ฆฌ
๋ฌธ์ฅ <-> ๋จ์ด ๋ถ๋ฆฌ
print(multiLine) ''' This is multi line string '''
1) ๋ฌธ๋จ -> ๋ฌธ์ฅ split
sent = multiLine.split(sep='\n') #์ค๋ฐ๊ฟ์ ๊ธฐ์ค์ผ๋ก separateํ๊ฒ ๋ค sent['This', 'is multi line', 'string'] print('๋ฌธ์ฅ ๊ฐ์:', len(sent)) #๋ฌธ์ฅ ๊ฐ์: 3
2) ๋ฌธ๋จ -> ๋จ์ด split
word = multiLine.split() #sep=' ' : ๊ณต๋ฐฑ ์๋ต ๊ฐ๋ฅ word #['This', 'is', 'multi', 'line', 'string'] print('๋จ์ด ๊ฐ์:', len(word)) #๋จ์ด ๊ฐ์: 5
3) ๋ฌธ์์ด ๊ฒฐํฉ(join)
ํ์) '๊ตฌ๋ถ์'.join(object)
lines = ''.join(word) print(lines)
escape
escape ๋ฌธ์ & ๊ธฐ๋ฅ ์ฐจ๋จ
- escape ๋ฌธ์ : ํน์๊ธฐ๋ฅ์ ๋ฌธ์(\n, \t, \b, \r, '', "")
- ๊ธฐ๋ฅ ์ฐจ๋จ : escape ๋ฌธ์๋ฅผ ์ถ๋ ฅ or ๊ฒฝ๋ก ์ฌ์ฉ ์
print('escape ๋ฌธ์') print('\n์ถ๋ ฅ') # escape ๋ฌธ์ ์ ์ฉ print('\\n์ถ๋ ฅ') # ๊ธฐ๋ฅ ์ฐจ๋จ1 print(r'\n์ถ๋ ฅ') # ๊ธฐ๋ฅ ์ฐจ๋จ2
๊ฒฝ๋ก ํํ
print('c:\python\test') # c:\python est print('c:\\python\\test') # c:\python\test print(r'c:\python\test') # c:\python\test print('c:/python/test')
ํน์๋ฌธ์ : c:\'aa'\"abc.txt"\
print("c:\\\'aa\'\\\"abc.txt\"\\") # c:\'aa'\"abc.txt"\
file read
file = open(file = r'c:\itwill\3_python-i\test.txt', encoding='utf-8') print(file.read())
'๋ฐ์ดํฐ๋ถ์๊ฐ ๊ณผ์ > Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
DAY36. Python ์ ๊ทํํ์ (0) | 2021.11.04 |
---|---|
DAY35. Python Basic ์๋ฃ๊ตฌ์กฐ (2)set, dict (0) | 2021.11.03 |
DAY34. Python Basic ์๋ฃ๊ตฌ์กฐ (1)list, tuple (0) | 2021.11.02 |
DAY33. Python Basic ์กฐ๊ฑด๋ฌธ, ๋ฐ๋ณต๋ฌธ (0) | 2021.11.01 |
DAY31. Python Basic ์ค์น, ๋ณ์์ ์ฐ์ฐ์ (0) | 2021.10.28 |