2022/12/23/THU
s='python\n\n'
# 직접 enter로 다음 줄로 넘겨도 됨
s
print(s)
- ''' => 긴 str배정시에 사용
s='python'
s.capitalize()
s.find('t')
s.count('y')
s.count('x')
s.count('p')
# 대소문자 구분X
s.find('a')
s.index('t')
a='pyathaon'
a.find('a')
a.index('a')
a.isalpha()
s='asd342'
s.isalnum()
a='python 3'
a.isalnum()
s='123'
# 십진법!
s.isdecimal()
s='223311.3'
s.isdecimal()
s.isdigit()
s.isnumeric()
s='123123123'
s.isdecimal()
a='2034'
a.isdigit()
a='3\u00B3'
a
# 십진법
a.isdecimal()
a.isdigit()
a.isnumeric()
a='\u00BC'
a
a.isdecimal()
a.isdigit()
a.isnumeric()
a='Python'
a.islower()
a.isupper()
중요
a='PythonP'
a.replace('P','Q')
중요
a='I like python'
# space 기준으로 나눈 뒤 리스트化
a.split(' ')
a='I,like,python'
a.split(',')
str에서 다음줄로 내릴 때
a='I like python\nHe likes python\nfucking python'
a
print(a)
a.splitlines()
b=a.splitlines()
b[0]
# 맨 앞만 대문자로 변경하는 건 Caitalize
b[0].upper()
b[0].count('L')
s='python'
a=3
print(s,a)
print(s,str(a))
print(s+str(a))
print(s + ' ' + str(a))
'Python {}'.format(25)
'Python {1} + {0}'.format(3,'asdas')
'Python {} {}'.format(3,'asd')
s='Python {1} {0}'
s.format(3,'asd')
s= 'My name is {} I am {} years old'
s.format('asd',12)
ver=3.8
print('python', ver)
print('python' + str(ver))
print('python{}'.format(ver))
s='python{}'.format(ver)
print(s)
data=4321.123456
print('DATA'+str(data))
print('DATA', data)
print('DATA '+str(data))
print('DATA= '+str(data))
print('DATA={}'.format(data))
참고
- {0:<10} => {0} 위치의 값을 ":<10" 10자리로 표현할건데 왼쪽 정렬
- {1:>5} => {1} 위치의 값을 ":>5" 5자리로 표현할건데 오른쪽 정렬
- {0:^10} => {0} 위치의 값을 ":^10" 10 자리로 표현할 건데 가운데 정렬
print('DATA= {:.2f}'.format(data))
print('DATA= {:.0f}'.format(data))
print('DATA={:4.0f}'.format(data))
print('DATA={:5.0f}'.format(data))
print('DATA={:6.0f}'.format(data))
print('DATA={:7.0f}'.format(data))
print('DATA={:,}'.format(data))
data2=454654456.1213546
print('DATA={:,}'.format(data2))
- data자체가 float이라서 d입력하면 오류가 발생
- int는 소수점 없으니까 .4d 이렇게 입력하면 오류 그냥 4d로 입력해야 함
data
print('DATA={:4d}'.format(int(data)))
print('DATA={:5d}'.format(int(data)))
print('DATA={:8d}'.format(int(data)))
scientific notation:e라는 것을 사용하여 수를 표현해줌
print('data = {:e}'.format(data))
- % 이용해보자
print('DATA=%(x)f' % {'x' : data})
print('DATA=%(x).0f' % {'x' : data})
print('DATA=%(x).2f' % {'x' : data})
print('DATA=%(x)7.0f' % {'x' : data})
print('DATA=%(x)8.3f' % {'x' : data})
print('DATA=%(x)8.0f' % {'x' : data})
import math
print(math.pi)
import numpy as np
print(np.pi)
math.inf
np.inf
math.nan #숫자가 아님을 표현할 때
np.nan
math.e
# 올림, 반올림과 다름
math.floor(math.pi)
# 버림, 반올림과다름
math.ceil(math.pi)
round(math.pi,0)
math.trunc(math.pi)
int(math.pi)
math.sqrt(math.pi)
np.sqrt(math.pi)
math.log(10)
math.log10(10)
math.log(math.e)
p=math.pi
math.sin(p/2)
math.sin(2*p)
x_degree=90
x_radian=x_degree*(math.pi/180)
x_radian #pi/2랑 동일
math.radians(90)
math.pow(2,3)
pow(2,3)
2**3
x=2
math.isinf(x)
math.isnan(x)
*중요
print(math.fsum([1,2,3]))
print(int(math.fsum([1,2,3])))
# error 발생
# 원소가 int일때만 가능
import os
os.chdir('c:\\test')
os.mkdir('temp4')
os.chdir('temp4')
os.getcwd()
os.chdir('..')
os.getcwd()
os.chdir('temp')
os.getcwd()
os.chdir('.')
os.getcwd()
os.mkdir('dir0')
os.mkdir('dir3')
os.mkdir('dir4')
os.getcwd()
os.listdir()
os.rename('dir0','dir100')
os.listdir()
os.rmdir('dir100')
os.listdir()
import sys
# 파이썬에서 사용가능한 모듈들을 불러준 것
# sys.path
os.chdir('C:\\Users\\ehfus\\Downloads\\python Introduction')
os.chdir('c:\\test\\temp')
os.getcwd()
f=open('file1.txt','w')#파일 작성
f.closed
f.write('fucking')
f.close()
f.closed
with open('file2.txt','w') as f:
f.write('sibal\n')
f.write('jotgatne\n')
f.write('just joke\n')
f.closed
fid=open('file2.txt','r')
fid.read()
fid.close()
fid.closed
fid=open('file2.txt','r')
fid.readline()
fid.readline()
fid.readline()
fid.readline()
한 줄씩 불러옴
fid.close()
fid=open('file2.txt','r')
data=[]
data.append(fid.readline())
data.append(fid.readline())
data.append(fid.readline())
data
자동화
data=['string1','string2','string3']
import json
f=open('file3.txt','w')
json.dump(data,f)
f.close()
f=open('file3.txt','r')
x=json.load(f)
x
f.close()
import pickle
f=open('file4.txt','wb') # 바이너리로 쓰겠다
용량을 작게 할 수 있다는 장점이 있음
pickle.dump(data,f)
f.close()
f=open('file4.txt','rb') # 바이너리 불러옴
x= pickle.load(f)
x
f.close()