python a basic data type (tuples)

Scratch. Ganso

1. For the container-type data type list, no matter who it may be CRUD, then there are some important data on the list is not secure, the data type requires a container class storage of important data, create it early additions and deletions can only be viewed but not this type of data is Ganso.

Ganso: commonly known as immutable list, and the list is read-only, one of the basic data types Ganso is the python,

Small brackets, which can put any data type, query, it can also cycle, can also be sliced, but just can not change. Keywords are tuple in python

tu = ('我','怎么','这么','可爱')

tu1 = tu[0]  # 记性下标
print(tu1)

for i in tu:
    print(i)  # 进行for循环

tu2 = tu[0:3]
print(tu2)  # 进行切片

结果:
Traceback (most recent call last):
  File "D:/python_object/path2/test.py", line 1286, in <module>
    tu[0] = '你'
NameError: name 'tu' is not defined

About immutable, Note: this tuple is immutable means immutable sub-elements and sub-elements within sub-elements can be changed, depending on whether the child element is a variable object.

Tuples if only one element. Be sure to add a comma, otherwise it is not a tuple

tu = ('meet')
print(type(tu))  #type是查看数据类型

结果:
<class:str>

tu = ('meet',)
print(type(tu))  #type是查看数据类型

结果:
<class:tuple>

How to use this knowledge

1. traversable

2. can be sliced

3. There len, count, index method

Ganso nesting  

tu = ('今天姐姐不在家','姐夫和小姨子在客厅聊天',('姐夫问小姨子税后多少钱','小姨子低声说道说和姐夫还提钱'))
tu1 = tu[0]
tu2 = tu[1]
tu3 = tu[2][0]
tu4 = tu[2][1]

print(tu1)
print(tu2)
print(tu3)
print(tu4)
结果:
今天姐姐不在家
姐夫和小姨子在客厅聊天
姐夫问小姨子税后多少钱
小姨子低声说道说和姐夫还提钱

Where to use

It is to some very important people can not change the data in Ganso, the viewable only. Late write your project when there will be profiles of individual variables did not want people to modify the configuration file using constants, if more people do not want to modify is to use tuples to store

Two .range

Translates range, then we look at me.

range(0,5,1)

参数第一个是范围的起始位置
参数第二个是范围的结束位置
参数第三个是步长
print(range(0,5))
# 结果:
range(0, 5)  #一个范围
# 我们可以通过list方法来转换这个范围来查看一下
l = list(range(0,5))
print(l)

# 结果:
[0, 1, 2, 3, 4]
l = list(range(0,5,2))
print(l)
# 结果:
[0, 2, 4]   # 这个结果就会发现和我之前用步长获取的内容是相似的,是的他就是步长
  • Range is characteristic care regardless tail

  • The difference python2 and python3

    print(range(0,10))
    #python2中直接返回列表,xrange()和python3中的range相似
    #python3中作为迭代对象,怎么写就怎么打印,可以使用list()进行转换
  • range syntax

    range(0,10)#起始位置和终止位置,起始位置是0时可以默认不写
    range(10,1,-1)#也可以从大到小生成数据,
    range(1,100,2)#也可以用来生成奇数和偶数等
  • Common Problems

    lst = []
    for i in lst:
        lst.append("meet")
        print(lst)  # 不会打印内容  因为lst是空的
    
    lst = [1,2]
    for i in lst:
        lst.append("meet")
        print(lst) # 循环打印lst中的内容 -- 此循环是死循环
    
    lst = [1,2]
    for i in lst:
        lst.append("meet")
    print(lst)  # 死循环 -- 不会打印内容
    
    lst = []
    for i in range(5):
        lst.append([])
    print(lst)  # [[],[],[],[],[]]
    
    lst = [1,2]
    lst[0] = lst
    print(lst)  # [[...],2]

Guess you like

Origin www.cnblogs.com/luckinlee/p/11619855.html