Swap 2 Variables in Python
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的更多相关文章
- Mutable and Immutable Variables in Python
本文解决python中比较令人困惑的一个小问题:传递到函数中的参数若在函数中进行了重新赋值,对于函数外的原变量有何影响.看一个小栗子: def fun(a): a=2 return a=1 fun(a ...
- leetcode Swap Nodes in Pairs python
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...
- correct ways to define variables in python
http://stackoverflow.com/questions/9056957/correct-way-to-define-class-variables-in-python later say ...
- Python基础(1)--Python编程习惯与特点
1.代码风格 在Python中,每行程序以换行符代表结束,如果一行程序太长的话,可以用“\”符号扩展到下一行.在python中以三引号(""")括起来的字符串,列表,元组 ...
- Object Oriented Programming python
Object Oriented Programming python new concepts of the object oriented programming : class encapsula ...
- 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 ...
- Python 开发面试题
Python部分 将一个字符串逆序,不能使用反转函数 求从10到100中能被3或5整除的数的和 What is Python? What are the benefits of using Pytho ...
- [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 ...
- Python 2, Python 3, Stretch & Buster
Python 2.7的终止支持时间为2020年,现在已经是2015年了,然而Debian中仍然有大量软件包是基于Python 2的实现.Debian的维护者开始认真讨论淘汰Python 2.开发者Pa ...
随机推荐
- mvn命令若干:
mvn命令若干: mvn -h,不会用时,可寻求帮助. mvn clean compile,将.java类编译为.class文件: mvn clean test, 执行单元测试.本质上,还是执行了一个 ...
- CI 点击图片刷新验证码
<img src="<?php echo site_url('home/login/code'); ?>" onclick= this.src="< ...
- mac上制作u盘启动盘
Mac上制作Ubuntu USB启动盘 一.下载ubuntu iso镜像 二.将iso转换为img文件 $ hdiutil convert -format UDRW -o /path/to/gener ...
- Collection接口与Iterator接口
Collection接口的实现类跟Vector相似.要从实现了Collection接口的类的实例中取出保存在其中的元素对象,必须通过Collection接口的Iterator()方法,返回一个Iter ...
- WebAPI发布IIS报错问题
1.看IIS中处理程序映射中有没有注册:ExtensionlessUrlHandler-Integrated-4.0 没有的话需要在[运行]中注册:aspnet_regiis.exe 2.配置文件中要 ...
- 蓝桥杯 第三届C/C++预赛真题(10) 取球游戏(博弈)
今盒子里有n个小球,A.B两人轮流从盒中取球,每个人都可以看到另一个人取了多少个,也可以看到盒中还剩下多少个,并且两人都很聪明,不会做出错误的判断. 我们约定: 每个人从盒子中取出的球的数目必须是:1 ...
- hdu 3681(bfs+二分+状压dp判断)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3681 思路:机器人从出发点出发要求走过所有的Y,因为点很少,所以就能想到经典的TSP问题.首先bfs预 ...
- uva 1478 - Delta Wave(递推+大数+卡特兰数+组合数学)
option=com_onlinejudge&Itemid=8&category=471&page=show_problem&problem=4224" st ...
- OpenSSL Heart Bleed 如何修复
一 . 前言 这两天这个事件沸沸扬扬啊,有了这个bug黑客在电脑前动动手指就能获取各大电商网站.各大银行用户的用户名和密码了,屌爆了 BUG具体内容 : http://heartbleed.com ...
- 机器学习框架MXnet安装步骤
安装环境:redhat7.1+vmw 安装步骤: # Install git if not already installed. sudo yum -y install git-all# Clone ...