Get the App
SLTechnology News&Howtos  ›  Development  › 

How to understand python tuples

Shulou Source: shulou.com Published: 2022-06-02 23:45:18 09月22日 Update

This article mainly explains "how to understand python tuples". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian slowly and deeply to study and learn "how to understand python tuples" together!

directory

1. Unpacking

2、enumerate

3、list()

A tuple is an immutable sequence that cannot be modified once created.

1. Unpacking

Take the elements of a tuple and assign them to different variables

>>> a = ('hello', 'world', 1, 2, 3)>>> str1, str2, n1, n2, n3 = a>>> str1'hello'>>> str2'world'>>> n11>>> n22>>> n33>>> str1, str2, *n = a>>> str1'hello'>>> str2'world'>>> n[1, 2, 3]>>> str1, _, n1, n2, _ = a2、enumerate

Explanation: used for tuple traversal, get tuple object, the first element is index, the second is numeric

a = ('1', 2, 35, 'hello')for i in enumerate(a): print(i)>>> (0, '1')>>> (1, 2)>>> (2, 35)>>> (3, 'hello')3、list()

Tuples converted to lists

Pythona =('1 ', 2, 35, ' hello') print(list(a))>> ['1', 2, 35, ' hello'] Thank you for your reading. The above is the content of "How to understand python tuples". After studying this article, I believe you have a deeper understanding of how to understand python tuples. The specific use needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Tags: Learning elements content differences variables objects that is sequences ideas situations numerical values articles more features catalogs knowledge knowledge points articles indexes follow Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Shulou Information Microsoft Huawei Shulou Tech Info