14.python tuple tuple - Basics python

    In the previous article, we explained about the python list List related content, today explain to you a list of List of brothers - tuple, commonly known as: tuple.

brothers

 

    Tuple tuple and list List is similar to the tuple has the following characteristics:

    1 is constituted by one or more data, the same types of data may not be the same;

    2. The data tuple needs to write () between the interior brackets, the data with the data separated by a comma;

    3. A tuple is an ordered set of default weight subscript index zero, and the like strings;

    4. The data tuple can not be modified

Tuple

     Tuple in fact, called read-only list, the list of supported functions also support tuples, the only difference is the data tuple in the tuple can not be modified, which means that you can not delete data tuples in the tuple, we can not be directly data tuples in the tuple assignment.

 

A. Tuple defined tuple

# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:何以解忧
@Blog(个人博客地址): shuopython.com
@WeChat Official Account(微信公众号):猿说python
@Github:www.github.com
 
@File:python_tuple.py
@Time:2019/9/26 20:45
 
@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
"""
 
tuple1 = tuple() # 定义一个空元组,元组的数据不能修改,意味永远都是一个空元组
print(tuple1)
print(type(tuple1)) # 获取数据类型
 
tuple2 = ("python","study") # 定义元组tuple2 ,该元组由两个字符串数据构成
print(tuple2)
 
tuple3= ("python","s",False,2.5) # 定义元组tuple3 ,该元组中的数据可以由不同类型的数据构成
print(tuple3)

    输出结果:

 

二.元组tuple查询

    元组tuple的查询和列表list的操作类似,同样也可以直接通过下标查询元组中的数据,演示代码如下:

    输出结果:

 

三.元组tuple不支持删除/修改数据

    元组tuple中的数据只能读取,不能修改也不能删除,如果对元组tuple中的数据删除或者修改会报错,代码演示:

    编译器会报错:TypeError: ‘tuple’ object does not support item assignment(翻译:元组tuple不支持修改)

 

四.元组tuple与列表list的相互转换

    两者之间直接强制转换即可,演示代码如下:

    输出结果:

 

五.重点总结

    1.注意元组tuple与列表list的区别,元组的数据不能被修改,其他使用和列表一样。

    2.注意元组tuple/列表list/字符串str三者的写法区别:

 

猜你喜欢:

    1.python字符串

    2.pycharm设置字体颜色/模板头文件

    3.python列表list

 

    转载请注明:猿说Python » python元组tuple

 

技术交流、商务合作请直接联系博主
Scan code or search: ape say python
No public python tutorial
Ape say python
No. sweep the micro-channel public concern

Guess you like

Origin www.cnblogs.com/shuopython/p/11704359.html