current position:Home>Stroke tuple in Python

Stroke tuple in Python

2022-01-30 19:12:33 Lei Xuewei

Little knowledge , Great challenge ! This article is participating in 「 A programmer must have a little knowledge 」 Creative activities

This article has participated in  「 Digging force Star Program 」 , Win a creative gift bag , Challenge creation incentive fund .

ceremonial Python Column No 24 piece , Classmate, stop , Don't miss this from 0 The beginning of the article !

Today, the school committee is writing code , Wrote a lot of , This time we'll talk about tuples Tuple, Don't miss !

What is? Tuple

Tuple yes Python A unique type , It's a bit like a list, but it's not exactly .

If you understand it simply and roughly , So let's call it tuple Of limited length list, Not too much !

This is a tuple


obtain tuple Elements of type data :

tup = (1,2,3)
tup[0] # Get the element corresponding to the first subscript
tup[0:2] # Get sub tuple

No more operation variants , This is in the School Committee Python On the basis, the first part tells .

tuple Add, delete, check and modify

Basically don't think about it ,Python Tuples in are not allowed to be tampered with .

however , There's a premise ,tuple If the element in is not the basic data type , That is, reference types .

Then we have a way to modify its internal state , Here's the picture :

 Screenshot  2021-10-31  Afternoon 6.55.54.png

We can view and try to modify 't[0]' When Python The interpreter immediately threw an error !

So what can be done on tuples ?

Basically, there are only addition and multiplication , The effect is to generate an extended new meta group .

It's kind of like 1+1 = 11 That's what I mean .

The school committee has prepared the following code :

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/10/30 6:39  Afternoon 
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat:  Lei Xuewei 
# @XueWeiTag: CodingDemo
# @File :
# @Project : hello

tup = (3, 3, 3)
# new_tup = tup - tup #TypeError: unsupported operand type(s) for -: 'tuple' and 'tuple'
new_tup = tup + tup

#  The academic committee still likes the three companies ,666
new_tup = tup * 3
print(" Three times in a row tuple:", new_tup)
# new_tup = tup / 3 #  I don't support it. It must be next time !

print("'666' in new_tuple ? ", '666' in new_tup)
 Copy code 

Readers can directly copy the running code , The school committee added the operation effect diagram :

 Screenshot  2021-10-31  Afternoon 6.55.54.png

list turn tuple

In front of us, we roughly put tuple As a limited length list 了 ( Some scenes can be very useful , For example, fixed length list).

So we sometimes put list Turn into tuple, Still get the elements for business operations .

Next, the school committee has prepared some code displays :

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/10/30 6:39  Afternoon 
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat:  Lei Xuewei 
# @XueWeiTag: CodingDemo
# @File :
# @Project : hello

tup = (3, 3, 3)

new_tup = ([x * 2 for x in tup])
print(" Doubled tuple:", new_tup)

list = [6, 6, 6]
new_tup = tuple(list)
print(" hold list turn tuple: ", new_tup)
if 6 in new_tup:
    print("new_tup has 6 !")
 Copy code 

The operation effect is as follows :

 Screenshot  2021-10-31  Afternoon 7.16.55.png


In addition to some of the functions described above , We can also use some built-in functions (max/min etc. ) Yes tuple To operate , These functions can also be used for string, list And so on , You can follow the subscription and summarize it separately later .

by the way , like Python Friend, , Please pay attention to Python Basic column or Python From getting started to mastering the big column

Continuous learning and continuous development , I'm Lei Xuewei !
Programming is fun , The key is to understand the technology thoroughly .
Welcome to wechat , Like support collection !

copyright notice
author[Lei Xuewei],Please bring the original link to reprint, thank you.

Random recommended