Advanced Python Summary

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

  1. Role: Defining age / height / weight / id number
  2. Defined method:
age = 18
age = int('18')
  1. method: + - * / % // **
  2. Value of a plurality of values ​​or: a value
  3. Ordered or disordered: they not say this orderly disorder
  4. Variable or non-variable: immutable
  • Variable: id value becomes constant, the value becomes variable id

Float

  1. Role: Define wage / price
  2. Defined method:
salary = 3.2

salary = float('3.2')
  1. method:+ - * / % // **
  2. Value of a plurality of values ​​or: a value
  3. Ordered or disordered: they not say this orderly disorder
  4. Variable or non-variable: immutable

String type built-in method

  1. Role: Name /
  2. Defined method:
name = 'nick'
name = "nick"
name = '''
nick
nick
'''
name = """
nick
nick
"""
name = "'nick'"
name = '"nick"'
  1. method
    1. Priority grasp
      1. Index values
      2. slice
      3. length
      4. Segmentation split
      5. In addition to the blank strip on both sides
      6. Member operator in & emsp; not in
      7. for loop
    2. Need to know
      1. lstrip/rstrip
      2. rsplit
      3. upper/lower
      4. startstwith/endswith
      5. join
      6. replace
      7. isdigit
    3. To understanding
      1. find/rfind/index/rindex/count
      2. Center / bright / rjust / zfill
      3. expantabs
      4. capitalize/swapcase/title
      5. series is
  2. Value of a plurality of values ​​or: a value
  3. Ordered or disordered: Ordered
  4. Variable or non-variable: immutable

List type built-in method

  1. Role: Multiple girlfriend /
  2. Is defined by: a plurality of spaced apart elements of any data type comma in []
friends_list = ['longzeluola','canglaoshi','qiaobenai','nick']
lis = list('abcd')
  1. method:
    1. Priority grasp
      1. Index value (in turn can take change)
      2. slice
      3. length
      4. append
      5. Member operator
      6. for loop
    2. Need to know
      1. count
      2. remove
      3. reverse
      4. pop
      5. insert
      6. sort
      7. index
      8. of the
      9. extend
      10. clear
  2. Value of a plurality of values ​​or: a plurality of values
  3. Ordered or disordered: Ordered
  4. Variable or immutable: variable

Ancestral type built-in method

  1. Role: similar to the list, you can not keep taking
  2. Defined method:
friends_tuple = ('longzeluola','canglaoshi','qiaobenai','nick')
tup = tuple('abcd')
  1. method
    1. Priority grasp
      1. Index values ​​(can not be changed)
      2. slice
      3. length
      4. Member operator
      5. for loop
      6. count
      7. index
  2. Value of a plurality of values ​​or: a plurality of values
  3. Ordered or disordered: Ordered
  4. Variable or immutable: immutable variable that says no

A dictionary built-in methods

  1. Role: value too easy to keep a list, use a dictionary to take trouble
  2. 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)
  1. method
    1. Priority grasp
      1. Key Value
      2. length
      3. keys/values/items
      4. for loop
      5. Member operator
    2. Need to know
      1. pop
      2. fromkeys
      3. setdefault
      4. get
      5. update
      6. of the
  2. Value of a plurality of values ​​or: a plurality of values
  3. Ordered or disordered: disordered
  4. Variable or immutable: variable

Set type built-in method

  1. Action: a plurality of stored values ​​to be set between the operation
  2. Defined method:
s = set()

s = {1,2,3,4,5,1}
  1. method:
    1. Priority grasp
      1. Deduplication
      2. Union |
      3. Intersection &
      4. Difference set -
      5. Symmetric difference ^
      6. Superset> emsp &;> =
      7. 子集 <&emsp;<=
      8. ==
    2. Need to know
      1. add
      2. difference_update
      3. isdisjoint
      4. remove # value error does not exist
      5. discard # without error
  2. Value of a plurality of values ​​or: a plurality of values
  3. Ordered or disordered: disordered
  4. Variable or immutable: variable

Boolean

  1. 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

Guess you like

Origin www.cnblogs.com/nickchen121/p/11069996.html