연습 문제

7.8. 연습 문제

  1. 다음 변수 ab를 사용하여 모든 산술 연산자(+, -, *, /, //, %, **)를 이용한 연산 결과를 출력하세요.

    a = 15
    b = 4
    
  2. 변수 xy를 사용하여 모든 비교 연산자(==, !=, >, <, >=, <=)를 이용한 연산 결과를 출력하세요.

    x = 10
    y = 20
    
  3. 다음 논리 연산자를 사용하여 결과를 출력하세요:

    a = True
    b = False
    
    print(a and b)  # 논리곱
    print(a or b)  # 논리합
    print(not a)  # 논리 부정
    
  4. 변수 a를 사용하여 모든 할당 연산자(+=, -=, *=, /=, //= %=, **=)를 이용한 결과를 출력하세요.

    a = 10
    
    a += 5
    print(a)
    
    a -= 3
    print(a)
    
    a *= 2
    print(a)
    
    a /= 4
    print(a)
    
    a //= 2
    print(a)
    
    a %= 2
    print(a)
    
    a **= 3
    print(a)
    
  5. 변수 ab를 사용하여 모든 비트wise 연산자(&, |, ^, ~, <<, >>)를 이용한 결과를 출력하세요.

    a = 10  # 1010 in binary
    b = 4   # 0100 in binary
    
    print(a & b)  # 비트 AND
    print(a | b)  # 비트 OR
    print(a ^ b)  # 비트 XOR
    print(~a)  # 비트 NOT
    print(a << 2)  # 비트 왼쪽 시프트
    print(a >> 2)  # 비트 오른쪽 시프트
    
  6. 다음 리스트 fruits를 사용하여 멤버십 연산자(in, not in)를 이용한 결과를 출력하세요.

    fruits = ['apple', 'banana', 'cherry']
    
    print('apple' in fruits)  # 포함되어 있음
    print('grape' not in fruits)  # 포함되어 있지 않음
    
  7. 다음 변수 a, b, c를 사용하여 아이덴티티 연산자(is, is not)를 이용한 결과를 출력하세요.

    a = [1, 2, 3]
    b = a
    c = [1, 2, 3]
    
    print(a is b)  # 동일한 객체
    print(a is not c)  # 동일한 객체가 아님
    

맨 위로 이동