How We Coding

동영상 강의 : http://pythonkim.tistory.com/notice/77


# Day_02_01_loop.py



# 제어문과 반복문의 연결고리


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# 문제
# count 에 0~3 사이의 숫자를 입력 받아서
# 입력받은 숫자 만큼 아침인사를 해봅니다.
# 2가지 종류의 코드를 만들어보기.
 
 
def ans1():
    count = int(input('count : '))
 
    if count == 1:
        print("Good Morning!")
    elif count == 2:
        print("Good Morning!")
        print("Good Morning!")
    elif count == 3:
        print("Good Morning!")
        print("Good Morning!")
        print("Good Morning!")
 
 
def ans2():
    count = 2
    if count >= 1:
        print("Good Morning!")
    if count >= 2:
        print("Good Morning!")
    if count >= 3:
        print("Good Morning!")
 
 
def ans3():
    count = 2
    i = 0
    if i < count:
        print("Good Morning!")
        i += 1
        if i < count:
            print("Good Morning!")
            i += 1
            if i < count:
                print("Good Morning!")
                i += 1
                if i < count:
                    print("Good Morning!")
                    i += 1
                    if i < count:
                        print("Good Morning!")
                        i += 1
 
 
def ans():
    count = 3
    i = 0
    while i < count:
        print("Good Morning!")
        i += 1
 
cs




- 규칙


# 규칙을 찾는 것이 중요. (시작, 종료, 증감)

# 1 3 5 7 9     1~9 까지 2칸씩 건너뛴다.

# 0 1 2 3 4     0~4 까지 1칸씩 건너뛴다.

# 5 4 3 2 1     5~1 까지 -1칸씩 건너뛴다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
def rule1():
    i = 1                   # 시작
    while i <= 9:           # 종료
        print("Hello")
        i += 2              # 증감
 
 
def rule2():
    i = 0
    while i <= 4:
        print("Hello")
        i += 1
 
 
def rule3():
    i = 5
    while i >= 1:
        print("Hello")
        i -= 1
cs


>> Python 에서는 i++ 과 같은 증감 연산자를 제공하지 않는다고 한다. ㅜㅜ

>> 세 개 모두 "Hello" 를 5번 출력한다.



- 줄이 바뀌지 않게 하기


1
2
3
4
5
rint('Hello', end=' ')
print('python')
 
# 실행결과 
# Hello python
cs


>> end 파라미터를 사용하면 된다.


- end 파라미터의 또 다른 예시.


1
2
3
4
5
print('Hello', end='***')
print('python')
 
# 실행결과
# Hello***python
cs



-  문제


# 0 ~ 99 까지 출력하는 함수를 만드시오.


1
2
3
4
5
def show100():
    i = 0
    while i <= 99:
        print(i, end=' ')
        i += 1
cs




- for in 문 


1
2
3
4
5
6
7
8
9
10
11
12
13
for i in range(0101):
    print(i, end=' ')
print()
 
for i in range(010):
    print(i, end=' ')
print()
 
for i in range(10):
    print(i, end=' ')
print()
 
# 0 1 2 3 4 5 6 7 8 9 
cs


>> 시작 : 0, 종료 : 10, 증감 : 1

>> 시작, 종료, 증감이 명확한 경우 for 문을 사용한다.

>> 증감의 기본은 1, 시작의 기본은 0



# 문제

# count 에 0~3 사이의 숫자를 입력 받아서

# 입력받은 숫자 만큼 아침인사를 해봅니다.

# 2가지 종류의 코드를 만들어보기.


1
2
3
4
5
6
7
8
9
10
11
def sumOfOddEven():
    odd, even = 00
    for i in range(1100):
        if i%2 == 1: odd  += i
        else :       even += i
 
    return odd, even
 
 
s1, s2 = sumOfOddEven()
print(s1, s2)       # 2500 2450
cs



# 난수


1
2
3
4
5
import random
 
print(random.randrange(10))
print(random.randrange(1020))
print(random.randrange(10202))
cs


>> random 모듈을 import 한다.

>> randrange() 는 range() 와 같이 3가지의 종류의 파라미터를 갖는다.

>> 5 줄의 경우 [10, 20) 에서 2씩 증가하는 수들 중에서 난수가 발생함.



# placeholder


1
2
for _ in range(5):
    print(random.randrange(10), end=' ')
cs


>> for i in range(5): 라고 썼을 때, i 를 사용하지 않는다.

>> 하지만 i 를 지우면 에러.

>> 필요가 없는 변수로 의미를 약화시킬 수 있음. _ 로 표현 (placeholder)

>> _ 는 변수이지만 변수로 사용하지 않겠다는 의미..



# random.seed(1)


1
2
3
random.seed(1)
for _ in range(5):
    print(random.randrange(10), end=' ')    # 2 9 1 4 1
cs


>> 난수값을 고정시킬 수 있다.

>> seed 가 1이 되고 나서의 첫번째 숫자, 두번째 숫자 ...

>> 프로그램 처음에 딱 한번 호출해서 사용한다.



1
2
3
4
5
next = 1
def rand():
    global next
    next = next * 1103515245 + 12345
    return int(next // 65536) % 32768
cs


>> seed() 의 원리 같은 코드라고 한다.