:실행되는 흐름을 제어한다
flow control (conditional & loop)
01
시간의 순서에 따라서 기능이 실행된다
=프로그램의 의미이자 자동화의 본질.
but
조건문 (조건에 따라서 실행되는 순서를 다르게 할 순 없을까?)
반복문 (조건을 만족하는 동안에 반복적으로 같은 작업을 실행하게 할 순 없을까? )
무엇을 실행할지 언제까지 반복할지 선택해야 하는데 선택의 핵심은 비교다!!
비교를 위한 연산자 -> 비교 연산자 (compare operator)
비교의 결과를 표현하는 데이터 타입 -> boolean
boolean (true & false) -> compare operator -> flow control(conditional & loop)
*특별한 연산자 느낌표!
!가 붙으면 반대의 의미 (다르냐~~)
1 != 1 1과 1이 다른가요
= 대입연산자
== 비교연산자
if boolean:
code
input_id = input('id : ')
id = 'egoing'
if input_id == id:
print('Welcome')
if boolean:
code
else:
code
input_id = input('id : ')
id = 'egoing'
if input_id == id:
print('Welcome')
else:
print('WHO?')
else if 줄인 elif엘리프
"if안이 true면 if코드 실행
if안이 false면 elif안에 있는 조건 체크,
elif안이 true면 elif코드 실행
elif안이 false면 else코드 실행"
*else는 생략할 수 있고, elif는 하나가 아니라 여러개가 올 수 있다!!!
input_id = input('id : ')
id1 = 'egoing'
id2 = 'basta'
if input_id == id1:
print('Welcome')
elif input_id == id2:
print('Welcome')
else:
print('WHO?')
조건문 중첩
아이디와 패스워드를 모두 만족할 때 실행되는 애플리케이션 만들기
input_id = input('id : ')
id = 'egoing'
input_password = input('password : ')
password = '111111'
if input_id == id:
if input_password == password:
print('welcome')
else:
print('wrong password')
else:
print('wrong id')
반복문 과 [ list ]
:이름은 필요없고 순서에 따라서 데이터를 저장할때 리스트 사용
리스트에 있는 값을 하나하나 꺼내서 반복적으로 일을 처리해야함
반복문과 리스트는 단짝!!!!
for문 기본형식
for 변수 in 리스트 :
print(변수)
-처리하고자 하는 데이터는 리스트 안에 있음!
-for문은 반복이 실행될때마다 리스트의 값들을 순차적으로 꺼내서 변수의 값으로 할당함!
->변수의 값은 리스트 내에서 각각의 순번에 해당되는 원소(element)가 된다
->(원소가 일억개라도) 원소의 갯수에 따라서 실행이된다
names = ['egoing', 'basta', 'blackdew']
for name in names:
print(name)
print('Hello, '+name+'. Bye, '+name+'.')
:정보가 복잡해지면 리스트 안에 리스트가 담기기도 함
일차원 리스트
['egoing', 'basta', 'blackdew' ]
이차원 리스트
[
['egoing','seoul','web'],
['basta','seoul','iot'],
['blackdew','tongyeong','ml'],
]
다차원리스트를 반복문으로 다루는 방법
persons = [
['egoing','seoul','web'],
['basta','seoul','iot'],
['blackdew','tongyeong','ml'],
]
for person in persons:
print(person[0]+','+person[1]+','+person[2])
동일코드
for name, address, interest in persons:
print(name+','+address+','+interest)
ㅡ
person = ['egoing','seoul','web']
name = person[0]
address = person[1]
interest = person[2]
print(name, address, interest)
동일 코드
name, address, interest = ['egoing','seoul','web']
print(name, address, interest)
dictionary 데이터 타입
key:value
순서는 상관없고 데이터에 이름을 주고싶을 때 사용
딕션어리 타입에서 데이터 가지고오는 방법
person = {‘name’ : ‘egoing’, ‘address’ : ‘seoul’, ‘interest’ : ‘web’}
print( person[‘name’] )
반복문 이용해 데이터 가지고 오는 방법
person = {'name':'egoing', 'address':'seoul', 'interest':'web'}
for key in person:
print(key, person[key])
완성코드
person = {'name':'egoing', 'address':'seoul', 'interest':'web'}
print(person['name'])
for key in person:
print(key, person[key])
persons = [
{'name':'egoing', 'address':'seoul', 'interest':'web'},
{'name':'basta', 'address':'seoul', 'interest':'iot'},
{'name':'blackdew', 'address':'tongyeong', 'interest':'ml'},
]
print('==== persons ====')
for person in persons:
for key in person:
print(key, ':', person[key])
print('-----------------')