DAY 06 impact Blue Bridge Cup - Python foundation 06python tuple

Posted by 01hanstu on Thu, 27 Jan 2022 22:50:42 +0100

7.1 some concepts of tuples

7.1.1 basic definition of tuple

Tuples are used to store multiple items in a single variable. Tuple is one of the four built-in data types used to store data sets in Python. The other three are list, set and dictionary, which have different properties and usages. A tuple is a set that is ordered and immutable. Tuples are written in parentheses.
The basic form is as follows:

mytuple = ("hg Number one", "hg Number two", "hg Number three")
print(mytuple)

tuple() can also be used to create tuples as follows:

tuple5 = tuple(("hg Number one", "hg Number two", "hg Number three", "hg Number three"))
print(tuple5)

Tuple items are ordered, unchangeable, and allow duplicate values. Tuple items are indexed, the first item is indexed [0], the second item is indexed [1], etc. The difference list is unordered and can be changed.
(1) Allow repetition, for example:

mytuple = ("hg Number one", "hg Number two", "hg Number three", "hg Number three")
print(mytuple)

(2) Index, for example

mytuple =  ("hg Number one", "hg Number two", "hg Number three")
# print(mytuple)
print(mytuple[0])

7.1.2 length of tuple

(1) Like the list, we use the len function. The example is as follows:

mytuple =  ("hg Number one", "hg Number two", "hg Number three")
print(len(mytuple))

(2) A tuple, remember the comma, otherwise it is not a tuple! I can read and write the two forms and make a comparison:

mytuple = ("hg Number one", )
print(type(mytuple))
# result tuple
mytuple = ("hg Number one")
print(type(mytuple))
# result str

7.1.2 data type

Tuple items can be any data type: string, integer, and Boolean

tuple1 = ("hg Number one", "hg Number two", "hg Number three", "hg Number three")
tuple2 = (1, 8, 5, 9, 3)
tuple3 = (True, False, False)

7.2 "query" of tuples

7.2.1 normal access

Tuple items can be accessed by referencing the index number in square brackets, such as printing the second item:

mytuple =  ("hg Number one", "hg Number two", "hg Number three")
# print(mytuple)
print(mytuple[0])

Note: the index of the first item is 0.

7.2.2 negative index

A negative index means starting from scratch- 1 refers to the last item, - 2 refers to the penultimate item, etc.
For example, print the last item of a tuple:

mytuple =  ("hg Number one", "hg Number two", "hg Number three")
# print(mytuple)
print(mytuple[-1])

7.2.3 scope index

(1) You can specify the index range by specifying the start and end positions of the range. When a range is specified, the return value will be a new meta group with the specified item. Here we use the range function, as mentioned earlier.
Return to items 3, 4 and 5:

mytuple =  ("hg Number one", "hg Number two", "hg Number three", "hg Number three", "hg Number three", "hg Number five")
# print(mytuple)
print(mytuple[2:5])

Note: the search will start at index 2 (inclusive) and end at index 5 (exclusive).
Remember that the index of the first item is 0.

(2) By omitting the starting value, the range will start from the first item:

mytuple = ("hg Number one", "hg Number two", "hg Number three", "hg Number three", "hg Number three", "hg Number five")
print(mytuple[:4])

(3) By omitting the end value, the range continues to the end of the list:

mytuple = ("hg Number one", "hg Number two", "hg Number three", "hg Number three", "hg Number three", "hg Number five")
print(mytuple[2:])

(4) Negative range:

mytuple =  ("hg Number one", "hg Number two", "hg Number three", "hg Number three", "hg Number three", "hg Number five")
print(mytuple[-4:-1])

(5) Check whether the item value exists:
To determine whether the specified item exists in the tuple, use the in keyword:

mytuple =  ("hg Number one", "hg Number two", "hg Number three", "hg Number three", "hg Number three", "hg Number five")
if "hg Number five" in thistuple:
  print("ha-ha, hg Number five is on the Yuanzu list")

7.3 "change" of tuple

Tuples are immutable, which means that once you create a tuple, you cannot change, add, or delete items. But there are some solutions: convert tuples to lists, change the list, and then convert the list back to tuples.

7.3.1 replacement

For example, I want to change the second element below to handsome guy:

mytuple =  ("hg Number one", "hg Number two", "hg Number three", "hg Number three", "hg Number three", "hg Number five")
y = list(x)
y[1] = "handsome guy"
x = tuple(y)
print(x)

7.3.2 add item value

Because tuples are immutable, they have no built-in append() method, but there are other methods to add items to tuples.

(1) Convert to list: just like the solution to changing tuples, you can convert them to lists, add your items, and then convert them back to tuples.
For example: convert a tuple to a list, add "love you", and then convert it back to a tuple:

mytuple =  ("hg Number one", "hg Number two", "hg Number three", "hg Number three", "hg Number three", "hg Number five")
y = list(mytuple)
y.append("love you")
mytuple = tuple(y)
print(mytuple)

