[Python] Tuples
Python provides another useful built-in type: tuples. Tuples are used to store related pieces of information. Consider this example involving latitude and longitude:
>>> AngkorWat = (13.4125, 103.866667) >>> print(type(AngkorWat)) <class 'tuple'> >>> print("Angkor Wat is at latitude: {}".format(AngkorWat[0])) Angkor Wat is at latitude: 13.4125 >>> print("Angkor Wat is at longitude: {}".format(AngkorWat[1])) Angkor Wat is at longitude: 103.866667
Tuples are similar to lists in that they store an ordered collection of objects which can be accessed by their indexes (for example AngkorWat[0]
and AngkorWat[1]
). Unlike lists, tuples are immutable. You can't add and remove items from tuples, or sort them in place.
Why Tuples?
Why do we have tuples if they're like lists with less features? Tuples useful when you have two or more values that are so closely related that they will always be used together, like latitude and longitude coordinates.
Tuples can be used to assign multiple variables in a compact way:
>>> dimensions = 52, 40, 100 >>> length, width, height = dimensions >>> print("The dimensions are {}x{}x{}".format(length, width, height)) The dimensions are 52x40x100
world_heritage_locations = {(13.4125, 103.866667): "Angkor Wat", (25.73333, 32.6): "Ancient Thebes", (30.330556, 35.4433330): "Petra", (-13.116667, -72.583333): "Machu Picchu"}
Notice that the values assigned to the tuple dimensions
aren't surrounded with parentheses as previous examples were. The parentheses are optional when making tuples, and programmers frequently omit them if parentheses don't clarify the code.
Tuple Unpacking
In the second line, three variables are assigned from the content of the tuple dimensions
. This is called tuple unpacking. You can use tuple unpacking to assign the information from a tuple into multiple variables without having to access them one by one and make multiple assignment statements.
In this example, if we won't need to use dimensions
directly, we could shorten those two lines of code into a single line that assigns three variables in one go!
length, width, height = 52, 40, 100
Returning Tuples
def first_and_last(sequence): """returns the first and last elements of a sequence""" return sequence[0], sequence[-1] >>> first_and_last(["Spam", "egg", "sausage", "Spam"]) ('Spam', 'Spam')
A function that returns a tuple can also be used to assign multiple variables:
>>> start, end = first_and_last(["Spam", "egg", "sausage", "Spam"]) >>> print(start) Spam >>> print(end) Spam
def hours2days(_hours): days = _hours//24 hours = _hours%24 return days, hours hours2days(24) """(1, 0)""" hours2days(25) """(1,1)""" hours2days(10000) """(416, 16)"""
相关文章
- python套接字解决tcp粘包问题
- 解惑Python模块学习,该如何着手操作...
- python实现Linux磁盘查看|清理
- 【转载】如何制作python安装模块(setup.py)
- python后端面试第六部分:git版本控制--长期维护
- Python tkinter库之Canvas 物体沿着函数图像的轨迹移动
- Python print函数及format()格式化输出详解
- python继承代码示例
- 安装python的第三方库 geopandas
- 生成SAP Leonardo API Access Token的python代码
- Atitit. servlet 与 IHttpHandler ashx listen 和HttpModule的区别与联系 原理理论 架构设计 实现机制 java php c#.net js javascript c++ python
- Python编程:Django中间件实现登陆验证
- Python: 矩阵与线性代数运算
- python常用命令