배워서? 남줘라!

[Python] #6 if, while, for 본문

Computer languages/Python

[Python] #6 if, while, for

developing 2022. 10. 9. 01:56

 

 

if 조건문: 조건을 판단 후 상황에 맞게 처리.
while 조건문: 조건이 참일 때 반복. cf. 조건문에 0이 아닌 다른 수가 와도 참.
for 변수 in 리스트/튜플/문자열: 리스트/튜플/문자열이 담고 있는 모든 요소를 순서대로 변수에 대입.

 

if

#python6_if
#if_Q1: 주머니에 카드 있으면 걸어가고, 없으면 버스타기.

pocket = ['카드', '현금']

if '카드' in pocket:
    print('take a walk')
else:
    print('take a bus')


#if_Q2: 주머니에 카드 없으면 걸어가고, 있으면 버스타기.

pocket = ['카드', '현금']

if '카드' not in pocket:
    print('take a walk')
else:
    print('take a bus')

##심플 코드 (조건부)
print('take a walk') if '카드' not in pocket else print('take a bus')


#if_Q3: 주머니에 카드 없으면 걸어가고, a 있으면 버스타고가고, 이둘에 해당 안되면 아무것도 아님(결과값 없도록).
#else와 elif는 이전 조건문이 false 일 때 실행된다.

pocket = ['카드', '현금']
coupon = True
if '카드' not in pocket:
    print('take a walk')
elif coupon:
    print('take a bus')
else:
    pass

##심플 코드

pocket = ['카드', '현금']
coupon = True
if '카드' not in pocket: print('take a walk')
elif coupon: print('take a bus')
else: pass
take a walk
take a bus
take a bus
take a bus
take a bus

 

 

while

#python6_while

## while_Q1:1~20 중 3의 배수 제외 숫자 출력1 (break 이용)
number=0
while True:
    number=number+1
    if number % 3 != 0:
        print(number)
    if number>20:
        break                       #while에서 강제로 빠져나가기.

## while_Q2: 1~20 중 3의 배수 제외 숫자 출력2 (continue 이용)
number=0
while number<20:
    number=number+1
    if number % 3 ==0: continue     #여기서 while의 처음으로 돌아감
    print(number)

## while_Q3:무한 루프
while True:
    print("press ESC to stop")
1
2
4
5
7
8
1
2
4
5
7
8
press ESC to stop
press ESC to stop
press ESC to stop
press ESC to stop
press ESC to stop
;
;
;

 

## while_Q4: 자판기에 돈을 넣고 물(1000원) 구매하고 거스름돈 받기.
#다음 코드를 실행시키거나, 다음 코드가 담긴 .py 파일을 열면 금액을 입력할 수 있는 창이 뜬다.
#원하는 금액을 입력하고 enter를 누르면 그에 대한 출력값이 나온다.

water=25
while True:
    money=int(input("돈을 넣어주세요."))
    if money == 1000:
        print("here is water.")
        water = water-1
    elif money > 1000:
        print("here is water and your change is %d."% (money-1000))
        water = water-1
    else:
        print("take your money back")
        print("we have %d water left." % (water))
    if water == 0:
        print("no more water")
        break
돈을 넣어주세요.1200
here is water and your change is 200.
돈을 넣어주세요.빈칸

 

for

#python6_for

## for_Q1: 리스트 내에 있는 쌍 끼리 더하기
aa=[(3,5), (6,7)]
for (first, last) in aa:
    print(first+last)

## for_Q2: 점수로 합격 불합격 알려주기. 70점 이상이면 합격 축하하기.
score = [100, 50, 65, 85, 40]
student =0
for SCORE in score:
    student=student+1
    if SCORE <70:
        continue    
    print("%d번 참가자 합격입니다.축하합니다." % student)


## for_Q3: range와 for 이용해 1~100 모두 더하기
#range(10)는 0부터 9까지, range(3,10)은 3부터 9까지 숫자 나타냄.
number=0
for a in range(1,101):
    number = number+a
print(number)


## for_Q4: range와 for 이용해 구구단 만들기1

for a in range(2,10):
    for b in range(10):
        b=b+1
        print(a*b, end=" ")     #그 줄에 출력하도록
    print(" ")                  #다음 줄에 출력하도록


## for_Q4: range와 for 이용해 구구단 만들기2 (리스트 안에 for 사용)
times = [x*y for x in range(2,10) for y in range(1,10)]
print(times)
8
13
1번 참가자 합격입니다.축하합니다.
4번 참가자 합격입니다.축하합니다.
5050
2 4 6 8 10 12 14 16 18 20  
3 6 9 12 15 18 21 24 27 30  
4 8 12 16 20 24 28 32 36 40  
5 10 15 20 25 30 35 40 45 50  
6 12 18 24 30 36 42 48 54 60  
7 14 21 28 35 42 49 56 63 70  
8 16 24 32 40 48 56 64 72 80  
9 18 27 36 45 54 63 72 81 90  
[2, 4, 6, 8, 10, 12, 14, 16, 18, 3, 6, 9, 12, 15, 18, 21, 24, 27, 4, 8, 12, 16, 20, 24, 28, 32, 36, 5, 10, 15, 20, 25, 30, 35, 40, 45, 6, 12, 18, 24, 30, 36, 42, 48, 54, 7, 14, 21, 28, 35, 42, 49, 56, 63, 8, 16, 24, 32, 40, 48, 56, 64, 72, 9, 18, 27, 36, 45, 54, 63, 72, 81]

 

<참고>

박응용 저, Do it! 점프 투 파이썬, 이지스퍼블리싱, 2019

'Computer languages > Python' 카테고리의 다른 글

[Python] #8 Class  (0) 2022.10.15
[Python] #7 function  (0) 2022.10.14
[Python] #5 Bool & Variables  (0) 2022.10.07
[Python] #4 Dictionary & Set  (1) 2022.10.07
[Python] #3 List & Tuple  (1) 2022.10.06
Comments