기타

파이썬 기본적인 사용법

mellamo 2023. 1. 11. 16:06

 

 

가장 기본적으로 출력하는 방법이다. 

print("hello world!")

 

 

변수를 입력하면 아래처럼도 가능하다

a=1
b=4
print(a+b)

 

 

a,b = (5,8)
print(a,b)

컴퓨터언어에서는 문자와 숫자를 확실히 구분하는데, 

따옴표를 넣게 되면 

a="1"
b="1"
print(a+b)

숫자 11이 아닌 문자 1과 1이 합친 문자 11이 된다. 

 

 

 

print ('A','B',10,20.0,sep="/")
print ('A','B',10,20.0,sep="\t")

 

 

print ('A','B',10,20.0,sep="\n\n\n")

 

 

 

print("SeSAC \racademy is awesome")

 

 

 

 

print("SeSAC academy"s awesome")

 

 

print("C:\\Users\\user\\.ssh")

 

 

 

print('서식','문자',100,5/2)
print('%s %s %d %.2f' % ('서식','문자',100,5/2))
print('{} {} {} {}'.format('서식','문자',100,5/2))
print('{} {} {} {:.2f}'.format('서식','문자',100,5/2))

 

 

print('%s %s %s' %('name', 'age', 'gender'))
print('%10s %10s %10s' %('name', 'age', 'gender'))
print('%-10s %-10s %-10s' %('name', 'age', 'gender'))
print('{:10} {:10} {:10}'.format('name', 'age', 'gender'))
print('{:>10} {:>10} {:>10}'.format('name', 'age', 'gender'))
print('{:^10} {:^10} {:^10}'.format('name', 'age', 'gender'))

 

 

 

 

 

 

print('{:,} {:,}'.format(1000,10000))

 

 

print('{gender} {name} {age}'.format(name='IU',age='70',gender='Female'))
print('{p[gender]} {p[name]} {p[age]}'.format(p={'name':'IU','age':'70','gender':'Female'}))

 

 

위에 배운것으로 아래를 출력하려면

print("============================================")
print('{:13}{:5}{:^20}{:13}'.format("name","age","phone","money"))
print("============================================")
print('{:13}{:^5}{:^20}{:13}'.format('Kim',38,'010-1111-1111','20.50$'))
print('{:13}{:^5}{:^20}{:13}'.format('Seo',24,'010-1234-5678','30.30$'))
print('{:13}{:^5}{:^20}{:13}'.format('Lee',25,'010-2525-2345','50.60$'))
print('--------------------------------------------')
print('{:26}{:>18}'.format('Total','100.00$'))
print("============================================")