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. J2EE之Servlet初见

    Servlet是J2EE12种规范之中的一个.它也是用java语言编写的程序,其本身也是一种JAVA类,在须要的时候被实例化,不须要的时候自己主动销毁,Servlet的执行是在Servlet容器内执行 ...

  2. SIGBUS 和 SIGSEGV

    一.导致SIGSEGV      1.试图对仅仅读映射区域进行写操作 . 2.訪问的内存已经被释放,也就是已经不存在或者越界. 3.官方说法是: SIGSEGV --- Segment Fault. ...

  3. uva11383 Golden Tiger Claw 深入理解km算法

    /** 题目: uva11383 Golden Tiger Claw 深入理解km算法 链接:https://vjudge.net/problem/UVA-11383 题意:lv 思路:lrj训练指南 ...

  4. edmx-新建表

  5. 【BZOJ】3402: [Usaco2009 Open]Hide and Seek 捉迷藏(spfa)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3402 又是spfa水题.. #include <cstdio> #include < ...

  6. 蓝桥杯 第三届C/C++预赛真题(5) 转方阵(C基本功)

    对一个方阵转置,就是把原来的行号变列号,原来的列号变行号 例如,如下的方阵: 1 2 3 4 5 6 7 8 9 10 11 1213 14 15 16 转置后变为: 1 5 9 13 2 6 10 ...

  7. cout顺序,i++和++i

    先看下以下代码 #include<iostream> using namespace std; ; int f1() { x = ; return x; } int f2() { x = ...

  8. 033 调整数组顺序使奇数位于偶数前面(keep it up)

    剑指offer中题目:http://ac.jobdu.com/problem.php?pid=1516 题目描写叙述: 输入一个整数数组,实现一个函数来调整该数组中数字的顺序.使得全部的奇数位于数组的 ...

  9. springmvc的一个错误

    初学ssm,碰到一个错误,非注解的就没错,注解的就错了 找了半天,终于发现错误,真的很傻比啊,导入的springframework版本不一致... 都导入4.3.18版本的即可...

  10. bootstrap Table API和一些简单使用方法

    官网: http://bootstrap-table.wenzhixin.net.cn/zh-cn/documentation/ 后端分页问题:后端返回”rows”.“”total,这样才能重新赋值 ...