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. JS——简单的正则表达式验证

    <!-- 用户注册:结构层:html;表现层:css;行为层:javascript; html利用ul,li来构造: 注意事项:1.每个Input都要有相应的id,这是在js中去调用的. 2.& ...

  2. redis-3.0.6安装

    此redis用来缓存跨屏账户绑定信息,安装步骤如下: ssh root@redis.td.com ,注意是root用户 tar -xzvf /nfs/install/softs/redis-3.0.4 ...

  3. 大数据(2) - Hadoop完全分布式的部署

    apache hadoop 官方文档 ** Hadoop介绍 ** HDFS:分布式存储文件 角色:NameNode和DataNode ** YARN:分布式资源调度框架(Hadoop2.x以上才引用 ...

  4. eclipse JavaEE版"javax.servlet.http.HttpServlet" was not found on the Java Build Path问题的解决办法

    使用eclipse JavaEE 版,新建 Dynamic Web Project 项目.在项目里添加 JSP 文件,会在文件头部出现错误提示.提示语句为:The superclass "j ...

  5. 【Mac + Python + Selenium】之PyCharm配置Selenium自动化

    一.安装PyCharm 1.下载地址: Pycharm编辑器官网下载地址 2.安装完成之后打开,一路下去点击,直到填写注册码,参考: <[转载][Pycharm编辑器破解步骤]之idea和Pyc ...

  6. 在grub的rescue模式修复linux引导

    今天在windows 10系统收到系统更新通知,没看清楚就手贱点了马上更新.以为只是像那些普通更新一样重启一下更新就完了,万万没想到这个是覆盖更新,也就是说这是一个全新的系统更新而不是系统补丁.在安装 ...

  7. easyui- grid前台分页

    function pagerFilter(data) { if (typeof data.length == 'number' && typeof data.splice == 'fu ...

  8. C++ 类的多态一(virtual关键字--构造函数深刻理解)

    //virtual关键字--构造函数深刻理解 #include<iostream> using namespace std; /* C语言编译器,c++编译器全部是静态链编,就是一段一段代 ...

  9. redis 连接池

    redis是一个key-value存储系统,和memcached类似,支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted set-有 ...

  10. 【BZOJ】3412: [Usaco2009 Dec]Music Notes乐谱(二分)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3412 维护前缀和,然后直接二分即可... #include <cstdio> #incl ...