Python Data structures: lists, tuples, and dictionaries

Source: Internet
Author: User

There are three built-in data structures in Python-lists, tuple tuples, and dictionary Dict

Items in the list are included in square brackets , and items are separated by commas

Tuples and lists are very similar, except that tuples and strings are immutable , meaning that you cannot modify tuples. Tuples are defined by a comma-separated list of items in parentheses .

The most common use of tuples is in the print statement

Age = 22

name = ' Swaroop '

Print '%s is%d years old '% (name, age)

Print ' Why are%s playing with that python? '% name

A dictionary is similar to a key-value pair, and a dictionary is an instance/object of the Dict class, remembering that the key/value pairs in the dictionary are not sequential.

Key-value pairs are tagged in the dictionary in such a way that: D = {key1:value1, key2:value2}. Note that their key/value pairs are separated by colons, and each pair is separated by commas, all of which are included in curly braces.

Lists, tuples, and strings are sequences, and the two main features of a sequence are index operators and slice operators.

The index operator allows us to grab a specific item from the sequence, which is the subscript operation. The index can be negative, in which case the position is calculated from the end of the sequence. Therefore, Shoplist[-1] represents the last element of the sequence

The slice operator allows us to get a slice of the sequence, which is part of the sequence.

The assignment statement for the list does not create a copy (it is simply a reference). You have to use the slice operator to create a copy of the sequence.

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.