Notice
Recent Posts
Recent Comments
ยซ   2024/12   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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
Archives
Today
In Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

A Joyful AI Research Journey๐ŸŒณ๐Ÿ˜Š

[41] 230224 Ch. 3 ๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด๋ฅผ ์œ„ํ•œ ํŒŒ์ด์ฌ ๊ธฐ์ดˆ ๋ฐฐ์šฐ๊ธฐ: 3. ํŒŒ์ด์ฌ ์ œ์–ด๋ฌธ, 4. ํŒŒ์ด์ฌ ํ•จ์ˆ˜ ์‚ฌ์šฉ๋ฒ• [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 41์ผ] ๋ณธ๋ฌธ

๐ŸŒณBootcamp Revision 2023โœจ/Python, Flask, SQL

[41] 230224 Ch. 3 ๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด๋ฅผ ์œ„ํ•œ ํŒŒ์ด์ฌ ๊ธฐ์ดˆ ๋ฐฐ์šฐ๊ธฐ: 3. ํŒŒ์ด์ฌ ์ œ์–ด๋ฌธ, 4. ํŒŒ์ด์ฌ ํ•จ์ˆ˜ ์‚ฌ์šฉ๋ฒ• [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 41์ผ]

yjyuwisely 2023. 2. 24. 12:48

230223 Fri 41st class

Ch. 3 ๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด๋ฅผ ์œ„ํ•œ ํŒŒ์ด์ฌ ๊ธฐ์ดˆ ๋ฐฐ์šฐ๊ธฐ
์ง„๋„: p. 93 ~ (๊ต์žฌ: IoT ์‚ฌ๋ฌผ์ธํ„ฐ๋„ท์„ ์œ„ํ•œ ๋ผ์ฆˆ๋ฒ ๋ฆฌํŒŒ์ด 4 ์ •์„, ์ €์ž: ์ตœ์ฃผํ˜ธ , ๊น€์žฌ๋ฒ” , ์ •๋™์ง„)
์ฐธ๊ณ  ๊ฐ€๋Šฅ ์ฑ…: ์™•์ดˆ๋ณด๋ฅผ ์œ„ํ•œ Python: ์‰ฝ๊ฒŒ ํ’€์–ด ์“ด ๊ธฐ์ดˆ ๋ฌธ๋ฒ•๊ณผ ์‹ค์Šต

๊ต์žฌ ์ฝ”๋“œ ๊นƒํ—ˆ๋ธŒ: https://github.com/codingspecialist/RaspberryPi4-Book-Example

๊ต์žฌ ์†Œ์Šค ๋ฐ PPT: http://jspstudy.co.kr/main/main.jsp
์ €์ž ์นดํŽ˜: https://cafe.naver.com/metacoding

 

IoT ์‚ฌ๋ฌผ์ธํ„ฐ๋„ท์„ ์œ„ํ•œ ๋ผ์ฆˆ๋ฒ ๋ฆฌํŒŒ์ด 4 ์ •์„ | ์ตœ์ฃผํ˜ธ - ๊ต๋ณด๋ฌธ๊ณ 

IoT ์‚ฌ๋ฌผ์ธํ„ฐ๋„ท์„ ์œ„ํ•œ ๋ผ์ฆˆ๋ฒ ๋ฆฌํŒŒ์ด 4 ์ •์„ | ใ€ŽIoT ์‚ฌ๋ฌผ์ธํ„ฐ๋„ท์„ ์œ„ํ•œ ๋ผ์ฆˆ๋ฒ ๋ฆฌํŒŒ์ด 4 ์ •์„ใ€์€ ์™•์ดˆ๋ณด๋„ ๋ผ์ฆˆ๋ฒ ๋ฆฌํŒŒ์ด๋ฅผ ์ด์šฉํ•ด ๋‹ค์–‘ํ•œ IoT๋ฅผ ์ง์ ‘ ๊ฐœ๋ฐœํ•  ์ˆ˜ ์žˆ๋„๋ก ์ „๊ธฐ์ „์ž ๊ธฐ์ดˆ ์›๋ฆฌ๋ถ€ํ„ฐ ์„ผ

product.kyobobook.co.kr

์˜ค๋Š˜ ๋ฐฐ์šด ๊ฒƒ ์ค‘ ๊ธฐ์–ตํ•  ๊ฒƒ์„ ์ •๋ฆฌํ–ˆ๋‹ค.


๋‚ด๊ฐ€ ๋ชฐ๋ž๋˜ ๊ฒƒ 

ํŒŒ์ด์ฌ์€ ์—”ํ„ฐ๊ฐ€ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ถ™๋Š”๋‹ค. 
ํŒŒ์ด์ฌ์˜ print = ์ž๋ฐ”์˜ System.print.outln()
end=""
 indicates that the end character has to be identified by whitespace and not a newline.

In computer science, control flow is the order in which individual statements, instructions or function calls of an imperative program are executed or evaluated.

In 
computer science, imperative programming is a programming paradigm of software that uses statements that change a program's state.

The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and stops before a specified number.

range(start, stop, step)

Parameter  Description
start Optional. An integer number specifying at which position to start. Default is 0
stop Required. An integer number specifying at which position to stop (not included).
step Optional. An integer number specifying the incrementation. Default is 1


