Python learning notes (the next day) and python learning notes

Source: Internet
Author: User

Python learning notes (the next day) and python learning notes

 

1. dictionary replication:

Dict = {'name': 'wang', 'sex': 'M', 'age': 34, 'job': 'it '}

Info = dict # Alias (the two dictionaries point to the same address space in the memory)

Info1 = dict. copy () # shadow copy (the first layer of the nested dictionary is independent and associated below the second layer)

Import copy

Copy. copy () # shadow copy

Copy. deepcopy () # deep copy (completely independent)

Note: The associations in the light replication mode are only for the nested objects contained in the dictionary's initial state. Newly Added objects will not

Example:

>>> Dict
{'Info': ['A', 'B', 1, 2], 'job': 'it', 'sex': 'M', 'age ': 40, 'name': 'wang '}
>>> Dict_alias = dict
>>> Dict_copy = copy. copy (dict)
>>> Dict_deep = copy. deepcopy (dict)

# Adding, changing, and deleting the first-level object key values are not affected in light replication and deep Replication

>>> Dict ['age'] = 32

>>> Del dict ['sex']
>>> Dict
{'Info': ['A', 'B', 1, 2], 'job': 'it', 'age': 32, 'name ': 'wang '}
>>> Dict_alias
{'Info': ['A', 'B', 1, 2], 'job': 'it', 'age': 32, 'name ': 'wang '}
>>> Dict_copy
{'Info': ['A', 'B', 1, 2], 'job': 'it', 'age': 40, 'name ': 'wang ', 'sex': 'M '}
>>> Dict_deep
{'Info': ['A', 'B', 1, 2], 'job': 'it', 'age': 40, 'name ': 'wang ', 'sex': 'M '}

# Change or delete the original second-level object key value. The shortest copy is affected, but the deep copy is not affected.

> Dict ['info'] [2] = 100
>>> Dict
{'Info': ['A', 'B', 100, 2], 'job': 'it', 'age': 32, 'name ': 'wang '}
>>> Dict_alias
{'Info': ['A', 'B', 100, 2], 'job': 'it', 'age': 32, 'name ': 'wang '}
>>> Dict_copy
{'Info': ['A', 'B', 100, 2], 'job': 'it', 'age': 40, 'name ': 'wang ', 'sex': 'M '}
>>> Dict_deep
{'Info': ['A', 'B', 1, 2], 'job': 'it', 'age': 40, 'name ': 'wang ', 'sex': 'M '}

# Adding second-level objects will not be affected in light replication and deep Replication

>>> Dict ['new'] = {'A': 1, 'B': 2, 'C': 5}
>>> Dict
{'Info': ['A', 'B', 100, 2], 'name': 'wang', 'age': 32, 'job ': 'it', 'new': {'A': 1, 'C': 5, 'B': 2 }}
>>> Dict_alias
{'Info': ['A', 'B', 100, 2], 'name': 'wang', 'age': 32, 'job ': 'it', 'new': {'A': 1, 'C': 5, 'B': 2 }}
>>> Dict_copy
{'Info': ['A', 'B', 100, 2], 'job': 'it', 'age': 40, 'name ': 'wang ', 'sex': 'M '}
>>> Dict_deep
{'Info': ['A', 'B', 1, 2], 'job': 'it', 'age': 40, 'name ': 'wang ', 'sex': 'M '}

 

2. built-in function description:

_ Name __: the main file is returned; otherwise, the file name is returned, which can be used to determine whether the main file or the import module is used;

_ File __: absolute file path;

_ Doc __: description of the beginning of the file

Example:

'''
Created by 2015-12-18
@ Author: kevin
'''

If _ name _ = '_ main __':
Print ('this is main file ')
Print (_ file __)
Print (_ doc __)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.