Tuples, dictionaries, sequences, objects, and references

Source: Internet
Author: User

Example one:

Tuples: immutable, commas inside parentheses

Similar to a list, except that tuples and strings are immutable , meaning that you cannot modify tuples, which are defined by a comma-separated set of items in parentheses. Tuples are typically used when a statement or user-defined function can safely take a set of values, that is, the value of the tuple being used does not change.

#!/usr/bin/python#Filename:using_tuple.pyZoo= ('Wolf','Elephant','Penguin')Print 'Number of animals in the zoo is', Len (Zoo) New_zoo= ('Monkey','Dolphin', Zoo)Print 'Number of animals in the new Zoo is', Len (new_zoo)Print 'All animals in new zoo is', New_zooPrint 'Animals brought from the old Zoo is', new_zoo[2]Print 'Last animal brought from the old Zoo is', New_zoo[2][2]

Example two:

Dictionary: Key-value pairs. D = {key1:value1, key2:value2}

The dictionary is similar to the Address book where you find the address and contact details by contact name, that is, we associate the key (first name) with the value (details). Note that the key must be unique

#-*-encoding:utf-8-*-#!/usr/bin/env python#Filename:using_dict.py#The dictionary is similar to the Address book where you find the address and contact details by contact name, that is, we associate the key (first name) with the value (details). #Note that the key must be unique, like if two people happen to have the same name, you can't find the right informationAB={'Swaroop':'[email protected]',    'Larry':'[email protected]',    'Matsumoto':'[email protected]',    'spammer':'[email protected]'   }Print "Swaroop ' s address is%s"%ab['Swaroop']#Adding a Key/value pairab['Guido']='[email protected]'#Deleting a Key/value pairdelab['spammer']Print '\nthere is%d contacts in the address-book\n'%Len (AB) forName,addressinchAb.items ():Print 'Contact %s at%s'%(name,address)if 'Guido' inchAb:#OR ab.has_key (' Guido ')    Print "\nguido ' s address is%s"%ab['Guido']

Example three, sequence

Lists, tuples, and strings are sequences, but what are the sequences and why are they so special? The two main features of a sequence are index operators and slice operators.

#!/usr/bin/env python#Filename:seq.pyshoplist=['Apple','Mango','Carrot','Banana']#indexing or ' Subscription ' OperationPrint 'Item 0 is', Shoplist[0]Print 'Item 1 is', shoplist[1]Print 'Item 2 is', shoplist[2]Print 'Item 3 is', shoplist[3]Print 'Item-1 is', shoplist[-1]Print 'Item-2 is', shoplist[-2]#slicing on a listPrint 'Item 1 to 3 is', Shoplist[1:3]Print 'Item 2 to end is', shoplist[2:]Print 'Item 1 to-1 is', shoplist[1:-1]Print 'Item start to end is', shoplist[:]#slicing on a stringName='Swaroop'Print 'characters 1 to 3 is', Name[1:3]Print 'characters 2 to end', name[2:]Print 'characters 1 to-1 is', name[1:-1]Print 'characters start to end are', name[:]

Example four references: (point)

#!/usr/bin/env python#Filename:reference.pyPrint 'Simple Assignment'shoplist=['Apple','Mango','Carrot','Banana']mylist=shoplist#MyList is just another name pointing to the same object!delshoplist[0] #del mylist[0] is the same result Print 'Shoplist is', ShoplistPrint 'MyList is', MyList#Notice that both shoplist and MyList both print the same list without#The ' Apple ' confirming that they point to the same objectPrint 'Copy by making a full slice'MyList=shoplist[:]#Make a copy by doing a full slicedelMYLIST[0]#Remove First itemPrint 'Shoplist is', ShoplistPrint 'MyList is', MyList#Notice that now the lists is different

Tuples, dictionaries, sequences, objects, and references

Related Article

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.