In Python, it's concise, easy and faster to swap 2 variables compared in other Programming languages:

Python:

x, y = y, x

Other programming languages:

temp = x
x = y
y = temp

Actually, we can also use the second method just like the other programming languages, but it's

slower than the firt method. Why? Let's take a look at the following codes:

>>> x = 1
>>> y = 2
>>> x
1
>>> y
2
>>> id(x)
160123056
>>> id(y)
160123044
>>> x, y = y, x
>>> x
2
>>> y
1
>>> id(x)
160123044
>>> id(y)
160123056
>>> x = 1
>>> y = 2
>>> x
1
>>> y
2
>>> id(x)
160123056
>>> id(y)
160123044
>>> temp = x
>>> id(temp)
160123056
>>> x = y
>>> y = temp
>>> id(x)
160123044
>>> id(y)
160123056
>>> x
2
>>> y
1

As we can see the codes above, the second method involves a new variables 'temp' while the first method not,

so the second method is slower than the first method.

Swap 2 Variables in Python的更多相关文章

  1. Mutable and Immutable Variables in Python

    本文解决python中比较令人困惑的一个小问题:传递到函数中的参数若在函数中进行了重新赋值,对于函数外的原变量有何影响.看一个小栗子: def fun(a): a=2 return a=1 fun(a ...

  2. leetcode Swap Nodes in Pairs python

    # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...

  3. correct ways to define variables in python

    http://stackoverflow.com/questions/9056957/correct-way-to-define-class-variables-in-python later say ...

  4. Python基础(1)--Python编程习惯与特点

    1.代码风格 在Python中,每行程序以换行符代表结束,如果一行程序太长的话,可以用“\”符号扩展到下一行.在python中以三引号(""")括起来的字符串,列表,元组 ...

  5. Object Oriented Programming python

    Object Oriented Programming python new concepts of the object oriented programming : class encapsula ...

  6. Working with Python subprocess - Shells, Processes, Streams, Pipes, Redirects

    Posted: 2009-04-28 15:20 Tags: Python Note Much of the "What Happens When you Execute a Command ...

  7. Python 开发面试题

    Python部分 将一个字符串逆序,不能使用反转函数 求从10到100中能被3或5整除的数的和 What is Python? What are the benefits of using Pytho ...

  8. [Python] Use Static Typing in Python 3.6

    In this lesson, you will learn how to statically type variables in Python 3.6 Static typing can help ...

  9. Python 2, Python 3, Stretch & Buster

    Python 2.7的终止支持时间为2020年,现在已经是2015年了,然而Debian中仍然有大量软件包是基于Python 2的实现.Debian的维护者开始认真讨论淘汰Python 2.开发者Pa ...

随机推荐

  1. linux 文件夹的颜色代表什么意思

    linux 文件夹的颜色代表什么意思 绿色 蓝色 黑色代表什么意思 蓝色表示目录: 绿色表示可执行文件: 红色表示压缩文件: 浅蓝色表示链接文件: 灰色表示其它文件: 红色闪烁表示链接的文件有问题了: ...

  2. 微信小程序3 - 对象的合并

    ES6中 Object.assign方法用于对象的合并,将源对象( source )的所有可枚举属性,复制到目标对象( target ). 限制:   只是浅拷贝, 即 内部对象 不会拷贝,只是 引用 ...

  3. ssd算法的pytorch实现与解读

    首先先放下github地址:https://github.com/acm5656/ssd_pytorch 然后放上参考的代码的github地址:https://github.com/amdegroot ...

  4. 动态添加js的方法

    var Skip={};//获取XMLHttpRequest对象(提供客户端同http服务器通讯的协议)Skip.getXmlHttpRequest=function (){ if ( window. ...

  5. 例举在诊断Oracle性能问题时,常用的工具、方法

    例举在诊断Oracle性能问题时,常用的工具.方法 解答: 1)简单一点的可以用toad及dbartisan这样的工具. 2)纯做性能监测,比较出色的有spolight和emc的I3,这两个软件都比较 ...

  6. python 多线程糗事百科案例

    案例要求参考上一个糗事百科单进程案例 Queue(队列对象) Queue是python中的标准库,可以直接import Queue引用;队列是线程间最常用的交换数据的形式 python下多线程的思考 ...

  7. IPMI特点和功能

    IPMI独立于操作系统外自行运作,并容许管理者即使在缺少操作系统或系统管理软件.或受监控的系统关机但有接电源的情况下仍能远程管理系统. ipmi可以实现对机器的操作举例如下: 开机,关机,重启,查看机 ...

  8. App上传到应用宝的一些问题

    问题:提示应用需要认领,怎么解决? 原因:如果app之前在其他市场上传过,再上传到应用宝,应用宝首先会从其他应用市场抓包,如果发现抓取的包和上传的app包名都是一致的,这时候提示你需要认领app. 操 ...

  9. 面试题思考:interface和abstract的区别

    抽象类(abstract) 含有abstract修饰符的class即为抽象类,abstract 类不能创建的实例对象. 含有abstract方法的类必须定义为abstract class,abstra ...

  10. Eclipse获取签名证书的SHA1

    签名后的获取方式 :http://jingyan.baidu.com/article/3ea51489da3bc252e71bba59.html 未签名的获取方式:http://www.th7.cn/ ...