오즈코딩스쿨 초격차캠프 백엔드 4일차

Hyemin Kim·2023년 12월 12일
0

✅ 1. 파이썬 2.7 버전과 파이썬 3에서 range 차이를 설명해주세요

range 함수는 파이썬 2.7과 파이썬 3에서 약간의 차이가 있습니다.

파이썬 2.7:

  • range 함수는 리스트를 반환합니다. 예를 들어, range(5)[0, 1, 2, 3, 4]를 반환합니다.
  • 파이썬 2.7에서는 xrange 함수가 있어서 이 함수가 range와 비슷하지만, 리스트 대신에 반복 가능한 객체(iterator)를 반환합니다.
  • range는 리스트를 생성하기 때문에 큰 범위의 경우에 메모리를 많이 사용할 수 있습니다.
# 파이썬 2.7에서의 range
for i in range(5):
    print(i)

파이썬 3:

  • range 함수는 리스트를 반환하는 대신, range 객체를 반환합니다. 이는 메모리를 효율적으로 사용하며, 필요한 값을 계산하는데 필요한 메모리 양이 거의 없습니다.
  • 파이썬 3에서는 xrange 함수가 없습니다. 대신, range 함수가 파이썬 2.7의 xrange와 비슷한 역할을 합니다.
# 파이썬 3에서의 range
for i in range(5):
    print(i)

즉, 파이썬 3에서는 range 함수가 보다 효율적이고 메모리를 적게 사용하도록 개선되었습니다. 파이썬 3의 range 함수는 파이썬 2.7의 xrange와 유사한 동작을 합니다.

- Please explain the range difference between Python 2.7 and Python 3

The range function in Python 2.7 and Python 3 has some differences.

In Python 2.7, the range function returns a list. For example:

# Python 2.7
result = range(5)
print(result)  # Output: [0, 1, 2, 3, 4]

In Python 3, the range function returns an immutable sequence type called a "range" object. The range object behaves like a list in many ways, but it doesn't generate all the numbers at once. Instead, it generates them on-the-fly as you iterate over it or convert it to a list. For example:

# Python 3
result = range(5)
print(result)  # Output: range(0, 5)
print(list(result))  # Output: [0, 1, 2, 3, 4]

This change in Python 3 was made to improve memory efficiency, especially when dealing with large ranges.

If you need a list in Python 3, you can explicitly convert the range object to a list using the list() function, as shown in the example above.

✅ 2. 얕은 복사와 깊은 복사에 대해서 설명하세요

얕은 복사(Shallow Copy)와 깊은 복사(Deep Copy)는 데이터 구조를 복사하는 두 가지 주요 방법입니다. 이들은 복사된 객체가 원본 객체와 어떻게 관련되어 있는지에 따라 다릅니다.

얕은 복사 (Shallow Copy):

  • 얕은 복사는 객체의 최상위 레벨에서만 복사를 수행합니다.
  • 복사된 객체는 원본 객체의 요소에 대한 참조를 포함하고 있으며, 원본과 복사본이 같은 객체를 참조할 수 있습니다.
  • 얕은 복사는 내부 객체에 대해서는 참조를 공유하므로, 원본 객체의 내부 객체가 변경되면 복사본에도 영향을 미칠 수 있습니다.
  • 주로 copy 모듈의 copy() 함수를 사용하거나, 리스트와 딕셔너리에서 제공하는 copy() 메서드를 통해 수행됩니다.

예시:

import copy

original_list = [1, [2, 3], 4]
shallow_copied_list = copy.copy(original_list)

original_list[1][0] = 'X'

print(original_list)       # Output: [1, ['X', 3], 4]
print(shallow_copied_list)  # Output: [1, ['X', 3], 4]

깊은 복사 (Deep Copy):

  • 깊은 복사는 원본 객체의 모든 레벨에서 복사를 수행합니다.
  • 복사된 객체는 완전히 새로운 객체이며, 내부 객체에 대한 참조를 포함하지 않습니다. 따라서 원본 객체의 변화가 복사본에 영향을 미치지 않습니다.
  • 깊은 복사는 copy 모듈의 deepcopy() 함수를 사용하여 수행됩니다.

예시:

import copy

original_list = [1, [2, 3], 4]
deep_copied_list = copy.deepcopy(original_list)

original_list[1][0] = 'X'

print(original_list)       # Output: [1, ['X', 3], 4]
print(deep_copied_list)     # Output: [1, [2, 3], 4]

얕은 복사와 깊은 복사는 데이터 구조와 사용 사례에 따라 선택되어야 합니다. 깊은 복사는 객체의 모든 레벨에서 완전한 독립성을 제공하지만, 얕은 복사는 간단하게 사용할 수 있고 일부 상황에서 효율적일 수 있습니다.

- Explain shallow copy and deep copy

Shallow copy and deep copy are concepts related to copying objects in programming, particularly in the context of complex data structures like lists, dictionaries, or custom objects.

  1. Shallow Copy:

    • A shallow copy creates a new object, but does not create new objects for the elements inside the original object. Instead, it copies references to the objects found in the original.
    • Changes made to the elements inside the copied object will affect the original object and vice versa if the elements are mutable.
    • In Python, you can use the copy module to create a shallow copy using the copy() method or using the [:] slice notation for lists.
    import copy
    
    original_list = [1, [2, 3], [4, 5]]
    shallow_copy = copy.copy(original_list)
    
    # Modify an element in the shallow copy
    shallow_copy[1][0] = 999
    
    print(original_list)  # Output: [1, [999, 3], [4, 5]]
  2. Deep Copy:

    • A deep copy creates a new object and recursively copies all objects found in the original object. It creates completely independent copies of both the original object and all nested objects.
    • Changes made to the elements inside the copied object do not affect the original object, and vice versa.
    • In Python, you can use the copy module to create a deep copy using the deepcopy() method.
    import copy
    
    original_list = [1, [2, 3], [4, 5]]
    deep_copy = copy.deepcopy(original_list)
    
    # Modify an element in the deep copy
    deep_copy[1][0] = 999
    
    print(original_list)  # Output: [1, [2, 3], [4, 5]]

In summary, a shallow copy creates a new object with references to the same elements as the original, while a deep copy creates a new object with copies of all elements, including nested elements, making it fully independent of the original.

profile
Full Stack Web Developer

0개의 댓글