-
Ch03. 파이썬 기초 (제어문)Raspberry Pi ( python ) 2023. 2. 24. 11:14
1 _ if 문
: 다른 프로그램과 기능적으로 큰 차이는 없다. if , else , elif 문 3개가 있다.
기본 형태 if 조건: 실행문 else: 실행문 if 조건: 실행문 elif 조건: 실행문 else: 실행문
예제)
2 _ for 문
: 특정 코드를 지정 횟수만큼 반복하는 문장
for 변수 in range( 시작 값 , 조건값(원하는 종료값+1) , 증감값 ): 실행문
예제)
- for 문을 선언할 때 처리할 목록은 리스트, 튜플, 문자열이 가능하고 이 변수들의 첫 요소부터 마지막 요소까지 변수에 대입되어 반복이 됩니다.
예제) 다양하게 for 문을 활용해보자
- print( ) 내부에 end="" 는 기본적으로 적용되어있는 print() 문의 개행을 없에고 print문이 출력이 끝난 지점에 내용을 추가할 수 있다. 본 예제에서는 " " 내부에 아무것도 넣지 않는 것으로 print문의 개행만 막는 역할을 했다.
3 _ while 문
: if 문의 반복문 형태라고 할 수 있다. 조건을 지정하고 조건이 true일 때 실행되며, 조건이 false가 될 때까지 반복한다.
조건이 false가 되지 않으면 무한루프가 발생한다.
예제) 일반적인 while문 선언과 while 안에 if문을 중첩으로 선언해보자
4 _ break 문
: 반복문을 종료시킵니다. 반복문에 break를 기술할 경우 해당 반복문의 코드는 실행되지 않고 반복문을 빠져 나오게 됩니다.
break;
예제) for 문과 while 문에 break를 사용하여 반복을 종료하는 예제입니다.
'Raspberry Pi ( python )' 카테고리의 다른 글
CH.02 라즈베리 파이를 위한 리눅스 기초 배우기 (0) 2023.02.28 Ch01. 라즈베리파이 시작하기 (0) 2023.02.27 Ch03. 파이썬 기초 (함수, 클래스, 모듈) (0) 2023.02.24 라즈베리파이 : 파이썬 연습문제 (02/23) (0) 2023.02.23 Ch03. 라즈베리 파이를 위한 파이썬 기초 (0) 2023.02.23