def () ์ด์ „์— print๊ฐ€ ์žˆ์œผ๋ฉด ํ•จ์ˆ˜ ์ธ์ถœ print๋˜์–ด์„œ ๋‚˜์˜จ๋‹ค. 


Ch. 3 ๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด๋ฅผ ์œ„ํ•œ ํŒŒ์ด์ฌ ๊ธฐ์ดˆ ๋ฐฐ์šฐ๊ธฐ

์ฑ…์˜ ๋ชฉ์ฐจ
01 _ ํŒŒ์ด์ฌ ์‹œ์ž‘ํ•˜๊ธฐ
01-1 ํŒŒ์ด์ฌ ๊ฐœ์š”
01-2 ํŒŒ์ด์ฌ Windows ์„ค์น˜ํ•˜๊ธฐ
01-3 ํŒŒ์ด์ฌ์˜ ์ฃผ์„

02 _ ํŒŒ์ด์ฌ ์ž๋ฃŒํ˜•
02-1 ์ˆซ์žํ˜•
02-2 ๋ฌธ์žํ˜•
02-3 ๋ฆฌ์ŠคํŠธ
02-4 ํŠœํ”Œ
02-5 ๋”•์…”๋„ˆ๋ฆฌ

03 _ ํŒŒ์ด์ฌ ์ œ์–ด๋ฌธ

03-1 if๋ฌธ
03-2 for๋ฌธ
03-3 whie๋ฌธ
03-4 break๋ฌธ

04 _ ํŒŒ์ด์ฌ ํ•จ์ˆ˜ ์‚ฌ์šฉ๋ฒ•

04-1 ํ•จ์ˆ˜ ์„ ์–ธํ•˜๊ธฐ
04-2 ํ•จ์ˆ˜ ์ธ์ˆ˜ ์‚ฌ์šฉํ•˜๊ธฐ

05 _ ํŒŒ์ด์ฌ ํด๋ž˜์Šค์™€ ๋ชจ๋“ˆ

05-1 ํด๋ž˜์Šค
05-2 ๋ชจ๋“ˆ
05-3 ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

3 ํŒŒ์ด์ฌ ์ œ์–ด๋ฌธ (Control flow) p. 95

์ œ์–ด๋ฌธ์€ ํ”„๋กœ๊ทธ๋žจ์˜ ์‹คํ–‰์—ฌ๋ถ€, ์ˆœ์„œ, ๋ฐฉ์‹์„ ์ œ์–ดํ•˜๋Š” ๋ฌธ์žฅ์ด๋‹ค.

3.1 if๋ฌธ

"๋งŒ์•ฝ ์ง€์ •ํ•œ ์กฐ๊ฑด์— ๋งž์•„ ๋–จ์–ด์ง€๋ฉด ์ง€์ •ํ•œ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ผ"์˜ ๋œป์ด๋‹ค.
ํŒŒ์ด์ฌ์—์„œ๋Š” ๋“ค์—ฌ์“ฐ๊ธฐ๊ฐ€ ์ค‘์š”ํ•˜๋‹ค. 

์˜ˆ์ œ) if๋ฌธ, else๋ฌธ, elif๋ฌธ ์„ค๋ช…์„ ์œ„ํ•œ ์˜ˆ์ œ 

a = 23
if a < 50:
    print('50๋ณด๋‹ค ์ž‘๊ตฐ์š”')
#if else๋ฌธ
if a < 20:
    print('20๋ณด๋‹ค ์ž‘๊ตฐ์š”')
else: 
    print('20๋ณด๋‹ค ํฌ๊ตฐ์š”')
#elif๋ฌธ
age = int(input('ํ˜„์žฌ ๋‚˜์ด๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค.'))
if age < 10:
    print('์œ ๋…„์ธต ์ž…๋‹ˆ๋‹ค.')
elif age < 20:
    print('10๋Œ€์ž…๋‹ˆ๋‹ค.')
elif age < 30:
    print('20๋Œ€์ž…๋‹ˆ๋‹ค.')
elif age < 40:
    print('30๋Œ€์ž…๋‹ˆ๋‹ค.')
else:
    print('์žฅ๋…„์ธต์ž…๋‹ˆ๋‹ค')

 

์•„๋ž˜๋Š” ์œ„์™€ ๊ฐ™์€ ์ž๋ฐ”์˜ ์ฝ”๋“œ์ด๋‹ค.

class If01{
		public static void main(String[] args){
        		int a = 23;
                if (a < 50){
                			System.out.println("50๋ณด๋‹ค ์ž‘๊ตฐ์š”");
                }
                if(a < 20){
                			System.out.println("20๋ณด๋‹ค ์ž‘๊ตฐ์š”");
                }else{
                			System.out.println("50๋ณด๋‹ค ์ž‘๊ตฐ์š”");
                }
        }
        
        Scanner scanner = new Scanner(System.in);
        int age = Integer.parseInt(scanner.next())
        if(age < 10){
        			System.out.println("์œ ๋…„์ธต ์ž…๋‹ˆ๋‹ค.");
        }else if(age < 20){
        			System.out.println("10๋Œ€ ์ž…๋‹ˆ๋‹ค.");
        }else if(age < 30){
        			System.out.println("20๋Œ€ ์ž…๋‹ˆ๋‹ค.");
        }else if(age < 40){
        			System.out.println("30๋Œ€ ์ž…๋‹ˆ๋‹ค.");
        }else{
        			System.out.println("์žฅ๋…„์ธต์ž…๋‹ˆ๋‹ค.");
		}
    }
}

