거북이개발자

[Python 기초 문법] 제어문 본문

Algorithm(Python)/기초

[Python 기초 문법] 제어문

류정식 2020. 12. 30. 12:05

1. if문

ㅇif 문의 기본 틀.

     -ex) if money >=3000 :

              print("택시를 타고 가라")

           else :

              print("걸어 가라")

 

ㅇif 문은 같은 너비로 들여쓰기를 꼭 해야한다.

 

ㅇand, or, not 연산자

     -and : x and y

     -or :  x or y

     -not :  not x

 

ㅇin, not in 연산자

     -in : x in 리스트, 튜플, 문자열

     -not in :  not in 리스트, 튜플, 문자열

 

ㅇelif 문 다른 언어의 else if와 같은 역활

     -ex) if money >=3000 :

              print("택시를 타고 가라")

           elif money >=1500 :

              print("버스를 타고 가라")

           else: 

              print("걸어 가라")

 

2. while문

ㅇwhile 문의 기존 틀.

     -ex) treeHit = 0

           while treeHit < 10 :

              treeHit = treeHit + 1

              print("나무 %d번 찍었습니다." %treeHit)

 

ㅇbreak, continue문 사용가능.

 

ㅇwhile True : ->로 무한루프 가능.

 

3. for문

ㅇfor 문의 기존 틀.

     -ex) test_list = ['one', 'two', 'three']

           for i in test_list:

              print(i)

 

ㅇfor문과 자주 사용하는 range 함수.

    -ex) for i in range(1, 11):

Comments