(2) Add tuples to tuples. You can add tuples to tuples, so if you want to add one (or more) items, use that item to create a new metagroup and add it to an existing tuple.
For example: create a new meta group with the value "orange" and add the tuple

mytuple =  ("hg Number one", "hg Number two", "hg Number three", "hg Number three", "hg Number three", "hg Number five")
y = ("love you",)
thistuple += y
print(mytuple)

Note: when creating a tuple with only one item, remember to add a comma after the item, otherwise it will not be recognized as a tuple (string).

7.3.3 delete item

Note: you cannot delete items in tuples. Tuples are immutable, so you cannot delete items from them, but you can use the same solution we used to change and add tuple items:

Examples
Convert tuples to lists, delete "apple", and then convert them back to tuples:

thistuple = ("hg Number one", "hg Number two", "hg Number three", "hg Number three", "hg Number three", "hg Number five")
y = list(thistuple)
y.remove("hg Number one")
thistuple = tuple(y)
print(thistuple)

Or you can completely delete tuples: tuples that can be completely deleted by the del keyword

this = ("hg Number one", "hg Number two", "hg Number three", "hg Number three", "hg Number three", "hg Number five")
del this

7.3.4 unpacking tuple

(1) When we create a tuple, we usually assign a value to it. This is called a "packed" tuple.
Packing tuple:

fruits = ("apple", "banana", "cherry")
print(fruits)

(2) However, in Python, we can also extract values back into variables. This is called "unpacking".
Unpacking tuple:

fruits = ("apple", "banana", "cherry")
(green, yellow, red) = fruits
print(green)
print(yellow)
print(red)

(3) Use asterisk*
If the number of variables is less than the number of values, you can * add one to the variable name, and the value will be assigned to the variable as a list.
Assign the remaining values to a list named "red":

fruits = ("apple", "banana", "cherry", "strawberry", "raspberry")
(green, yellow, *red) = fruits
print(green)
print(yellow)
print(red)

If an asterisk is added to another variable name instead of the last one, Python assigns a value to the variable until the number of remaining values matches the number of remaining variables.
Add value list "tropic" variable:

fruits = ("apple", "mango", "papaya", "pineapple", "cherry")
(green, *tropic, red) = fruits
print(green)
print(tropic)
print(red)

7.4 cyclic tuple

7.4.1 epoch group

That means traversing tuples. As long as we mention traversal, it is a for loop.
(1) Method 1: direct traversal
An example is as follows: traverse the project and print the value

thistuple = ("hg Number one", "hg Number two", "hg Number three", "hg Number three", "hg Number three", "hg Number five")
for i in thistuple:
    print(i)

(2) Method 2: traverse index numbers
Use the range() and len() functions to create suitable iteratable objects.

thistuple =("hg Number one", "hg Number two", "hg Number three", "hg Number three", "hg Number three", "hg Number five")
for i in range(len(thistuple)):
    print(thistuple[i])

(3) Use While loop

thistuple = ("hg Number one", "hg Number two", "hg Number three", "hg Number three", "hg Number three", "hg Number five")
i = 0
while i < len(thistuple):
  print(thistuple[i])
  i = i + 1

7.5 yuan combination

7.5.1 combination + connection

tuple1 = ("hg Number one", "hg Number two", "hg Number three", "hg Number three", "hg Number three", "hg Number five")
tuple2 = (1, 2, 3)

tuple3 = tuple1 + tuple2
print(tuple3)

7.5.2 * operator can be used to repeat tuple content:

tuple1 =  ("hg Number one", "hg Number two", "hg Number three", "hg Number three", "hg Number three", "hg Number five")
tuple4=tuple1*2
print(tuple4)

7.5.3 count function is used to return the specified number of times:

thistuple = (1, 3, 7, 8, 7, 5, 4, 6, 8, 5)
x = thistuple.count(5)
print(x)

7.5.4 index() finds the specified value and returns its location:

thistuple = (1, 3, 7, 8, 7, 5, 4, 6, 8, 5)
x = thistuple.index(8)
print(x)

Exercises

Q1 - print the first item in the fruits tuple with the correct syntax

fruits = ("apple", "banana", "cherry")
print(fruits[0])

Q2 - print the number of items in the fruits tuple with the correct syntax.

fruits = ("apple", "banana", "cherry")
i=0
while i<len(fruits):
	print(fruits[i])
	i+=1

Q3 - print the last item in a tuple using a negative index.

fruits = ("apple", "banana", "cherry")
print(fruits[-1])

Q4 - print the third, fourth and fifth items in a tuple using a series of indexes.

fruits = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(fruits[2:5])

answer has been debugged in python.

  1. https://chuanchuan.blog.csdn.net/article/details/120419754?spm=1001.2014.3001.5502

  2. Playing with data with Python -- Chinese University mooc

Topics: Python