๊ฒฐ๊ณผ) 

50๋ณด๋‹ค ์ž‘๊ตฐ์š”
20๋ณด๋‹ค ํฌ๊ตฐ์š”
ํ˜„์žฌ ๋‚˜์ด๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค.23
20๋Œ€์ž…๋‹ˆ๋‹ค.

3.2 for๋ฌธ

for๋ฌธ์€ ํŠน์ • ์ฝ”๋“œ๋ฅผ ์ง€์ •ํ•œ ํšŸ์ˆ˜๋งŒํผ ๋ฐ˜๋ณตํ•˜๋Š” ๋ฌธ์žฅ์ด๋‹ค. 
๋ฐ˜๋ณตํ•˜๋Š” ๊ธฐ์ค€์€ ๋ณ€์ˆ˜์˜ ํฌ๊ธฐ๋‚˜ ์ˆซ์ž, ๋ฌธ์ž์—ด์˜ ๊ธธ์ด ๋“ฑ ๋‹ค์–‘ํ•œ ๊ธฐ์ค€์ด ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋‹ค. 

์˜ˆ์ œ 1) for๋ฌธ์„ ๋‹ค์–‘ํ•œ ํ˜•ํƒœ๋กœ ์„ ์–ธํ•œ ์˜ˆ์ œ 

# 0 ~ 4 ์ถœ๋ ฅ
# for(int i = 0; i < 5; i++)
#             ์ดˆ๊ธฐํ™” ์กฐ๊ฑด์‹ ์ฆ๊ฐ๊ฐ’
for i in range(0, 5, 1): #for๋ฌธ ์„ ์–ธ ๋ณ€์ˆ˜ i์˜ ์ดˆ๊ธฐ๊ฐ’์€ 0์ด๊ณ  ๋ฒ”์œ„๋Š” 5์ด๊ณ  1์”ฉ ์ฆ๊ฐ€
    print(i) #for๋ฌธ ์กฐ๊ฑด์ด ๋งž์œผ๋ฉด ๋ฐ˜๋ณต๋˜๋Š” ์ฝ”๋“œ
print("--------------------")

#๋Œ€๊ด„ํ˜ธ: ๋ฆฌ์ŠคํŠธ 
for j in[1, 3, 5, 7, 9]: #for๋ฌธ ์ฒ˜๋ฆฌํ•  ๋ชฉ๋ก์—๋Š” ๋ฆฌ์ŠคํŠธ ๋ณ€์ˆ˜ ์„ ์–ธ
    print(j)
print("--------------------")
for k in range(0, 3, 1):
    print("๊ฟˆ์€ ์ด๋ฃจ์–ด ์ง„๋‹ค.")

์•„๋ž˜๋Š” ์ž๋ฐ”์˜ ์ฝ”๋“œ์ด๋‹ค.

class For01{
	public static void main(String[] args){
    		for(int i = 0; i < 5; i++){
            		System.out.println(i);
            }
            
            int[] odd = {1,3,5,7,9}
            for (int j : odd){
            		System.out.println(j);
            }
     }
}

๊ฒฐ๊ณผ)

0
1
2
3
4
--------------------
1
3
5
7
9
--------------------
๊ฟˆ์€ ์ด๋ฃจ์–ด ์ง„๋‹ค.
๊ฟˆ์€ ์ด๋ฃจ์–ด ์ง„๋‹ค.
๊ฟˆ์€ ์ด๋ฃจ์–ด ์ง„๋‹ค.

์˜ˆ์ œ 2) for๋ฌธ์„ ์ด์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์งˆ๋ฌธ์— ๋Œ€ํ•œ ๋‹ต๋ณ€์„ ๊ตฌํ•œ๋‹ค.
ํŒŒ์ด์ฌ์€ ์—”ํ„ฐ๊ฐ€ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ถ™๋Š”๋‹ค. 
ํŒŒ์ด์ฌ์˜ print = ์ž๋ฐ”์˜ System.print.outln()

end=""
indicates that the end character has to be identified by whitespace and not a newline.

#for๋ฌธ์„ ์ด์šฉํ•˜์—ฌ 1์—์„œ 10๊นŒ์ง€์˜ ํ•ฉ ๊ตฌํ•˜๊ธฐ
sum = 0
for i in range(1, 11, 1):
    sum+=i
print("sum : %d" % sum)
print("-----------------")

#for๋ฌธ์„ ์ด์šฉํ•˜์—ฌ  1์—์„œ 10๊นŒ์ง€ ์‹๊ณผ ํ•ฉ ๊ตฌํ•˜๊ธฐ
sum = 0
for j in range(1, 11, 1):
    if j<10:
        print("%d + " % j, end="")
    elif j==10:
        print("%d = " % j, end="")
    sum+=j
print("%d" % sum)

์ž๋ฐ”์˜ ์ฝ”๋“œ๋Š” ์•„๋ž˜๊ณผ ๊ฐ™๋‹ค.
The Java printf function helps simplify the task of printing formatted output to the console, terminal window or log files.

