Python basic data types (6)

A word of positive energy every day

There are a few things to do in your life: take care of your body. Second, manage your emotions and think positively. 3. Serve your family well and make your family live a happy life. Fourth, do your job well, and do one or two things that are particularly perfect and earth-shattering. Make a group of sincere friends with similar values. 6. Help others as much as possible.

In my life, when I had the opportunity to choose, I chose to stay away from home, I chose my work, my show and my love. I thought it was freedom. However, I never felt relaxed, like a person dancing in shackles, never leaving the square.

Trekking through the years, everyone has their own stories. Only when you look down on your mood can you be beautiful, and only when you look away can your emotions be bright. Take a break when you are tired, dance with the breeze, be quiet when you are bored, stare at the flowers and plants, slow down when you are in a hurry, and smile with yourself.

Science can not only "give knowledge to the young and happiness to the old", it can also make people accustomed to labor and the pursuit of truth, create real spiritual and material wealth for the people, and create things that cannot be obtained without it .

Sometimes, we will be in a confused and unknown future, at a loss, wait a moment, the next second, there will be a broad road; There are clouds and the moon is bright; the life of each of us is destined to not be smooth sailing, but I firmly believe that in the next second of everyone's life, there will be unexpected warmth and endless expectations.

Whether or not many things are worth doing does not depend on how others see, say, or think. No matter how experienced and experienced they are, with high IQ and EQ, they cannot make decisions for you. Oftentimes, things that seem insignificant to others are extremely important to you. Therefore, whether to do one thing or not, ask yourself. If you can’t fall asleep if you don’t do it, you will regret it, so you have to do it.

Tuple

A tuple is similar to a list, except that the elements of a tuple cannot be modified. Tuples are written in parentheses (), and elements are separated by commas.

The element types in the tuple can also be different:

#!/usr/bin/python3

tuple = ( 'abcd', 786 , 2.23, 'runoob', 70.2  )
tinytuple = (123, 'runoob')

print (tuple)             # 输出完整元组
print (tuple[0])          # 输出元组的第一个元素
print (tuple[1:3])        # 输出从第二个元素开始到第三个元素
print (tuple[2:])         # 输出从第三个元素开始的所有元素
print (tinytuple * 2)     # 输出两次元组
print (tuple + tinytuple) # 连接元组

The output of the above example:
insert image description here
tuples are similar to strings and can be indexed and the subscript index starts from 0, and -1 is the position from the end. Interception can also be performed (see above, no more details here).

In fact, strings can be thought of as a special kind of tuple.

>>> tup = (1, 2, 3, 4, 5, 6)
>>> print(tup[0])
1
>>> print(tup[1:5])
(2, 3, 4, 5)
>>> tup[0] = 11  # 修改元组元素的操作是非法的
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>>

Although the elements of a tuple are immutable, it can contain mutable objects, such as lists.

Constructing tuples with 0 or 1 elements is special, so there are some additional syntax rules:

tup1 = ()    # 空元组
tup2 = (20,) # 一个元素,需要在元素后添加逗号

string, list and tuple all belong to sequence (sequence).

Notice:

  • 1. Like strings, elements of tuples cannot be modified.
  • 2. Tuples can also be indexed and sliced ​​in the same way.
  • 3. Pay attention to the special syntax rules for constructing tuples containing 0 or 1 elements.
  • 4. Tuples can also be concatenated using the + operator.

epilogue

  Everyone is a novice when they first start learning. During this process, they will more or less read blogs written by others, and they may step on many pitfalls in the process. When encountering problems, we can record them down, whether it is very helpful to ourselves or other learning partners. Most programmers are very lonely, writing can also let their loneliness "place", express more, express more, there are always many benefits for programmers who are not good at words, the most important thing in the process of blog writing The thinking is clear and organized. The biggest difference from typing codes is that programming has a clear goal, and you will become a master day after day. If you write just for writing, you will often lose your original intention and true nature. Writing can not only improve verbal expression skills, but also cultivate the perseverance to continue doing things. It is best to meet many like-minded friends.

Reprinted from: https://blog.csdn.net/u014727709/article/details/131380557
welcome to start, welcome to comment, welcome to correct

Guess you like

Origin blog.csdn.net/u014727709/article/details/131380557