Python learns the basics of 5 tuples and common functions

Tuple concept

Tuple: a = (1, 23)
List: a= [1,23]

Create and access tuples

Python tuples are similar to lists, except that tuples cannot be modified after they are created, which is similar to strings.
Tuples, like lists, are indexed and sliced with integers.
Tuple subscript: 0 – (n-1)
You can create tuples with parentheses (), or without anything. For readability, it is recommended to use ().

t1 = (1, 10.31, 'python')
t2 = 1, 10.31, 'python'
print(t1, type(t1))
# (1, 10.31, 'python') <class 'tuple'>

print(t2, type(t2))
# (1, 10.31, 'python') <class 'tuple'>

tuple1 = (1, 2, 3, 4, 5, 6, 7, 8)
print(tuple1[1])  # 2
print(tuple1[5:])  # (6, 7, 8)
print(tuple1[:5])  # (1, 2, 3, 4, 5)
tuple2 = tuple1[:]
print(tuple2)  # (1, 2, 3, 4, 5, 6, 7, 8)

be careful:
When a tuple contains only one element, you need to add a comma after the element, otherwise the parentheses will be used as operators

x = (1,)
print(type(x))  # <class 'tuple'>
print(8 * (8))  # 64
print(8 * (8,))  # (8, 8, 8, 8, 8, 8, 8, 8)

Create a two-dimensional tuple

x = (1, 10.31, 'python'), ('data', 11)
print(x)
# ((1, 10.31, 'python'), ('data', 11))

print(x[0])
# (1, 10.31, 'python')
print(x[0][0], x[0][1], x[0][2])
# 1 10.31 python

print(x[0][0:2])
# (1, 10.31)

update tuple

Note: you can connect two tuples directly with +
Tuple:

week = ('Monday', 'Tuesday', 'Thursday', 'Friday')
week = week[:2] + ('Wednesday',) + week[2:]
print(week)  # ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday')

Update tuple:
Tuples have immutable properties, so we cannot directly assign values to the elements of tuples. However, as long as the elements in tuples are mutable, we can directly change their elements. Note that this is different from assigning their elements.

t1 = (1, 2, 3, [4, 5, 6])
print(t1)  # (1, 2, 3, [4, 5, 6])

t1[3][0] = 9
print(t1)  # (1, 2, 3, [9, 5, 6])

Tuple related operators

Equal sign operator:==
Join operator+
Repeat operator*
Membership operators in, not in
"Equal sign = =" returns True only when the member and member position are the same.

There are two ways to splice tuples, using "plus sign +" and "multiplication sign *". The former splices the beginning and the end, and the latter copies the splice.

t1 = (123, 456)
t2 = (456, 123)
t3 = (123, 456)

print(t1 == t2)  # False
print(t1 == t3)  # True

t4 = t1 + t2
print(t4)  # (123, 456, 456, 123)

t5 = t3 * 3
print(t5)  # (123, 456, 123, 456, 123, 456)

t3 *= 3
print(t3)  # (123, 456, 123, 456, 123, 456)

print(123 in t3)  # True
print(456 not in t3)  # False

count function and index() function

The tuple size and content cannot be changed, so there are only count and index methods.

count() is the number of times the element appears in the tuple
index() is the index to find the element in the tuple

t = (1, 10.31, 'python')
print(t.count('python'))  # 1
print(t.index(10.31))  # 1

Decompress one-dimensional tuples

Tuples have several elements, and the left parenthesis defines several variables

t = (1, 10.31, 'python')
(a, b, c) = t
print(a, b, c)

Decompress two-dimensional tuples

Tuples have several elements, and the left parenthesis defines several variables

t = (1, 10.31, ('OK', 'python'))
(a, b, (c, d)) = t
print(a, b, c, d)
# 1 10.31 OK python

If you only want a few elements of a tuple, use the wildcard "*", which is called wildcard in English. It represents one or more elements in computer language.
The following example is to throw multiple elements to the rest variable.

t = 1, 2, 3, 4, 5
a, b, *rest, c = t
print(a, b, c)  # 1 2 5
print(rest)  # [3, 4]

If you don't care about rest variables at all, use the wildcard "*" and underline "".

t = 1, 2, 3, 4, 5
a, b, *_ = t
print(a, b)  # 1 2

Conversion of tuples and lists


tuple() list tuple
list() tuple to list

#List and tuple conversion
a=['d','2345','dfwvtgrh']
b=(1,'u',8.0)
print(a,b,sep=' ')
print(tuple(a),list(b),sep=' ')
#output
# ['d', '2345', 'dfwvtgrh'] (1, 'u', 8.0)
# ('d', '2345', 'dfwvtgrh') [1, 'u', 8.0]

join() method:
Used to connect the elements in the sequence with the specified characters to generate a new string.
Syntax: join() method syntax:
str.join(sequence)

Parameters:
Sequence -- the sequence of elements to be connected.
Return value:
Returns a new string generated by concatenating elements in a sequence with a specified character.

The following example shows how to use join():

#List join
str='*'
tuple1=('45','gt','45ghgju')
print(str.join(tuple1))
#45*gt*45ghgju

Tags: Python index join

Posted on Tue, 28 Sep 2021 15:53:00 -0400 by GooberDLX