class For02{
		public static void main(String[] args){
        		int sum1 = 0;
                for(int i = 1; i < 11; i++){
                		sum1 += i //sum1 = sum1 + i
                }
                System.out.printf("sum1: %d", sum1)
	}
    			int sum2 = 0;
                for(int i = 1; i < 11; i++){
                		//j๊ฐ’์ด 1~9๊นŒ์ง€ "+"๋ฌธ์ž์—ด์„ ์—ฐ๊ฒฐํ•˜๊ณ ,
                        if(j < 10){
                        	System.out.printf("%d + ", j)
                        }else if(j==10){
                        	System.out.printf("%d = ", j)
                        }
                        //j๊ฐ’์ด 10์ด๋ฉด "=" ๋ฌธ์ž์—ด์„ ์—ฐ๊ฒฐ
                        sum2 += i //sum2 = sum2 + i
                }
                System.out.printf("sum2: %d", sum2)
	}
}

๊ฒฐ๊ณผ)

sum : 55
-----------------
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55

3.3 while๋ฌธ

์กฐ๊ฑด๋ถ€ ๋ฐ˜๋ณต๋ฌธ์ด๋‹ค. if๋ฌธ์˜ ๋ฐ˜๋ณต๋ฌธ ํ˜•ํƒœ์ด๋‹ค. ์กฐ๊ฑด์„ ์ง€์ •ํ•˜๊ณ  ์กฐ๊ฑด์ด true์ผ ๋•Œ ์‹คํ–‰๋˜๋ฉฐ, ์กฐ๊ฑด์ด false๊ฐ€ ๋  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค. ์กฐ๊ฑด์ด false๊ฐ€ ๋˜์ง€ ์•Š์œผ๋ฉด ๋ฌดํ•œ๋ฃจํ”„(๋ฌดํ•œ๋ฐ˜๋ณต)์ด ๋ฐœ์ƒํ•œ๋‹ค.

ํŒŒ์ด์ฌ์—์„œ ์กฐ๊ฑด์ด ์—†๋Š” ๋ฐ˜๋ณต๋ฌธ์€ for๋ฌธ
์กฐ๊ฑด์ด ์žˆ๋Š” ๋ฐ˜๋ณต๋ฌธ์€ while๋ฌธ์ด๋‹ค. 

์ž๋ฐ”: for, while, do-while

์˜ˆ์ œ)

str = "๊ฟˆ์€ ์ด๋ฃจ์–ด ์ง„๋‹ค"
i = 0
while i < 3:
    print(str)
    i = i + 1
print("-------------------------------")
#while๋ฌธ์œผ๋กœ ์ž…๋ ฅํ•œ ์ˆซ์ž๋งŒํผ str์„ ๋ฐ˜๋ณต ์ถœ๋ ฅํ•˜์‹œ์˜ค.
i = int(input("๋ฐ˜๋ณต ํšŸ์ˆ˜ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค.")) #์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๊ฐ’์„ ์ •์ˆ˜๋กœ ๋ฆฌํ„ด
j = 1
flag = True
while flag:
    j = j + 1 #j๊ฐ’์„ 1์”ฉ ์ฆ๊ฐ€
    if i < j: #๋งŒ์•ฝ j๊ฐ€ i๋ณด๋‹ค ํฌ๋‹ค๋Š” ์กฐ๊ฑด์˜ if๋ฌธ ์„ ์–ธ
        flag = False #flag ๋ณ€์ˆ˜์— False๊ฐ’ ๋Œ€์ž…
    print(str)

์œ„์˜ ์ฝ”๋“œ๋Š” ์•„๋ž˜์˜ ์ž๋ฐ” ์ฝ”๋“œ์™€ ๊ฐ™๋‹ค.

import java.util.Scanner;
public class While01 {
		public static void main(String[] args){
        		String str = "๊ฟˆ์€ ์ด๋ฃจ์–ด ์ง„๋‹ค";
                int i = 0;
                while (i < 3){
                		System.out.println(str);
                        i = i + 1;
              
                System.out.println("---------------------");
                
                Scanner scanner = new Scanner(System.in);
                i = Integer.parseInt(scanner.next());
                int j = 1;
                boolean flag = true;
                while flag{
                		j = j + 1;
                        if (i < j){
                        		flag = false;
                        }
                        System.out.println(str);
                }
                }
}
}

๊ฒฐ๊ณผ)

๊ฟˆ์€ ์ด๋ฃจ์–ด ์ง„๋‹ค
๊ฟˆ์€ ์ด๋ฃจ์–ด ์ง„๋‹ค
๊ฟˆ์€ ์ด๋ฃจ์–ด ์ง„๋‹ค
-------------------------------
๋ฐ˜๋ณต ํšŸ์ˆ˜ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค.5
๊ฟˆ์€ ์ด๋ฃจ์–ด ์ง„๋‹ค
๊ฟˆ์€ ์ด๋ฃจ์–ด ์ง„๋‹ค
๊ฟˆ์€ ์ด๋ฃจ์–ด ์ง„๋‹ค
๊ฟˆ์€ ์ด๋ฃจ์–ด ์ง„๋‹ค
๊ฟˆ์€ ์ด๋ฃจ์–ด ์ง„๋‹ค

3.4 break๋ฌธ

