面试总结之Python

All contents have already been moved to haoran119/python (github.com).


source code

[ZZ]知名互联网公司Python的16道经典面试题及答案 - 浩然119 - 博客园 (cnblogs.com)

百度大牛总结十条Python面试题陷阱,看看你是否会中招 (toutiao.com)

Python练手题

Python面试攻略(coding篇)- Python编程

2018年最常见的Python面试题&答案(上篇) (juejin.cn)

100+Python编程题给你练~(附答案) (qq.com)

春招苦短,我用百道Python面试题备战 (qq.com)

110道python面试题 (qq.com)

Python 面试中 8 个必考问题 (qq.com)

Python 爬虫面试题 170 道:2019 版 (qq.com)

用Python手写十大经典排序算法

53个Python经典面试题详解 (qq.com)

吐血整理的 Python 面试题(qq.com)


函数参数

    • 在 python 中,strings, tuples, 和 numbers 是不可更改的对象,而 list,dict 等则是可以修改的对象。
      • 不可变类型:变量赋值 a=5 后再赋值 a=10,这里实际是新生成一个 int 值对象 10,再让 a 指向它,而 5 被丢弃,不是改变a的值,相当于新生成了a。
      • 可变类型:变量赋值 la=[1,2,3,4] 后再赋值 la[2]=5 则是将 list la 的第三个元素值更改,本身la没有动,只是其内部的一部分值被修改了。
    • python 函数的参数传递:
      • 不可变类型:类似 c++ 的值传递,如 整数、字符串、元组。如fun(a),传递的只是a的值,没有影响a对象本身。比如在 fun(a)内部修改 a 的值,只是修改另一个复制的对象,不会影响 a 本身。
      • 可变类型:类似 c++ 的引用传递,如 列表,字典。如 fun(la),则是将 la 真正的传过去,修改后fun外部的la也会受影响
    • python 中一切都是对象,严格意义我们不能说值传递还是引用传递,我们应该说传不可变对象和传可变对象。
 1 # -*- coding: utf-8 -*-
 2 """
 3 @author: hao
 4 """
 5 
 6 def myfun1(x):
 7       x.append(1)
 8 
 9 def myfun2(x):
10       x += [2]
11 
12 def myfun3(x):
13       x[-1] = 3
14 
15 def myfun4(x):
16       x = [4]
17 
18 def myfun5(x):
19       x = [5]
20       return x
21 
22 # create a list
23 mylist = [0]
24 print(mylist)     # [0]
25 
26 # change list
27 myfun1(mylist)
28 print(mylist)     # [0, 1]
29 
30 # change list
31 myfun2(mylist)
32 print(mylist)     # [0, 1, 2]
33 
34 # change list
35 myfun3(mylist)
36 print(mylist)     # [0, 1, 3]
37 
38 # did NOT change list
39 myfun4(mylist)
40 print(mylist)     # [0, 1, 3]
41 
42 # return a new list
43 mylist = myfun5(mylist)
44 print(mylist)     # [5]
45 
46 
47 def myfun(x=[1,2]):
48       x.append(3)
49       return x
50 
51 print(myfun())    # [1, 2, 3]
52 
53 # result is not [1, 2, 3] coz x was changed
54 print(myfun())    # [1, 2, 3, 3]
View Code

Consecutive assignment

  • Assignment against list is shallow copy.
 1 a = b = 0
 2 
 3 a = 1
 4 
 5 print(a)  # 1
 6 print(b)  # 0
 7 
 8 a = b = []
 9 
10 a.append(0)
11 
12 print(a)  # [0]
13 print(b)  # [0]
14 
15 a = []
16 b = []
17 
18 a.append(0)
19 
20 print(a)  # [0]
21 print(b)  # []
View Code

 

posted on 2018-08-16 14:42  浩然119  阅读(322)  评论(0编辑  收藏  举报