# the difference between value and address
# For simple values, pass operations are used, that is, operations on parameters inside a function do not affect variables outside
# For complex variables, address transfer operation is adopted. In this case, the parameters inside the function and external variables are the same content.
# Any change to this content affects the use of additional variables or parameters
def a(n) :
n[2] = 300
print(n)
return None
def b(n) :
n += 100
print(n)
return None
an = [1.2.3.4.5.6]
bn = 9
print(an)
a(an)
print(an)
print(bn)
b(bn)
print(bn)
Copy the code
[1, 2, 3, 4, 5, 6] [1, 2, 300, 4, 5, 6] [1, 2, 300, 4, 5, 6] 9 September 109Copy the code
Functions of lists
l = ['a'.'i love you'.45.766.5+4j]
l
Copy the code
['a', 'i love you', 45, 766, (5+4j)]
Copy the code
# append Inserts a content, append at the end
a = [i for i in range(1.5)]
print(a)
a.append(100)
print(a)
Copy the code
[1, 2, 3, 4, 100]Copy the code
# insert: insert at the specified position
# insert(index, date) before index
print(a)
a.insert(3.Awesome!)
print(a)
Copy the code
[1, 2, 3, 666, 4, 100]Copy the code
# remove
# del delete
# pop, take an element out of the corresponding bit, that is, take the last element out
print(a)
last_ele = a.pop()
print(last_ele)
print(a)
Copy the code
[1, 2, 3, 666, 4, 100]Copy the code
# remove: Removes the element with the specified value from the list
If the deleted value is not in the list, an error is reported
Delete a List of values using a try... Excepty statement, or the judgment first
# if x in list:
# list.remove(x)
a.insert(4.Awesome!)
print(a)
print(id(a))
a.remove(Awesome!)
print(a)
print(id(a))
Select * from list; remove from list
Copy the code
[1, 2, 3, 4, 666]
2261435601928
[1, 2, 3, 4]
2261435601928
Copy the code
# clear: Clear
print(a)
print(id(a))
a.clear()
print(a)
print(id(a))
If you do not want the list address to remain the same, you can clear the list using the following method
# a = list[]
# a = [ ]
Copy the code
[] [2261435601928] 2261435601928Copy the code
# reverse: Reverse in place
a = [1.2.3.4.5]
print(a)
print(id(a))
a.reverse()
print(a)
print(id(a))
Copy the code
[1, 2, 3, 4, 5] 2261441839496 [5, 4, 3, 2, 1Copy the code
# exrend: Extended list, two lists, concatenating one directly to the other
a = [1.2.3.4.5]
b = [6.7.8.9.10]
print(a)
print(id(a))
a.extend(b)
print(a)
print(id(a))
Copy the code
[1, 2, 3, 4, 5]
2261441540872
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
2261441540872
Copy the code
# count: Find the number of specified values or elements in the list
print(a)
a.append(8)
a.insert(4.8)
print(a)
a_len = a.count(8)
print(a_len)
Copy the code
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 8, 5, 6, 7, 8, 9, 10, 8]
3
Copy the code
# copy: shallow copy
Example of list-type variable assignment
a = [1.2.3.4.5.Awesome!]
print(a)
# list, a simple assignment operation, is to pass the address
b = a
b[3] = 777
print(a)
print(id(a))
print(b)
print(id(b))
print("*" * 20)
To solve this problem, copy is required for list assignment
b = a.copy()
print(a)
print(id(a))
print(b)
print(id(b))
print("*" * 20)
b[3] = 888
print(a)
print(b)
Copy the code
[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]Copy the code
# Deep copy vs. shallow copy
Copy is a shallow copy function that copies only one layer of content
Deep copy requires specific tools
a = [1.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] = Awesome!
print(a)
print(b)
Copy the code
[1, 2, 3, [10, 20, 666]] [1, 2, 3, [10, 20, 666]Copy the code
A tuple – tuple
- A tuple can be thought of as an immutable list
Tuples create
Create an empty tuple
t = ()
print(type(t))
Create a tuple with a single value
s = (1)
print(type(s))
print(s)
t = (1.)print(type(t))
print(t)
t = 1.print(type(t))
print(t)
Create tuples of multiple values
t = (1.2.3.4.5)
print(type(t))
print(t)
t = 1.2.3.4.5
print(type(t))
print(t)
Create using another structure
l = [1.2.3.4.5]
t = tuple(l)
print(t)
Copy the code
<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)
Copy the code
Properties of tuples
- It’s a list of sequences, ordered
- Tuple data values are accessible, cannot be modified, cannot be modified, cannot be modified
- Tuple data can be of any type
- In short, all features of list, except modifiable, are available to tuples
- This means that some of the operations that list has, such as indexing, sharding, sequence addition, multiplication, membership operations, etc., are exactly the same
# index operation
t = (1.2.3.4.5)
print(t[4])
Copy the code
5
Copy the code
# exceed error
print(t[12])
Copy the code
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-22-0db0bf4ec3b5> in <module> 1 # supererror ----> 2 print(t[12]) IndexError: tuple index out of rangeCopy the code
t = (1.2.3.4.5.6)
t1 = t[1: :2]
print(id(t))
print(id(t1))
print(t1)
# Slices can exceed the standard
t2 = t[2:100]
print(t2)
Copy the code
2195798058760 2195797607552 (2, 4, 6)Copy the code
# add sequence
t1 = (1.2.3)
t2 = (4.5.6.7)
Address operation
print(t1)
print(id(t1))
t1 += t2
print(t1)
print(id(t1))
# The above operation is similar to
t1 = (1.2.3)
t1 = (2.3.4)
# tuple is immutable, which means the content cannot be modified
# Error if tuple contents are modified
t1[1] = 100
Copy the code
(1, 2, 3) 2195798298200 (1, 2, 3, 4, 5, 6, 7) 2195795953560 --------------------------------------------------------------------------- TypeError Traceback (most Recent call last) <ipython-input-32-e65ebb898657> in <module> 16 # tuple 17 t1[1] = 100 TypeError: 'tuple' object does not support item Assignment. TypeError: 'tuple' object does not support item AssignmentCopy the code
# tuple multiplication
t = (1.2.3)
t = t * 3
print(t)
Copy the code
(1, 2, 3, 1, 2, 3, 1, 2, 3)
Copy the code
# Member detection
t = (1.2.3)
if 2 in t:
print("Yes")
else:
print("No")
Copy the code
Yes
Copy the code
# tuple traversal, usually using for
# 1. Single-layer tuple traversal
t = (1.2.3."ruochen"."i"."love")
for i in t:
print(i, end="")
Copy the code
1 2 3 ruochen i love
Copy the code
# 2. Traversal of double tuples
t = ((1.2.3), (2.3.4), ("i"."love"."you"))
The traversal of the above tuples can be as follows
# 1.
for i in t:
print(i)
for k,m,n in t:
print(k, "--", m, "--", n)
Copy the code
(1, 2, 3)
(2, 3, 4)
('i', 'love', 'you')
1 -- 2 -- 3
2 -- 3 -- 4
i -- love -- you
Copy the code