본문 바로가기
IT/Python

Python - 기본문법

by 최고영회 2018. 8. 27.
728x90
반응형
SMALL
# 쌍따옴표, 홑따옴표 차이는 없지만 홑따옴표를 기본으로 하자. 
print('hello world')

# 따옴표 세 개 
'''This is a multi-line string.
This is the second line.
"What's your name?
'''

# 문자열 포맷팅
age = 20
name = 'yhkim'

print('{0} was {1} years old when the wrote this book'.format(name, age))
# name + ' is ' str(age) + ' years old' 와 같이 작성하지 말고 format 을 이용하자. 
print('{0: .3f} , {1: .2f}'.format(1.0/3, 1/3))
print('{0:_^11}'.format('hello'))
print("a", "b")  # comma 를 사용하지 않으면 python 의 print는 기본적으로 모두 \n 가 들어간다. 
print(r'NewLines are indicated by \n') # 앞에 r 또는 R 을 넣으면 이스케이프 처리가 된다.


i = 5
print(i)

i = 5;
print(i);

i = 5; print(i);

# 위 3가지는 모두 같은 결과를 보여준다. 
# 다만, Python 은 한 물리적 명령행에 두개 이상의 논리적 명령행을 사용하지 말것을 강력히 권한다. 
# 그리고 세미콜론은 작성하지 않는 것이 일반적이다. 

# 들여쓰기 할 때는 공백 4개를 이용하자. (파이썬 언어에서 공식적으로 추천하는 방법)
# 들여쓰기를 기준으로 코드블럭이 정해지기 때문에 잘못된 들여쓰기는 문법 오류에 속한다.





num = 23
guess = int(input('enter an integer: '))

if guess == num:
    print('congratulations, you guessed it')
    print('but you do not win any prizes!')
elif guess < num:
    print('NO, it is a little higer than that')
else:
    print('NO, it is a little lower than that ')




num = 23
run = True

while run:
    guess = int(input('enter an integer:'))

    if guess == num:
        print('greate !!')
        run = False
    elif guess > num:
        print('It is a little higher than that')
    else:
        print('It is a little lower than that')
else:
    print('The while loop is over')

print('Done')
728x90
반응형
LIST

'IT > Python' 카테고리의 다른 글

Python - I/O, File I/O, pickle  (0) 2018.09.10
Python - Class  (0) 2018.09.07
Python - 자료구조  (0) 2018.08.30
Python - 함수  (0) 2018.08.27
Python 시작하기 - hello world  (0) 2018.08.24