table of Contents
Abnormal TODO
Universal catch exceptions formula
try:
# 逻辑代码
1/0
except Exception as e:
print(e)
division by zero
Copy depth
copy
Copy / Copy shallow / deep copy of the data types are variable terms for the
l1 = ['a','b','c',['d','e','f']]
l2 = l1
l1.append('g')
print(l1) # ['a','b','c',['d','e','f'],'g']
print(l2) # ['a','b','c',['d','e','f'],'g']
If l1 l2 is a copy of the object, any change in the data element type l1 inside, the elements within l2 will also change, since the type of the variable value becomes constant id
Shallow copy
import copy
l1 = ['a','b','c',['d','e','f']]
l2 = copy.copy(l1)
l1.append('g')
print(l1) # ['a','b','c',['d','e','f'],'g']
print(l2) # ['a','b','c',['d','e','f']]
l1[3].append('g')
print(l1) # ['a','b','c',['d','e','f','g'],'g']
print(l2) # ['a','b','c',['d','e','f','g']]
If l1 l2 shallow copy of the object, the immutable elements within changed l1, l2 unchanged; if the variable elements in the changed l1, l2 will follow the change
Deep copy
import copy
l1 = ['a','b','c',['d','e','f']]
l2 = copy.deepcopy(l1)
l1.append('g')
print(l1) # ['a','b','c',['d','e','f'],'g']
print(l2) # ['a','b','c',['d','e','f']]
l1[3].append('g')
print(l1) # ['a','b','c',['d','e','f','g'],'g']
print(l2) # ['a','b','c',['d','e','f']]
If l1 l2 is deep copy of the object, the immutable elements within changed l1, l2 unchanged; if the variable elements in the changed l1, l2 will not change, i.e. l1 l2 never change because of change
Built-in data type Method
Built-in digital type method
Integer
- Role: Defining age / height / weight / id number
- Defined method:
age = 18
age = int('18')
- method:
+ - * / % // **
- Value of a plurality of values or: a value
- Ordered or disordered: they not say this orderly disorder
- Variable or non-variable: immutable
- Variable: id value becomes constant, the value becomes variable id
Float
- Role: Define wage / price
- Defined method:
salary = 3.2
salary = float('3.2')
- method:
+ - * / % // **
- Value of a plurality of values or: a value
- Ordered or disordered: they not say this orderly disorder
- Variable or non-variable: immutable
String type built-in method
- Role: Name /
- Defined method:
name = 'nick'
name = "nick"
name = '''
nick
nick
'''
name = """
nick
nick
"""
name = "'nick'"
name = '"nick"'
- method
- Priority grasp
- Index values
- slice
- length
- Segmentation split
- In addition to the blank strip on both sides
- Member operator in & emsp; not in
- for loop
- Need to know
- lstrip/rstrip
- rsplit
- upper/lower
- startstwith/endswith
- join
- replace
- isdigit
- To understanding
- find/rfind/index/rindex/count
- Center / bright / rjust / zfill
- expantabs
- capitalize/swapcase/title
- series is
- Priority grasp
- Value of a plurality of values or: a value
- Ordered or disordered: Ordered
- Variable or non-variable: immutable
List type built-in method
- Role: Multiple girlfriend /
- Is defined by: a plurality of spaced apart elements of any data type comma in []
friends_list = ['longzeluola','canglaoshi','qiaobenai','nick']
lis = list('abcd')
- method:
- Priority grasp
- Index value (in turn can take change)
- slice
- length
- append
- Member operator
- for loop
- Need to know
- count
- remove
- reverse
- pop
- insert
- sort
- index
- of the
- extend
- clear
- Priority grasp
- Value of a plurality of values or: a plurality of values
- Ordered or disordered: Ordered
- Variable or immutable: variable
Ancestral type built-in method
- Role: similar to the list, you can not keep taking
- Defined method:
friends_tuple = ('longzeluola','canglaoshi','qiaobenai','nick')
tup = tuple('abcd')
- method
- Priority grasp
- Index values (can not be changed)
- slice
- length
- Member operator
- for loop
- count
- index
- Priority grasp
- Value of a plurality of values or: a plurality of values
- Ordered or disordered: Ordered
- Variable or immutable: immutable variable that says no
A dictionary built-in methods
- Role: value too easy to keep a list, use a dictionary to take trouble
- Defined method:
nick_info_dict = {
'name':'nick',
'height':180,
'weight':140,
'hobby_list':['read','run','music','fishing','programming','coding','debugging']
}
for k,v in nick_info_dict.items():
print(k,v)
- method
- Priority grasp
- Key Value
- length
- keys/values/items
- for loop
- Member operator
- Need to know
- pop
- fromkeys
- setdefault
- get
- update
- of the
- Priority grasp
- Value of a plurality of values or: a plurality of values
- Ordered or disordered: disordered
- Variable or immutable: variable
Set type built-in method
- Action: a plurality of stored values to be set between the operation
- Defined method:
s = set()
s = {1,2,3,4,5,1}
- method:
- Priority grasp
- Deduplication
- Union |
- Intersection &
- Difference set -
- Symmetric difference ^
- Superset> emsp &;> =
- 子集 <&emsp;<=
==
- Need to know
- add
- difference_update
- isdisjoint
- remove # value error does not exist
- discard # without error
- Priority grasp
- Value of a plurality of values or: a plurality of values
- Ordered or disordered: disordered
- Variable or immutable: variable
Boolean
- Role: generally used for determining, in addition to 0 / None / air / False carrying boolean value False, carrying other data types Boolean value is True
Data type classification
By the number of values
A value | Multiple values |
---|---|
Integer / Float / String | List / Ganso / dictionary / collection / |
By order-disorder
Ordered | Disorderly |
---|---|
String / list / ancestral | Dictionary / collection |
By variable immutable
variable | Immutable |
---|---|
List / dictionary / collection | Integer / Float / String |