Differences between meta-ancestors, lists, and dictionaries in Python

Source: Internet
Author: User
1. Lists (list)

A list is a data structure that handles a set of ordered items, that is, you can store a sequence of items in a list.

The items in the list should be included in square brackets so python knows you are specifying a list. Once you have created a list, you can add, delete, or search for items in the list. Since you can add or delete items, we say that the list is a mutable data type, that is, the type can be changed, and the list can be nested.

Instance:

#coding =utf-8#author:rxs002animalslist = [' Fox ', ' Tiger ', ' rabbit ', ' Snake ']print (' I don't like these ', Len (animalslist ), ' animals ... ') for item in Animalislist:print (item) print (' \ n operation ') #对列表的操作, add, remove, sort animalslist.append (' pig ') del Animalslist[0]animalslist.sort () #sort是排序for I in range (0,len (animalslist)): Print (Animallist[i])

Execution Result:

I don't like these 4 animals...fox Tiger rabbit snake after Operation pig rabbit Snake Tiger

2, Ganso (tuple)

Ganso and lists are very similar, but Ganso is immutable. That is, you cannot modify the ancestor.

Ganso items are defined by commas separated by parentheses. Ganso usually makes it possible for a statement or user-defined function to safely take a set of worthwhile times, that is, the value of the Ganso being used does not change. Ganso can be nested.

>>>zoo = (' Wolf ', ' Elephant ', ' Penguin ') >>>zoo.count (' Penguin ')

1

>>>zoo.index (' Penguin ')

2

>>>zoo.append (' pig ')

execution Error: because the meta-ancestor cannot be modified

3. Dictionary (Dictionary)

The dictionary is similar to the Address book where you find the address and contact details by contact name, i.e. we associate the key (first name) with the value (details) . Note that the key must be unique, as if two people happen to have the same name, you cannot find the correct information.

Key-value pairs are tagged in the dictionary in such a way d={key:value,key2:value2}. that their key/value pairs are separated by colons, and each team is delimited by commas, all of which are included in curly braces. Also, remember that the keys/values in the dictionary are not sequential. If you want a particular order, then you should sort them before using them.

Instance:

#coding = UTF-8 #author: Rxs002dict1 = {' Zhang ': ' hellos ', ' Wang ': ' Wangbaoqiang ', ' li ': ' Bingbing ', ' Zhao ': ' Vicki '} #字典的操作, add, delete, print dict1[' Huang '] = ' Huang Jiaju ' del dict1[' Zhao ']for firstname,name in Dict1.item (): Print Firstname,name

Execution Result:

Li Bingbing Wang Wangbaoqiang Huang Huang Jiaju Zhang Hellos

Summarize

This is about Python in the meta-ancestor, list and dictionary of the introduction and the difference, I hope that you learn to use Python can be helpful.

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.