Python Interview

https://github.com/leisurelicht/wtfpython-cn

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))

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

相关推荐

搜索部 首先说下timeline8.18,投递8.19,约一面8.21,晚上一面call约二面8.22,上午二面下午oc周末等待(8.23,8.24)8.25,offer一年前,我还是懵懵懂懂,高考完的暑假,只会提前学学高数,未来的画像是什么?我或许无法预测。开学后,自学Python,接单,无数个客户的ddl,偷偷摸摸一个人找自习的地方,这一步步竟然为后来的我,搭建工程能力的基础。大一上,我也要感谢我的第一位老板,让我接触到了实习,师兄带着我一步步入门,看他们写的飞书文档。大一下,导师带我参与企业项目,这让我渐渐发现,应该去实践,增长见识,而非局限当下,盯着自己的小新pro。不久后,第一波投递开始,结果当然是约面极少。盯着简历上的文字和ssob,我开始思考,确实很多可以去提升。带着些许不甘心,继续沉淀,慢慢的约面也越来越多,有的时候两天7场,准备完就接着下一个日程。这一次,也许是刚好到位吧,比较match,面试答的流利,关关难关关过,成为度孝子展望未来,依然是重重挑战,果然只有收到offer的那一刻是开心的。愿在百度星海拆解的每一段代码,都能成为丈量宇宙的诗行;此志终赴星河,而今迈步重铸天阶。屏幕前的你们,在无数个向星海奔赴的日夜,一定一定,会在未来化作群星回响的征程——请永远相信此刻埋首耕耘的自己!!!
一天三顿半:???百度提前批发 offer了?不是统一和正式批排序完再发吗我靠
百度求职进展汇总
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务