Python Basic Exec1

JeyS·2020년 6월 3일

Python Exec

목록 보기
1/3

python image2

print(r"c:\abc\df\nm")

r = raw의 뜻으로 '\n'을 넣기 위해서는 앞에 특별한 의미를 지니지 않는다는 r을 넣어줘야 된다.

a=float(input())
print("%f"%a)

실수형 입력 받기 후 출력. %f는 실수형으로 서식지정(formating)하는 것. %a는 a변수를 받아오는 것.

%s는 string(문자열)이고, %d는 decimal(10진 정수).

a=float(input())
print("%.2f"% a)

%.2f 는 .2를 통해 소수점 몇째 자리까지 표현할지 지정가능.

a=int(input())
b=int(input())
print(a,b)

정수 2개 입력받아서 출력.

a=int(input())
b=int(input())
print("%d%03d" % (a, b))

정수 2개 입력받음. %03d >> 3은 출력값 앞에 빈칸을 2칸 띔(기본값 1인듯). 0은 빈칸을 0으로 채움.

a= str(input())
b= str(input())
print("%s %s" %(b, a))
a, b = input().split()
a=int(a)
b=int(b)
print("%d %d" %(a,b))

값을 2개 받아서 정수로 변환한 다음 출력. 출력값 사이에 공백하나.

a, b =map(int, input().split())
print("%d %d" %(a,b))

map 함수를 이용하여 좀더 간편히 변수 변환하기.

a, b = map(str, input().split())
print("%s %s" %(b,a))
a= float(input())
print("%.2f" %a)
a = int(input())

print("%d %d %d" %(a,a,a))
while True:
    try:
        h,m = input().split(sep=':')
        h = int(h)
        m = int(m)
        print("%d:%02d" %(h, m))
        break
    except ValueError:
        print("'시:분' << 형식으로 입력해주세요.")
    finally:
        print("Input completed".")

while True ~ break 명령으로 입력값이 참일때까지 계속 코드를 반복하는 명령을 넣고, try except로 error 발생시 실행할 코드를 넣어둔다.

sep은 multi input대상의 형식구분자. finally는 위쪽작업 후 가장 마지막에 항상 실행되는 코드를 넣을때 씀.

y,m,d = input().split(sep='.')
y = int(y)
m = int(m)
d = int(d)
print("%d.%02d.%02d" %(y,m,d))
import datetime
date = datetime.datetime.now()
print(date)
print(date.strftime('%Y-%m-%d'))
print(date.strftime('%y.%m.%d.%h'))
testtime = date.strptime('30.03', '%y.%m')
print(testtime)

strftime 형식지정하면 날짜를 문자열로, strptime 형식에 맞춰 입력한 데이터를 객체로 변환.

help(datetime)
import datetime
datetime.timezone.max
datetime.datetime.fromtimestamp(tz=)

0개의 댓글