복습
1. indentation에 둥지라는 뜻이 있다고 한다. for 반복문의 원소는 횟수와 값으로 사용할 수 있다.
for i in [1,2,3]:
print("환영합니다.")
for i in [1,2,3]:
print("i=", i)
2. range의 타입은 range타입이다. list를 만들기 위해서, tuple을 붙여줘야 한다. 반복문에서는 range타입으로만 요소를 주더라도 상관이 없다.
for i in range(1,101,2):
print(i)
3. turtle과 for 반복문을 통해서 그림그리기
import turtle
t = turtle.Turtle()
t.shape("turtle")
for i in range(3):
t.forward(100)
t.left(360/3)
t.penup()
t.goto(200,0)
t.pendown()
for i in range(4):
t.forward(100)
t.left(360/4)
4. 전역변수 설정을 해줘야 한다. 반복문은 조건이 참(True)일 동안 수행된다.
i = 0
while i < 5:
print(i)
i =i +1
5. %d, %s 고민할 바에 그냥 formating 써서 편하게 사용하자!
dan = input("원하는 단은?")
dan = int(dan)
for i in range(1,10):
print("%d * %d ="%(dan,i), dan*i)
print("{0}*{1}={2}".format(dan, i, dan*i))
n= 1
while n<10 :
print("%d * %d ="%(dan,n), dan*n)
print("{0}*{1}={2}".format(dan, n, dan*n))
n +=1
i = 2
while i < 10 :
print("{0}단 시작".format(i))
e=1
while e <10:
print("{0}*{1}={2}".format(i,e,i*e))
e += 1
i += 1
print("구구단 끝")
6. 문법과 형식이 필요한 것은 list 형식이 필요한 것! 배열과 리스트는 파이썬에서 같다.
리스트가 파이썬의 핵심인 이유는 반복문에 있다.
관용적으로 사용하는 패턴에 대해 익숙해져야 한다.
break는 하나의 while문 만을 벗어난다.
While True :
break
continue
While Condition :
body
condition satisfied
진도
1-1. 순차 구조에서는 그대로 진행된다.
1-2. 선택 구조에서는 참/거짓일 때의 실행과정이 달라진다.(if/else)
1-3. 반복문 while, for문
2-1. else문에 if문을 넣어주면 elif라고 한다.
2-2. if else에서 if에는 조건문이 들어가지만 else에는 조건문이 들어가지 않는다.
2-3. elif에는 조건문이 들어간다.
import random
anw = random.randint(1,50)
cnt = 1
guess = int(input("숫자를 입력해 봐"))
while guess != anw :
if guess > anw:
print("줄여")
elif guess < anw :
print("키워")
cnt +=1
guess = int(input("숫자를 입력해 봐"))
print("정답", "총 시도 횟수 {0}번".format(cnt))
3. 논리연산자 안에 관계연산자와 비교연산자가 있다.
논리연산자는 and or not이고
비교(관계)연산자는 부등호다.
3-1. 단항 연산자와 다항 연산자, 논리 값과 논리연산자의 콤비네이션! 햇갈리지 말자!
'2019년 혁신성장 청년인재 집중양성(빅데이터) > PYTHON 라이브러리' 카테고리의 다른 글
sklearn Doc2Vec 라이브러리 parameter (0) | 2019.10.07 |
---|---|
PYTHON 기초 (2) (0) | 2019.07.17 |
PYTHON 기초 (1) (0) | 2019.07.17 |