Python Learning meta-group

Source: Internet
Author: User

Tuples and lists are similar, except that tuples are two times the list is processed, the list is [], the tuple is (), and the tuple cannot be modified, added, deleted, and it is recommended to add a comma (,) after the tuple is written.

Tuples represent:

Tu = (the "DDS",)

1. Index value or slice value

# !/usr/bin/env python # index value tu = (tu[0, "DDS",) n= "print"(n) # Slice Fetch value n1=tu[0:3]print(N1)

Results:

1(1, 2, 3)

2. For loop value

# !/usr/bin/env python # for loop value Tu = (-----,"DDS",) for -in tu:     Print(a)

Results:

123DDS

3. Convert strings and lists into tuples

# !/usr/bin/env python # string conversion to tuple s="ssshdj"= tuple (s)print(S1) # List converted to tuple li=["1df", 12,11= tuple (li)print (S2)

Results:

('s' s ' h ' s '  'd'J') ('  1df', 12, 11)

4, the first level element of the tuple cannot be modified, but the element that gets to the list can be modified

# !/usr/bin/env python # the first-level element of a tuple cannot be modified tu= (1,"ww","rtr", [(45,87)],2,) S1=tu[3]print(S1) S2=tu[3][0]print(S2) S3 =tu[3][0]=100Print(S3)print(TU)

Results:

[(a)] (the"ww""rtr ") , [100], 2)

Python Learning meta-group

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.