The Python Quick Syntax Reference by Gregory Walters

The Python Quick Syntax Reference by Gregory Walters

Author:Gregory Walters
Language: eng
Format: epub, pdf
ISBN: 9781430264781
Publisher: Apress


.copy( )

To make a copy of a dictionary, use the .copy() method. This is a shallow copy, which means that the content of the dictionary is not copied directly by value, but by reference and points to the actual original dictionary.

>>> first = {'a':1,'b':2,'c':3}

>>> clone = first.copy()

>>> first

{'a': 1, 'b': 2, 'c': 3}

>>> clone

{'a': 1, 'b': 2, 'c': 3}

.get(key[,default])

Returns a single value by key from a dictionary. Unlike the .pop() method, this does not remove the key/value pair from the dictionary. If key does not exist in dictionary, value from the optional default parameter is returned. If default is not given, returns None.

>>> names = {'lname': 'Frackel', 'city': 'Aurora', 'state': 'CO', 'fname': 'Fred', 'phone':'222-222-2222'}

>>> names

{'lname': 'Frackel', 'city': 'Aurora', 'state': 'CO', 'fname': 'Fred', 'phone':'222-222-2222'}

>>> names.get('lname')

'Frackel'



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.