It is often useful to swap the values of two variables. With conventional assignments, you have to use a temporary variable. This solution is cumbersome; tuple assignment is more elegant:

The left side is a tuple of variables; the right side is a tuple of expressions. Each value is assigned to its respective variable. All the expressions on the right side are evaluated before any of the assignments. The number of variables on the left and the number of values on the right have to be the same:

More generally, the right side can be any kind of sequence (string, list or tuple). For example, to split an email address into a user name and a domain, you could write:

from Thinking in Python

Tuple assignment的更多相关文章

  1. 《python解释器源码剖析》第5章--python中的tuple对象

    5.0 序 我们知道对于tuple,就相当于不支持元素添加.修改.删除等操作的list 5.1 PyTupleObject对象 tuple的实现机制非常简单,可以看做是在list的基础上删除了增删改等 ...

  2. [python] File path and system path

    1. get files in the current directory with the assum that the directory is like this: a .py |----dat ...

  3. Think Python - Chapter 12 Tuples

    12.1 Tuples are immutable(元组是不可变的)A tuple is a sequence of values. The values can be any type, and t ...

  4. Think Python Glossary

    一.The way of the program problem solving: The process of formulating a problem, finding a solution, a ...

  5. Python in minute

    Python 性能优化相关专题:    https://www.ibm.com/developerworks/cn/linux/l-cn-python-optim/   Python wikipedi ...

  6. The Go Programming Language. Notes.

    Contents Tutorial Hello, World Command-Line Arguments Finding Duplicate Lines A Web Server Loose End ...

  7. [Python] 07 - Statements --> Functions

    故事背景 一.阶级关系 1. Programs are composed of modules.2. Modules contain statements.3. Statements contain ...

  8. 10 The Go Programming Language Specification go语言规范 重点

    The Go Programming Language Specification go语言规范 Version of May 9, 2018 Introduction 介绍 Notation 符号 ...

  9. Python中的赋值语法

    Python中复制语法有6种 Basic Form >>>spam = 'spam' Tuple assignment >>>spam, ham = 'spam', ...

随机推荐

  1. POJ 1942

    开始时竟然用了分情况讨论. 仔细思考一下,哈哈,发现不过是多重集合的组合数而已. #include <iostream> #include <cstdio> #include ...

  2. CF 447B(DZY Loves Strings-贪心)

    B. DZY Loves Strings time limit per test 1 second memory limit per test 256 megabytes input standard ...

  3. 王立平--自己定义TitleBar

    效果: 1.自己定义titleBar的布局. <?xml version="1.0" encoding="utf-8"?> <Relative ...

  4. 三种SVM的对偶问题

    一.SVM原问题及要变成对偶问题的解决办法 对于SVM的,我们知道其终于目的是求取一分类超平面,然后将新的数据带入这一分类超平面的方程中,推断输出结果的符号,从而推断新的数据的正负. 而求解svm分类 ...

  5. NOIP2017提高组模拟赛 7(总结)

    NOIP2017提高组模拟赛 7(总结) 第一题 斯诺克 考虑这样一个斯诺克球台,它只有四个袋口,分别在四个角上(如下图所示).我们把所有桌子边界上的整数点作为击球点(除了4个袋口),在每个击球点我们 ...

  6. 2.windows下安装git

    转自:https://blog.csdn.net/lvkelly/article/details/54666868

  7. xBIM 高级03 更改日志创建

    系列目录    [已更新最新开发文章,点击查看详细]  模型中发生的每一个变化都是事务的一部分,这是我们设计的核心.所有事务都是由 IModel 的实现创建的,并且从中被弱引用,因此当使用 using ...

  8. ImageUtils

    JavaSE package com.easystructure.utils.system; import java.awt.Color; import java.awt.Font; import j ...

  9. 关于 jsp java servlet 中文汉字乱码的解决方法

    在servlet类中的get,post最前面加上 req.setCharacterEncoding("UTF-8"); resp.setCharacterEncoding(&quo ...

  10. Servlet学习(四)——response

    1.概述 在创建Servlet时会覆盖service()方法,或doGet()或doPost(),这些方法都有两个参数,一个是代表请求的request和代表响应response. service方法中 ...