3

Python基础(8)--元组 | CHEGVA

 3 years ago
source link: https://chegva.com/4567.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Python基础(8)–元组

◎知识点

  1. Python元组的概述

  2. 只包含一个元素的元组

◎脚本练习

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@FileName:    tuple_def.py
@Function:    tuple definition
@Author:      Zhihe An
@Site:        https://chegva.com
@Time:        2021/6/23
"""
"""一、元组的概述"""
"""
1、什么是元组?
除了列表,元组也是python语言提供的内置数据结构之一
元组与列表的主要区别:
(1) 元组用小括号表示(列表用中括号表示)
"""
= ('Python'18True)
print(t)    # ('Python', 18, True)
# 小括号是可以省略的
= 'Python'18True
print(t)    # ('Python', 18, True)
# 空元组的两种表示方式
print(())       # ()
print(tuple())  # ()
"""
(2) 元组是不可变类型(列表是可变类型)
为什么要设计元组这样的不可变类型呢?因为一旦创建了不可变类型的对象,对象内部的所有数据
就不能被修改了,这样就避免了由于修改数据导致的错误。此外,对于不可变类型的对象,在多任务环境下
同时操作对象时不需要加锁。因此,在程序中尽量使用不可变类型的对象
"""
= ('Python'18True)
# t[1] = 20   # TypeError: 'tuple' object does not support item assignment
"""
对于元组中可变类型的数据,元组中存储的是其引用(在内存中的地址),因此,
存储的引用是不能被改变的,也就是说,不能再引用任何其它对象。
但是,引用所指向的可变类型的数据是可以改变的
"""
= (5, [13], 8)
# t[1] = 7    # TypeError: 'tuple' object does not support item assignment
t[1][0= 7
print(t)    # (5, [7, 3], 8)
"""
2、只包含一个元素的元组
元组中至少要包含一个逗号,即使元组只有一个元素,
否则,小括号会被看做是数学公式中的小括号
"""
= (18)
print(t)    # 18
print(type(t))  # <class 'int'>
= (18,)
# t = 18,
print(t)    # (18,)
print(type(t))  # <class 'tuple'>

◎脚本地址:https://github.com/anzhihe/learning/blob/master/python/practise/learn-python/python_basic/tuple_def.py

anzhihe安志合个人博客,版权所有丨 如未注明,均为原创 丨转载请注明转自:https://chegva.com/4567.html | ☆★★每天进步一点点,加油!★★☆

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK