디필의 요모조모

파이썬 for Beginner 4장 연습문제 본문

Programming Language/Python

파이썬 for Beginner 4장 연습문제

Diphylleia12 2019. 12. 7. 22:40

1. 산술 연산자가 아닌 것을 모두 고르시오.
    
+, -, *, ^, ~, /, //, %, **, &, ***, ?

2. 다음 계산식의 결과를 예측하시오.
    a, b = 10, 20

    ① a + b                                30
    ② a - b                                 -10
    ③ a % b                               10
    ④ a // b                                0

3. 다음 계산식의 결과를 예측하시오.
    a, b, c = 1, 2, 3

    ① a + b % c                          3
    ② a * b - c                            -1
    ③ a / b * c                            1.5

4. 문자열을 숫자로 변환한 후 계산하는 식이다. 오류가 발생하는 것을 고르고, 오류가 발생하지 않도록 수정하시오.
    s1, s2, s3 = "111", "111.11", "99999999999999"

    ① int(s1) + 111.11
    ② int(s2) + 111.11                  int -> float으로 변환
    ③ int(s3) + 111.11

5. 대입 연산자의 활용이다. 결과를 예측하시오.
    ① a, b = 10, 20; a += b; print(a)    30
    ② a, b = 10, 20; a %= b; print(a)   10 
    ③ a, b = 10, 20; a //= b; print(a)   

6. 논리 연산자가 아닌 것을 모두 고르시오.
    ① and
    ② or
    ③ not
    ④ nor
    ⑤ xor
    ⑥ equal

7. 다음 비트 연산자의 활용 결과를 16진수로 예측하시오.
    0xFF00 & 0x00FF
    0xFF00 | 0x00FF
    0xFF00 ^ 0x00FF

    0x0000
    0xffff
    0xffff

8. 다음 비트 시프트 연산자의 활용 결과를 예측하시오.
    a = 100; a = a << 100; a = a >> 100; print(a)

    100

9. 다음과 같이 입력한 금액을 5만원, 1만원, 5000원, 500원, 100원, 50원, 10원 동전으로 교환하는 프로그램을 작성하시오.

money, c1, c2, c3, c4, c5, c6, c7, c8 = [0] * 9

money=int(input("교환할 돈은 얼마? "))

c1 = money // 50000
money %= 50000

c2 = money // 10000
money %= 10000

c3 = money // 5000
money %= 5000

c4 = money // 1000
money %= 1000

c5 = money // 500
money %= 500

c6 = money // 100
money %= 100

c7 = money // 50
money %= 50

c8 = money // 10
money %= 10

print(" 50000원 %d장, 10000원 %d장, 5000원 %d장, 1000원 %d장" % (c1,c2,c3,c4))
print(" 500원 %d개, 100원 %d개, 50원 %d개, 10원 %d개" % (c5,c6,c7,c8))
print(" 바꾸지 못한 돈 ==> %d원"% money)

9번 코드 실행 결과

 

10. (심화문제) 115쪽의 [응용예제 02]를 활용해 비트 논리곱을 구현하려고, 숫자 2개 입력받아서 각 숫자에 대한 2진수와 비트 논리곱의 결과 2진수를 출력하는 프로그램을 작성하시오. 예로 123과 456을 입력하면 1111011&111001000의 결과 1001000이 다음과 같이 차례로 출력된다.

import turtle

num1, num2, result = 0, 0, 0
swidth, sheight = 1000, 300
curX, curY = 0, 0

def binary(Bin,num):
    curX = swidth / 2
    for i in range(len(Bin) - 2):
        turtle.goto(curX, curY)
        if num & 1 :
            turtle.color('red')
            turtle.turtlesize(2)
        else :
            turtle.color('blue')
            turtle.turtlesize(1)
        turtle.stamp()
        curX -= 50
        num >>= 1
        
if __name__ == "__main__" :
    turtle.title('거북이로 두 숫자 비트 논리곱(&) 연산하기')
    turtle.shape('turtle')
    turtle.setup(width = swidth + 50, height = sheight + 50)
    turtle.screensize(swidth, sheight)
    turtle.penup()
    turtle.left(90)

    num1 = int(input("숫자1을 입력하세요 : "))
    num2 = int(input("숫자2을 입력하세요 : "))
    binary1 = bin(num1)
    binary2 = bin(num2)
    result = num1 & num2

    curY = 50
    binary(bin(num1),num1)
    curY = 0
    binary(bin(num2),num2)
    curY = -50
    binary(bin(result),result)
    
turtle.done()

10번 코드 실행 결과

Comments