Home > Backend Development > Python Tutorial > The difference between primitives, lists and dictionaries in Python

The difference between primitives, lists and dictionaries in Python

高洛峰
Release: 2017-02-25 11:23:11
Original
1609 people have browsed it

1. List

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

The items in the list should be enclosed in square brackets so Python knows you are specifying a list. Once you create a list, you can add, delete, or search for items in the list. Because you can add or remove items, we say that a list is a mutable data type, that is, the type can be changed, and lists can be nested.

Example:

#coding=UTF-8

#author:RXS002

animalslist = ['fox','tiger','rabbit','snake']

print('I do not like these',len(animalslist),'animals...')

 

for item in animalislist:

print(item)

 

print('\n操作后')

#对列表的操作,添加,删除,排序

animalslist.append('pig')

del animalslist[0]

animalslist.sort() #sort是排序

for i in range(0,len(animalslist)):

  print(animallist[i])
Copy after login

Execution result:

I do not like these 4 animals...

fox tiger rabbit snake

操作后

pig rabbit snake tiger
Copy after login

2. Tuple(tuple)

Tuple is very similar to list, but tuple is immutable. That is, you cannot modify the ancestor.

Primaries are defined by comma-separated items in parentheses. Tuples are usually used to enable statements or user-defined functions to safely take a set of values, that is, the value of the tuple being used will not change. Ancestors can be nested.

>>>zoo = ('wolf','elephant','penguin')

>>>zoo.count('penguin')
Copy after login

1

>>>zoo.index('penguin')
Copy after login

2

>>>zoo.append('pig')
Copy after login

Execution error: Because the ancestor cannot be modified

3. Dictionary

A dictionary is similar to an address book where you look up addresses and contact details by contact name, that is, we associate the key (name) with the value (details) . Note that the key must be unique, as you won't be able to find the correct information if two people happen to have the same name.

Key-value pairs are marked in the dictionary in this way: d={key:value,key2:value2}.Note that their key/value pairs are separated by colons, and each team Separate with commas, all enclosed in curly braces. In addition, Remember that the keys/values ​​in the dictionary are not in order. If you want a specific order, then you should sort them before using them.

Example:

#coding = UTF-8 

#author:rxs002

dict1 = {'zhang':'张家辉','wang':'王宝强','li':'李冰冰','zhao':'赵薇'}

#字典的操作,添加,删除,打印

dict1['huang'] = '黄家驹'

del dict1['zhao']

for firstname,name in dict1.item():

  print firstname,name  
Copy after login

Execution result:

li   李冰冰

wang 王宝强

huang 黄家驹

zhang 张家辉
Copy after login

Summary

The above is the introduction and difference between primitives, lists and dictionaries in Python. I hope it will help It can be helpful for everyone to learn to use Python.

For more articles related to the differences between Yuanzu, lists and dictionaries in Python, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template