break๋ฌธ์€ ๋ฐ˜๋ณต๋ฌธ์„ ์ข…๋ฃŒ์‹œํ‚จ๋‹ค. ๋ฐ˜๋ณต๋ฌธ์— break๋ฅผ ๊ธฐ์ˆ ํ•  ๊ฒฝ์šฐ ํ•ด๋‹น ๋ฐ˜๋ณต๋ฌธ์˜ ์ฝ”๋“œ๋Š” ์‹คํ–‰๋˜์ง€ ์•Š๊ณ  ๋ฐ˜๋ณต๋ฌธ์„ ๋น ์ ธ ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค. 
range(1, 21, 1): ์ดˆ๊ธฐ๊ฐ’ 1,  20๊นŒ์ง€, ์ฆ๊ฐ๊ฐ’: 1


์˜ˆ์ œ)

#for๋ฌธ๊ณผ break๋ฌธ์„ ์ด์šฉํ•˜์—ฌ 1์—์„œ 20๊นŒ์ง€ ํ•ฉ์ด 100๋ณด๋‹ค ๊ฐ€์žฅ ๊ฐ€๊น๊ณ  ์ž‘์€ ํ•ฉ์„ ๊ตฌํ•œ๋‹ค.
sum, i = 0, 0 #sum๊ณผ i์˜ ๋ณ€์ˆ˜๋ฅผ ๊ฐ๊ฐ ์„ ์–ธ
for i in range(1, 21, 1): #์ดˆ๊ธฐ๊ฐ’์ด 1์ด๊ณ  ์ฆ๊ฐ๊ฐ’์ด 1์ด๋ฉด 20๊นŒ์ง€ ๋ฐ˜๋ณต
    sum+=i #sum = sum + 1
    if sum > 100:
        break; #break ๋’ค์— ;์„ ์‚ฌ์šฉ 
sum-=i #ํ˜„์žฌ์˜ i๊ฐ’์„ ๋งˆ์ด๋„ˆ์Šค
print("%d" % sum)
print("----------------------")
#while๋ฌธ๊ณผ break๋ฌธ์„ ์ด์šฉํ•˜์—ฌ ์ž…๋ ฅํ•œ 1์—์„œ ์ˆซ์ž๋งŒํผ ํ•ฉ์„ ๊ตฌํ•˜์‹œ์˜ค.
sum, i = 0, 0
j = int(input("์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ")) #์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๊ฐ’์„ ์ •์ˆ˜๋กœ ๋ฆฌํ„ด
while True:
    if i<j:
        i = i+1
        sum+=i;
    elif i==j:
        break;
print("1์—์„œ %d๊นŒ์ง€์˜ ํ•ฉ์€ %d์ž…๋‹ˆ๋‹ค." % (j, sum))

๊ฒฐ๊ณผ)

91
----------------------
์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. 20
1์—์„œ 20๊นŒ์ง€์˜ ํ•ฉ์€ 210์ž…๋‹ˆ๋‹ค.

๋ฌธ์ œ ํ’€์–ด๋ณด๊ธฐ)

Q1. ์•„๋ž˜ ์ฝ”๋“œ์˜ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ์˜ˆ์ƒํ•˜๋ผ

if True:
	print("1") #True์ผ ๊ฒฝ์šฐ ์‹คํ–‰ 
	print("2") #True์ผ ๊ฒฝ์šฐ ์‹คํ–‰ 
else:
	print("3") #False์ผ ๊ฒฝ์šฐ ์‹คํ–‰
print("4") #True์ผ ๊ฒฝ์šฐ ์‹คํ–‰ (๋“ค์—ฌ์“ฐ๊ธฐ ๋•Œ๋ฌธ์— if๋ฌธ๊ณผ ์ƒ๊ด€์—†์œผ๋ฏ€๋กœ)
# == ๊ฒฐ๊ณผ ==

True์ผ ๊ฒฝ์šฐ 1,2,4 ์ถœ๋ ฅ

if True:
	if False:
    	print("1")
        print("2")
    else:
    	print("3")
else:
	print("4")
print("5")
# == ๊ฒฐ๊ณผ ==

True์ผ ๊ฒฝ์šฐ 3, 5 ์ถœ๋ ฅ


Q2. ์ ์ˆ˜ ๊ตฌ๊ฐ„์— ํ•ด๋‹นํ•˜๋Š” ํ•™์ ์ด ์•„๋ž˜์™€ ๊ฐ™์ด ์ •์˜๋˜์—ˆ๋‹ค. ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ score์„ ์ž…๋ ฅ๋ฐ›์•„ ํ•™์ ์„ ์ถœ๋ ฅํ•˜๋ผ.

์ ์ˆ˜ ํ•™์ 
81~100 A
61~80 B
41~60 C
21~40 D
0~20 E
score: 83
grade is A

์ฝ”๋“œ)

score = int(input("ํ•™์ ์„ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค."))
if (81 <= score <= 100):
    print("grade is A.")
elif (61 <= score <= 80):
    print("grade is B.")
elif (41 <= score <= 60):
    print("grade is C.")
elif (21 <= score <= 40):
    print("grade is D.")
else:
    print("grade is E.")


๊ฒฐ๊ณผ)

ํ•™์ ์„ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค.83
grade is A.

