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. java.awt包提供了基本的java程序的GUI设计工具

    java.awt包提供了基本的java程序的GUI设计工具.主要包括下述三个概念: 组件--Component 容器--Container 布局管理器--LayoutManager package T ...

  2. 推荐linux命令在线查,简约而不简单

    1.相关介绍: 网址:http://blog.51yip.com/linux/1518.html#more-1518 2.Linux 命令在线sce 网址:http://linux.51yip.com ...

  3. 【Raspberry pi】python ide-spyder

    sudo apt-get install spyder 简单 明了

  4. VC++ 带界面的ActiveX控件

    一.新建MFC ActiveX工程OleHasInterface: 二.新建一个对话框资源,ID为 IDD_FORMVIEW,关联类CActXInterfaceDlg,基类CDialog: 三.设计对 ...

  5. hdu4456 Crowd(二维树状数组)

    题意:给出一个n*n的矩阵,然后m个operation,1表示坐标(x,y)的值加z,2表示与坐标(x,y)的曼哈顿距离不超过z的点的权值和. 解题思路:将矩阵側过来45度.发现询问的时候,有效的点构 ...

  6. hiho一下第109周《Tower Defense Game》

    题目链接:传送门 题目大意:给你一棵树,根节点为1,树上每一个节点都有一个花费值和收入值(花费值>=收入值),要访问一个节点需先支付花费值,访问该节点结束后得到收入值 同时访问树时要求是有序的, ...

  7. 【BZOJ4550】小奇的博弈 博弈论

    [BZOJ4550]小奇的博弈 Description 这个游戏是在一个1*n的棋盘上进行的,棋盘上有k个棋子,一半是黑色,一半是白色.最左边是白色棋子,最右边是黑色棋子,相邻的棋子颜色不同.   小 ...

  8. 【BZOJ4503】两个串 FFT

    [BZOJ4503]两个串 Description 兔子们在玩两个串的游戏.给定两个字符串S和T,兔子们想知道T在S中出现了几次, 分别在哪些位置出现.注意T中可能有“?”字符,这个字符可以匹配任何字 ...

  9. E71自带铃声下载

    NOKIA-e71 E71系统自带铃声下载,有几个听着不错~ E71铃声下载

  10. 高性能Web开发系列

    1. 高性能WEB开发基础 http://www.uml.org.cn/net/201404225.asp 2. 高性能WEB开发进阶(上) http://www.uml.org.cn/net/201 ...