# For simple values, use value transfer operation, that is, the operation on parameters within the function does not affect the outside variables # For complex variables, use address transfer operation, in this case, the parameters within the function and the external variables are the same content. Def a(n): n[2] = 300 print(n) return None def b(n): print(n) return None def b(n): Print (n) return n = [1,2,3,4,5,6] bn = 9 print(an) a(an) print(an) print(b) print(bn)
[1, 2, 3, 4, 5, 6] [1, 2, 300, 4, 5, 6] [1, 2, 300, 4, 5, 6] 9 September 109
Functions about lists
l = ['a', 'i love you', 45, 766, 5+4j]
l
['a', 'i love you', 45, 766, (5+4j)]
A = [I for I in range(1,5)] print(a) a.append(100) print(a)
[1, 2, 3, 4] [1, 2, 3, 4]
Print (a) a.insert(3, 666) print(a)
[1, 2, 3, 666, 4, 100]
Print (a) last_ele = a.op () print(last_ele) print(a) last_ele = a.op () print(last_ele) print(a)
[1, 2, 3, 666, 4] [1, 2, 3, 666, 4]
If the value is not in the list, the list should be removed using a try... Excepty statement: # if x in list: # list.remove(x) a.insert(4, 666) print(a) print(id(a)) a.insert(666) print(id(a)) The remove operation operates directly from the original list
[1, 2, 3, 4] 2261435601928 [1, 2, 3, 4
Print (a) print(id(a)) a.clear() print(a) print(id(a)) # print(id(a)) # print(id(a)) # print(id(a)) #
[] [2261435601928] 2261435601928
A = [1,2,3,4,5] print(A) print(A) print(A) print(A)
[1, 2, 3, 4, 5] 2261441839496 [5, 4, 3, 1
A = [1,2,3,4,5] b = [6,7,8,9,10] print(a) print(id(a)) a.exxtend (b) print(a) print(id(a))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] [1, 2, 3, 4, 5
Print (a) a.append(8) a.insert(4, 8) print(a) a_len = a.count(8) print(a_len)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 8, 5, 6, 7, 8, 9, 10, 8]
3
# copy: Print (a) # print(a) # print(a) # print(a) # print(a) # print(a) # print(a) Print (a) print(id(a)) print(b) print(id(b)) print("*" * 20) # Print (a) print(id(a)) print(b) print(id(b)) print("*" * 20) b[3] = 888 print(b) print("*" * 20) b[3] = 888 print(b) print(b)
[1, 2, 3, 4, 5, 666] [1, 2, 3, 777, 5, 666] $2195797555400 [1, 2, 3, 777, 5, 666] 2195797555400 * * * * * * * * * * * * * * * * * * * * [1, 5, 2, 3, 777, 666] 2195797555400 [1, 2, 3, 777, 5, 666] 2195798283976 * * * * * * * * * * * * * * * * * * * * [1, 2, 3, 777, 5, 666] [1, 2, 3, 888, 5, 666]
This is because the copy function is a shallow copy function. A = [1,2,3, 2,3] [10 ,20 ,30]] b = a.copy() print(id(a)) print(id(b)) print(id(a[3])) print(id(b[3])) a[3][2] = 666 print(a) print(b)
[1, 2, 3, [10, 20, 666]] [2, 3, [10, 20, 666]]
A tuple – tuple
- A tuple can be thought of as an immutable list
Tuples create
Print (type(s)) print(type(s)) print(type(s)) print(type(s)) print(type(s)) print(type(t)) print(type(t)) print(type(t)) print(type(t)) print(type(t)) print(type(t) Print (type(t)) print(type(t)) print(type(t)) print(type(t)) print(type(t)) print(t) # print(type(t) L = [1,2,3,4,5] t = tuple(l) print(t)
<class 'tuple'>
<class 'int'>
1
<class 'tuple'>
(1,)
<class 'tuple'>
(1,)
<class 'tuple'>
(1, 2, 3, 4, 5)
<class 'tuple'>
(1, 2, 3, 4, 5)
(1, 2, 3, 4, 5)
Properties of tuples
- It’s a sequential list, ordered
- Tuple data values can be accessed, cannot be modified, cannot be modified, cannot be modified
- Tuple data can be of any type
- In summary, all of the features of a List, except for being modifiable, are shared by tuples
- This means that some of the operations on a list, such as indexing, sharding, sequence addition, multiplication, and membership operations, are identical
Print (t = (1,2,3,4,5)) print(t[4])
5
Print (t[12]) print(t[12])
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-22-0db0bf4ec3b5> in <module> 1 # overstandard error ----> 2 print(t[12]) indexError: tuple index out of range
T = (6) t1 = t / 1:2: print (id (t)) print (id) (t1) print (t1) # slice can exceed the t2 = t [2:10 0] print (t2)
2195798058760 2195797607552 (2, 4, 6)
Addition of # sequence t1 = (1, 2, 3) t2 = (4, 7) # reference print operation (t1) print (id) (t1) t1 + = t2 print (t1) print (id) (t1) # above operations, T1 = (1,2,3) t1 = (2,3,4) # Unmodifiable # Modifying the contents of a tuple causes an error t1[1] = 100
(1, 2, 3) 2, 3, 4, 5, 6, 7) 2195795953560 --------------------------------------------------------------------------- TypeError Traceback (most Recent call last) <ipython-input-32-e65ebb898657> in <module> 16 # tuple > 18 t1[1] = 100 TypeError: 'Tuple' object does not support item assignment
T = (1,2,3) t = t * 3 print(t)
(1, 2, 3, 1, 2, 3)
If 2 int: print("Yes") else: print("No")
Yes
T = (1,2,3,"ruochen", "I ", "love") for I int: print(I, end=" ")
1 2 3 ruochen i love
For I int: print(I) for k,m,n int: print(I) for k,m,n int: print(I) for k,m,n int: print(k, "--", m, "--", n)
(1, 2, 3)
(2, 3, 4)
('i', 'love', 'you')
1 -- 2 -- 3
2 -- 3 -- 4
i -- love -- you