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. spring cloud 启动报错问题 Input length = 1

    Caused by: org.yaml.snakeyaml.error.YAMLException: java.nio.charset.MalformedInputException: Input l ...

  2. 打开.py文件的方法

    用IDLE打开这个文件,然后按F5,系统就自动开始运行这个python程序,然后当前运行目录就跳转到这个目录了

  3. jquery.js:8672 Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/.

    html5谷歌流浪器报错:jquery.js:8672 Synchronous XMLHttpRequest on the main thread is deprecated because of i ...

  4. sql server 2008安装要求

    sql server 2008安装要求 新部署的R2需要被安装在格式化为NTFS格式的磁盘上: 微软的.NET Framework 3.5 SP1 微软Windows Installer 4.5或以上 ...

  5. 上下居中css

    .css{ position: relative, top: 50%, transform: translateY(-50%) }

  6. 【IOS】 readonly IOS下实战入门

    当非常多XXX.h 文件属性YY设计中,对外的设计是 YY(readonly) 时.外界便不能改动该属性, 同一时候问题也来了,该XXX类的内部也不能改动改YY属性了.你瞬间凌乱过么. ..Y_Y 然 ...

  7. K-Means算法Demo

    简介:本Demo是参照这个网站上的Demo自己用Java实现的.将Java打包为Jar,再将Jar转为exe,源代码及程序Demo下载请点我. K-Means算法简介 我尽量用通俗易懂但不规范的语言来 ...

  8. structure machine learning projects 课程笔记

    orthogonalization/ one metric train.dev/test 划分 开发集和测试集一定来自同一分布  onthe  same distribution Human leve ...

  9. 基础-Eclipse 教程

    1.Eclipse 是一个开放源代码的.基于 Java 的可扩展开发平台.2.下载地址为: https://www.eclipse.org/downloads/.3.Eclipse 修改字符集 : W ...

  10. 【BZOJ3522】[Poi2014]Hotel 树形DP

    [BZOJ3522][Poi2014]Hotel Description 有一个树形结构的宾馆,n个房间,n-1条无向边,每条边的长度相同,任意两个房间可以相互到达.吉丽要给他的三个妹子各开(一个)房 ...