Q3. for๋ฌธ์˜ ์‹คํ–‰๊ฒฐ๊ณผ๋ฅผ ์˜ˆ์ธกํ•˜๋ผ.

fruits = ["์‚ฌ๊ณผ", "๊ทค", "์ˆ˜๋ฐ•"]
for fruit in fruits:
	print(fruit)

๊ฒฐ๊ณผ)

์‚ฌ๊ณผ
๊ทค
์ˆ˜๋ฐ•


์ฝ”๋“œ2)

fruits = ["์‚ฌ๊ณผ", "๊ทค", "์ˆ˜๋ฐ•"]
for fruit in fruits:
	print(fruit, end=" ")

๊ฒฐ๊ณผ)

์‚ฌ๊ณผ ๊ทค ์ˆ˜๋ฐ•

Q4. for๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ ๋ฆฌ์ŠคํŠธ์— ์ €์žฅ๋œ ๊ฐ’์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ถœ๋ ฅํ•˜๋ผ.
menu = ["๊น€๋ฐฅ", "๋ผ๋ฉด", "ํŠ€๊น€"]
์˜ค๋Š˜์˜ ๋ฉ”๋‰ด: ๊น€๋ฐฅ
์˜ค๋Š˜์˜ ๋ฉ”๋‰ด: ๋ผ๋ฉด
์˜ค๋Š˜์˜ ๋ฉ”๋‰ด: ํŠ€๊น€

์ฝ”๋“œ)

menu = ["๊น€๋ฐฅ", "๋ผ๋ฉด", "ํŠ€๊น€"]

for i in range(0, 3, 1):
    print("์˜ค๋Š˜์˜ ๋ฉ”๋‰ด: %s" %menu[i])


๊ฒฐ๊ณผ)

์˜ค๋Š˜์˜ ๋ฉ”๋‰ด: ๊น€๋ฐฅ
์˜ค๋Š˜์˜ ๋ฉ”๋‰ด: ๋ผ๋ฉด
์˜ค๋Š˜์˜ ๋ฉ”๋‰ด: ํŠ€๊น€

๊ฐ•์‚ฌ๋‹˜์˜ ์ฝ”๋“œ) ๋” ๊ฐ„๋‹จํ•˜๋‹ค! :0

menu = ["๊น€๋ฐฅ", "๋ผ๋ฉด", "ํŠ€๊น€"]
for i in menu:
	print("์˜ค๋Š˜์˜ ๋ฉ”๋‰ด: " + i)

๊ฒฐ๊ณผ)

์˜ค๋Š˜์˜ ๋ฉ”๋‰ด: ๊น€๋ฐฅ
์˜ค๋Š˜์˜ ๋ฉ”๋‰ด: ๋ผ๋ฉด
์˜ค๋Š˜์˜ ๋ฉ”๋‰ด: ํŠ€๊น€

Q5. ๋ฆฌ์ŠคํŠธ์—๋Š” ๋„ค ๊ฐœ์˜ ์ •์ˆ˜๊ฐ€ ์ €์žฅ๋ผ ์žˆ๋‹ค.
number = [3, -20, -3, 44]
for๋ฌธ๊ณผ if๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ ๋ฆฌ์ŠคํŠธ์˜ ์Œ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋ผ.

-20
-3

์ฝ”๋“œ)

number = [3, -20, -3, 44]

for i in range(0, 4, 1):
    if(number[i] < 0):
        print(number[i])

๊ฒฐ๊ณผ)

-20
-3

4 ํŒŒ์ด์ฌ ํ•จ์ˆ˜ ์‚ฌ์šฉ๋ฒ• p. 99

ํ•จ์ˆ˜๋Š” ์ˆœ์ฐจ์ ์œผ๋กœ ์ง„ํ–‰๋˜๋Š” ํ”„๋กœ๊ทธ๋žจ์—์„œ ์ผ์ข…์˜ ์ค‘๊ฐ„ ํ”„๋กœ๊ทธ๋žจ ๊ฐ™์€ ์—ญํ• ์„ ํ•œ๋‹ค. ๋Œ€๋ถ€๋ถ„์ด ์ž…๋ ฅ ๊ฐ’์— ๋Œ€ํ•œ ์–ด๋– ํ•œ ์ฒ˜๋ฆฌ๋ฅผ ์ง„ํ–‰ํ•˜๊ณ  ๊ฒฐ๊ณผ ๊ฐ’์„ ์ œ๊ณตํ•œ๋‹ค. 
ํŒŒ์ด์ฌ์€ ๋‚ด์žฅ ํ•จ์ˆ˜๋กœ๋„ ์ œ๊ณต์ด ๋˜๊ณ  (ex. print)
๋˜ ์‚ฌ์šฉ์ž๊ฐ€ ํ•จ์ˆ˜๋ฅผ ์„ ์–ธํ•ด์„œ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๋‹ค.

4.1 ํ•จ์ˆ˜ ์„ ์–ธํ•˜๊ธฐ

def ํ•จ์ˆ˜๋ช…(๋งค๊ฐœ๋ณ€์ˆ˜1, ...):
    ์‹คํ–‰ํ•  ์ฝ”๋“œ1
    ์‹คํ–‰ํ•  ์ฝ”๋“œ2
             โฆ™

