Data processing in Python (tuple) - (1)

To learn a programming language, you have to know how this program stores data. Only when you understand its storage structure can you perform more in-depth data processing. Let me talk about the three things I learned about Python in the process of learning. A storage structure used in data processing

Python also provides several different storage structures to store our data, they are: tuples, lists and dictionaries

Let's start with tuples , the directory is as follows:

 

Table of contents

1. Tuple

Q1: Generally, when we learn programming, we often encounter a lot of strings, but sometimes when the size of the strings is too large, when we want to find the data we need, but don’t know how to deal with it, what should we do at this time? What about?

Q2: We can use a simple judgment structure to input any number between (1-7), and then output the corresponding day of the week. The program is implemented as follows:

Q3: We know that tuple is a data structure in Python, which can store different data types, such as numbers, characters, etc., but can we operate on the data in the tuple as a whole?

Q4: But sometimes the data we get is not the data we want, so there is a slice operation

Q5: Slicing handles so many numbers, can slices still handle strings?

Q6: It is worth reminding that the data in the tuple in Python is immutable! ! !

Q7: Use tuples to read multiple return values ​​​​in functions

Sum: summary


1. Tuple

Q1: Generally, when we learn programming, we often encounter a lot of strings , but sometimes when the size of the strings is too large, when we want to find the data we need, but don’t know how to deal with it, what should we do at this time? What about? (store data in tuples)

At this time, we need to find a container to store these data, so tuple is such a storage container, let's see how to define a tuple

Look at the following program first, so that we have defined a tuple

 tuple = (10,200)

Tuples are unique to Python. Like most programming languages, tuples can also be compared to arrays in C language. The first element of a tuple starts from 0, which means it represents the first element. Let's see Take a look at this program in interactive mode

Tuples not only support numbers, but also string input!

mytuple = (10,"awesome")
print(mytuple[0])
print(mytuple[1])

 You must have guessed it, the program will output 10 and awesome respectively, but the problem is coming

 

Q2: We can use a simple judgment structure to input any number between (1-7), and then output the corresponding day of the week. The program is implemented as follows:

 

day1 = 'Monday'
day2 = 'Tuesday'
day3 = 'Wednesday'
day4 = 'Thursday'
day5 = 'Friday'
day6 = 'Saturday'
day7 = 'Sunday'
x = int(input('Please input a number:'))

if x == 1:
    print(day1)

elif x==2:
    print(day2)

elif x==3:
    print(day3)

elif x == 4:
    print(day4)

The result of the program running is as follows:

 

But such a long code obviously looks redundant and takes up a lot of resources. The code that does not meet the requirements of our program should be kept concise.

So, at this time, the tuple can play its role. We use a tuple to put the seven days of our week, then input one of the days, and then use the output function to print it.

days = ('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday')

x = int(input('please input a number:'))

print(days[x-1])#没忘记吧,元组是从0开始的

The running result of the program here is the same as above, this is the magic

 

Q3 : We know that tuple is a data structure in Python, which can store different data types, such as numbers, characters, etc., but can we operate on the data in the tuple as a whole?

 We can use the print function to print out all the elements in the tuple, and we can also use a loop to print out each individual element

mytuple = (1,'Hello','达瓦达瓦')
print(mytuple)

for x in mytuple:#将元组中的每一个数据遍历一遍,在打印下来,也就是用for循环来使用元组中的数据
    print(x)

program running result

 

Q4: But sometimes the data we get is not the data we want, so there is a slice operation

 

方法一
mytuple = (1,122,32,3434,444,1000,4443,9000)

for x in mytuple[0:3]:#不打印第三个元素,方括号中前一个数字代表开始的,第二个代表结束的数字
    print(x)

方法二
for i in mytuple[:3]: #告诉编辑器只显示前三个元素
    print(i)

 These two operations are equivalent, and the running result

 

One more point here, if we want to print the last element directly

#这样直接打印前面我们定义的元组中的最后一个数
print(mytuple[-1])

 

Here is another slice operation

mytuple = (1,122,32,3434,444,1000,4443,9000)
for t in mytuple[3:]:  #这次的循环是跳过前三个元素,直接打印后面的元素
    print(t)

operation result

 

 Q5: Slicing handles so many numbers, can slices still handle strings?

 The answer is yes, see the following program

mytuple1 = 'Python is totally awesome!'
for i in mytuple1[::2]:
    print(i)

 Here we add a third parameter to the slice, its actual function is to skip two elements each time it loops through the elements

The following is the result of running the program

 

Q6:  It is worth reminding that the data in the tuple in Python is immutable! ! !

 Let's look at the program first

mytuple3 = (3,2,'222')
mytuple3[0] = 24

At this time, the program will report  an error

At this time, the editor tells us that tuple (tuple) does not support this task, which means assigning values  ​​to the data in the tuple .

Therefore, when using the data structure in Python to store data, it is necessary to clearly distinguish : tuples, lists, and dictionaries work

 

Q7: Use tuples to read multiple return values ​​​​in functions

The definition function my_func  has multiple return values, and then we can store these values ​​in the tuple ( mytuple ) we defined, and then we can print the function return value we need!

def my_func():
    return (11,22,33,44,55,66)

mytuple = my_func()
print(mytuple[0])
print(mytuple[1]) #访问特定位置的元素
print(mytuple)  #直接访问返回的所有值

program running result

 

Sum: summary

 

Tuples in Python support us to store different types. In the process of reading data, we need to use slice operations. Three values ​​can be placed in the slice position, the first one is the value of the starting element, and the middle one is The position where the end element is placed. The last position is generally used in the loop. Its function is how many elements to skip after each loop. The function of the tuple is much more than that. It can also store multiple return values. function

Guess you like

Origin blog.csdn.net/caidewei121/article/details/85850724