Get the App
SLTechnology News&Howtos  ›  Development  › 

How to understand the basis and common operations of tuples in Python

Shulou Source: shulou.com Published: 2022-06-03 09:28:08 10月12日 Update

This article introduces the knowledge about "how to understand the basics and common operations of tuples in Python". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

directory

1. Introduction to tuples

2. access tuple

3. Modify tuples (which cannot be modified)

4. Tuples have built-in functions such as count, index

5. type conversion

1. Convert tuples to lists

2. Convert tuples to collections

1. Introduction to tuples

Python tuples are similar to lists, except that the elements of tuples cannot be modified.

Tuples use parentheses, lists use square brackets.

Tuples are easy to create by adding elements in parentheses and separating them with commas.

Format of tuple:

tup=('a ',' b','c',' d') 2. Access Tuples

Tuples can be accessed using subscript indices, which start at 0, to access values in tuples

For example:

tup=('a','b','c','d')print(tup[0])print(tup[1])print(tup[2])print(tup[3])

It can also be accessed by looping

use a for loop

For example:

tup1=('a','b','c','d')for i in tup1: print(i)

Use the while loop

tup1 =('a ',' b','c',' d') i=0# len is the length of computation tup1 while i

Tags: Element subscript type loop foundation commonly used middle element content function parenthesis more knowledge different practical same learned next can pass dilemma character Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi MariaDB Docker NVidia Microsoft