def mydef()
def: ์„ ์–ธ ํ‚ค์›Œ๋“œ
mydef: ํ•จ์ˆ˜๋ช…
(): ๋งค๊ฐœ ๋ณ€์ˆ˜

์ด๋ ‡๊ฒŒ ์„ ์–ธ๋œ ํ•จ์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค.

ํ•จ์ˆ˜๋ช…(์ธ์ˆ˜, ...)

 

์˜ˆ์ œ) ๊ธฐ๋ณธ์ ์ธ ํ•จ์ˆ˜๋ฅผ ์„ ์–ธํ•˜๊ณ  ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค. 
mydef01(), mydef02()๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์ด๋‹ค.
() ์•ž์— print๊ฐ€ ์žˆ์œผ๋ฉด ํ•จ์ˆ˜ ์ธ์ถœ print๋˜์–ด์„œ ๋‚˜์˜จ๋‹ค. 

def mydef01(): #์ธ์ˆ˜ ์—†๋Š” ํ•จ์ˆ˜ ์„ ์–ธ
    print("ํ•จ์ˆ˜๋ฅผ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค.") #๋“ค์—ฌ์“ฐ๊ธฐ๋ฅผ ํ†ตํ•ด ํ•จ์ˆ˜ ๊ตฌํ˜„ ๋ถ€๋ถ„
mydef01() #ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœ: ํ•จ์ˆ˜๋ฅผ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค.

def mydef02(str="์ธ์ˆ˜ํ•จ์ˆ˜๋ฅผ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค."): #์ธ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜ ์„ ์–ธ, str๋Š” ์ธ์ˆ˜๋กœ์จ ๊ธฐ๋ณธ ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ฌธ์ž์—ด ๋ณ€์ˆ˜์ž…๋‹ˆ๋‹ค.
    print(str) #์ธ์ˆ˜๋กœ ๋ฐ›์€ ๋ณ€์ˆ˜๋ฅผ ์ถœ๋ ฅ
mydef02() #ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœ: ์ธ์ˆ˜ํ•จ์ˆ˜๋ฅผ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค.
mydef02("์ธ์ˆ˜๋ฅผ ๋„ฃ์Šต๋‹ˆ๋‹ค.") #๋ฌธ์ž์—ด ์ธ์ˆ˜๋ฅผ ๋„ฃ์–ด์„œ mydef02 ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ–ˆ๋‹ค. ์ธ์ˆ˜๋Š” str ๋ณ€์ˆ˜์— ๋Œ€์ž…๋œ๋‹ค.:์ธ์ˆ˜๋ฅผ ๋„ฃ์Šต๋‹ˆ๋‹ค.


๊ฒฐ๊ณผ) 
str๋Š” ์ธ์ˆ˜๋กœ์จ ๊ธฐ๋ณธ ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ฌธ์ž์—ด ๋ณ€์ˆ˜์ด๋‹ค.

mydef01(): ํ•จ์ˆ˜๋ฅผ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค.
mydef02(): ์ธ์ˆ˜ํ•จ์ˆ˜๋ฅผ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค.
mydef02("์ธ์ˆ˜๋ฅผ ๋„ฃ์Šต๋‹ˆ๋‹ค."): ์ธ์ˆ˜๋ฅผ ๋„ฃ์Šต๋‹ˆ๋‹ค.

ํ•จ์ˆ˜๋ฅผ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค.
์ธ์ˆ˜ํ•จ์ˆ˜๋ฅผ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค.
์ธ์ˆ˜๋ฅผ ๋„ฃ์Šต๋‹ˆ๋‹ค.

 

์˜ˆ์ œ 2) ์ฐธ๊ณ : https://www.w3schools.com/python/python_functions.asp

def my_function():
  print("Hello from a function")

my_function()

๊ฒฐ๊ณผ)

Hello from a function

4.2 ํ•จ์ˆ˜ ์ธ์ˆ˜ ์‚ฌ์šฉํ•˜๊ธฐ

์˜ˆ์ œ) ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ธ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์•„์„œ ๊ณ„์‚ฐ์„ ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์„ ์–ธํ•œ๋‹ค.

#mydef01 ํ•จ์ˆ˜ ์„ ์–ธ(์ธ์ˆ˜๊ฐ€ ์—†๋Š” ํ•จ์ˆ˜ ์„ ์–ธ)
def mydef01():
    i = 10
    j = 20
    print(i+j)
    
#mydef01 ํ•จ์ˆ˜ ํ˜ธ์ถœ    
mydef01()

#mydef02 ํ•จ์ˆ˜ ์„ ์–ธ(์ธ์ˆ˜๊ฐ€ ๋‘ ๊ฐœ ์žˆ๋Š” ํ•จ์ˆ˜ ์„ ์–ธ)
def mydef02(i, j):
    print(i+j)

#mydef02 ํ•จ์ˆ˜ ํ˜ธ์ถœ
mydef02(10, 20)

#๊ณ„์‚ฐํ•  ์ˆซ์ž๋ฅผ ๋‘ ๊ฐœ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. 
def mydef03(i, j, p): #์„ธ ๊ฐœ์˜ ์ธ์ˆ˜๋ฅผ ๋ฐ›๋Š” ํ•จ์ˆ˜ ์„ ์–ธ
    if p == '+':
        print(i + j)
    elif p == '-':
        print(i - j)
    elif p == '*':
        print(i*j)
    elif p == '/':
        print(i/j)
