Python Interview

The original URL: https://github.com/satwikkansal/wtfpython

Datatype

All problems about datatype.

Shallow-copy and Deep-copy

When it comes to deep copy and shallow copy, the differences can be compared based on data types, copy principles, and copy functions. Here is a table in Markdown format that summarizes the differences between deep copy and shallow copy:

Deep Copy

Mutable and Immutable Data Types

Create a new object and recursively copy all nested objects of the original object. The new object is completely independent from the original object, and modifications to the copy won't affect the original object.

copy.deepcopy()

Shallow Copy

Mutable Data Types

Create a new object where a part of it shares the same reference as the original object. Only the first level of the original object is copied, and nested objects are still shared. Modifications to the copy will affect the original object.

copy.copy()

Immutable Data Types

Not applicable

Not applicable

Not applicable

Strings, Numbers, and other Immutable Objects

Whether it's a deep copy or shallow copy, a new object will be created. However, for immutable objects that cannot be modified, there is no difference between deep copy and shallow copy.

This table provides an overview of the differences between deep copy and shallow copy based on different data types, copy principles, and the use of copy functions. Please note that the mentioned copy functions refer to the copy module provided by the Python standard library.

Following is code about this feature:

import copy

nested_array = [1, 2, 7, [4, 9, 6]]

shallow_copy = copy.copy(nested_array)

deep_copy = copy.deepcopy(nested_array)

# Original array: [1, 2, 7, [4, 9, 6]] 2456116559808
print("Original array:", nested_array, id(nested_array), "\n")
# Shallow copy before modifying: [1, 2, 7, [4, 9, 6]] 140724048356296
print("Shallow copy before modifying:", shallow_copy, id(shallow_copy[3]), "\n")
nested_array[3].append(512)
# Shallow copy after modifying: [1, 2, 7, [4, 9, 6, 512]] 140724048356296 
print("Shallow copy after modifying:", shallow_copy, id(shallow_copy[3]), "\n")
# original array: [1, 2, 7, [4, 9, 6]] 2456116539264 
print("Original array:", deep_copy, id(deep_copy), "\n")
nested_array[3][2] = 1024
# Original array after modifying by deep copying: [1, 2, 7, [4, 9, 1024, 512]] 2456116559808
print("Original array after modifying by deep copying:", nested_array, id(nested_array), "\n")
# original array after deep copying: [1, 2, 7, [4, 9, 6]] 2456116539264 
print("Original array after deep copying:", deep_copy, id(deep_copy), "\n")

unconspicuous shallow copying

>>> row = [""] * 3
>>> board = [row] * 3
>>> board
[['', '', ''], ['', '', ''], ['', '', '']]
>>> board[0][0]
''
>>> board[0][0] = "X"
>>> board
[['X', '', ''], ['X', '', ''], ['X', '', '']]

To avoid this problem, we can initialize a grid, and instantly modify the value of [0][0].

board = [[""] * 3 for _  in range(3)]
board[0][0] = "X"
print(board)

small integer caching

Original: Pylong_Fromlong

Quote: The reason of small integer caching

The range of caching is from -5 to 256. And the meaning of is mean to varaibles which possessed the same memory address. So, for those caching integer number, they are assigned memory address in advance. When we use assignment statement to assign number for variables, those variables will get the same memory address for the same value.

a, b = 256, 256
for i in range(250, 260):
    if a is not b:
        break
    a += 1
    b += 1
print("start point: ", a, "end point:", b)  # a = b = 257

dict or set

uniqueness of the key's logic

Althogh there are three object, such as number, complex, and float number, those objects have the same logic value.

>>> collections = dict()
>>> temporary = 5 + 0j
>>> collections[5] = "Dog"
>>> collections[5]
'Dog'
>>> collections[5.0] = "Pear"
>>> collections[5.0]
'Pear'
>>> collections[temporary] = "King"
>>> collections[temporary]
'King'
>>> collections[5]
'King'
>>> collections[5.0]
'King'
>>> collections[temporary]
'King'

Operator

all about operator in python.

walrus operator

version: Python 3.8

PEP No: 572

Function

try-except

  • The return value is determined by the last return sentence.
  • The finally sentence will be definitely be executed.
  • The interactive interface gives the output, but the IDE maybe gives output.
def simple():
    try:
        # return 10086
        return "from try"
    finally:
        # return 1024
        return "from finally"


def asceticism():
    for _ in range(3):
        try:
            continue
        finally:
            print(10000)


def complex():
    try:
        for i in range(3):
            try:
                1 / i
            except ZeroDivisionError:
                raise ZeroDivisionError("Invalid value")
            finally:
                print("Normal", i)
                break
    except ZeroDivisionError as e:
        print("Zero devision error occured", e)

reassign value

array = [1, 2, 5]
g = (x for x in array if array.count(x) > 0)
array = [9, 2, 4]
print(list(g))

#我的实习求职记录#
全部评论

相关推荐

昨天 00:11
已编辑
广东工业大学 算法工程师
避雷深圳  yidao,试用期 6 个月。好嘛,试用期还没结束,就直接告诉你尽快找下一家吧,我谢谢您嘞
牛客75408465号:笑死,直属领导和 hr 口径都没统一,各自说了一些离谱的被裁理由,你们能不能认真一点呀,哈哈哈哈哈😅😅😅
点赞 评论 收藏
分享
面试摇了我吧:啊哈哈面试提前五个小时发,点击不能参加就是放弃
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务