n = int(input("์ฒซ ๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค.")) #์‚ฌ์šฉ์ž๊ฐ€ ์ฒซ ๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅ
m = int(input("๋‘ ๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค.")) #์‚ฌ์šฉ์ž๊ฐ€ ๋‘ ๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅ
p = input("์—ฐ์‚ฐ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”(+, -, *, /)") #์‚ฌ์šฉ์ž๊ฐ€ ์—ฐ์‚ฐ์ž๋ฅผ ์ž…๋ ฅ
mydef03(n, m, p) #์ฒซ ๋ฒˆ์งธ, ๋‘ ๋ฒˆ์งธ, ์—ฐ์‚ฐ์ž๋ฅผ ์ž…๋ ฅ ๋ฐ›์•„์„œ ํ•จ์ˆ˜ ํ˜ธ์ถœ

๊ฒฐ๊ณผ)
int()๋Š” String์„ Integer๋กœ ๋ฐ”๊พผ๋‹ค. 
The int() function converts a number or a string to its equivalent integer.

30
30
์ฒซ ๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค.10
๋‘ ๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค.20
์—ฐ์‚ฐ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”(+, -, *, /)*
200

 

 


๋ฌธ์ œ ํ’€์–ด๋ณด๊ธฐ)
Q1. ์•„๋ž˜์˜ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ์ด์œ ์— ๋Œ€ํ•ด ์„ค๋ช…ํ•˜๋ผ

hello()
def hello():
	print("Hi")

"hello" is not defined
์ด์œ : ์„ ์–ธ ์ „์— ๋จผ์ € ํ˜ธ์ถœ๋˜์—ˆ๋‹ค.

์ˆ˜์ •๋œ ์ฝ”๋“œ)

def hello():
	print("Hi")   
hello()

๊ฒฐ๊ณผ)

Hi

Q2. ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์˜ˆ์ƒํ•˜๋ผ

์ฝ”๋“œ 1)

def message():
	print("A")
    print("B")
    
message()
print("C")
message()

๊ฒฐ๊ณผ)

A
B
C
A
B

์ฝ”๋“œ 2)
range(start, stop, step)

def message1():
	print("A")

def message2():
	print("B")

def message3():
	for i in range(3):
	    message2()
print("C")
message1()
    
message3()

๊ฒฐ๊ณผ)

C
A
B
B
B

Q3. ๋ฆฌ์ŠคํŠธ์—๋Š” ๋„ค ๊ฐœ์˜ ์ •์ˆ˜๊ฐ€ ์ €์žฅ๋ผ ์žˆ๋‹ค.

number = [3, -20, -3, 44]
for๋ฌธ, if๋ฌธ๊ณผ negative()ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด์„œ ๋ฆฌ์ŠคํŠธ์˜ ์Œ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋ผ.

-20
-3

์ฝ”๋“œ)

number = [3, -20, -3, 44]
for i in number:
    if i < 0:
        print(i)

๊ฒฐ๊ณผ)

-20
-3

์ฝ”๋“œ 2) negative() ์ด์šฉ

number = [3, -20, -3, 44]
def negative():
    for i in number:
        if i<0:
            print(i)

negative()

๊ฒฐ๊ณผ)

-20
-3

 

728x90
๋ฐ˜์‘ํ˜•

'๐ŸŒณBootcamp Revision 2023โœจ > Python, Flask, SQL' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[48] 230308 Ch. 6 Flask๋ฅผ ํ™œ์šฉํ•œ ์„ผ์„œ ์ œ์–ด: 5. Flask ์›นํŽ˜์ด์ง€, 6. IP, Port, Process ๊ฐœ๋… ์žก๊ธฐ, 7. cron๊ณผ daemon ๊ฐœ๋… ์žก๊ธฐ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 48์ผ]  (1) 2023.03.08
[47] 230307 Ch. 6 Flask๋ฅผ ํ™œ์šฉํ•œ ์„ผ์„œ ์ œ์–ด: 1. Flask ์„ธํŒ…ํ•˜๊ธฐ, 2. Flask ์›น์„œ๋ฒ„ ๊ตฌ์ถ•ํ•˜๊ธฐ, 3. Flask ๋ผ์šฐํŒ…, 4. Flask LED ์ œ์–ดํ•˜๊ธฐ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 47์ผ]  (0) 2023.03.07
[42] 230227 Ch. 3 ๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด๋ฅผ ์œ„ํ•œ ํŒŒ์ด์ฌ ๊ธฐ์ดˆ ๋ฐฐ์šฐ๊ธฐ: 5. ํŒŒ์ด์ฌ ํด๋ž˜์Šค์™€ ๋ชจ๋“ˆ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 42์ผ]  (0) 2023.02.27
[40] 230223 Ch. 3 ๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด๋ฅผ ์œ„ํ•œ ํŒŒ์ด์ฌ ๊ธฐ์ดˆ ๋ฐฐ์šฐ๊ธฐ: 1. ํŒŒ์ด์ฌ ์‹œ์ž‘ํ•˜๊ธฐ, 2. ํŒŒ์ด์ฌ ์ž๋ฃŒํ˜• [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 40์ผ]  (1) 2023.02.23
Comments