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 ...
随机推荐
- python笔记9 : 多线程
基础: 什么是进程(process)? 每一个程序的内存是独立的,例如:world不能访问QQ. 进程:QQ是以一个整体的形式暴露给操作系统管理,里面包含了各种资源的调用(内存管理.网络接口调用等). ...
- 第二百二十二节,jQuery EasyUI,DataGrid(数据表格)组件
jQuery EasyUI,DataGrid(数据表格)组件 学习要点: 1.加载方式 2.分页功能 本节课重点了解 EasyUI 中 DataGrid(数据表格)组件的使用方法,这个组件依赖于 Pa ...
- java web 页面提速
记得面试现在这份工作的时候,一位领导语重心长地谈道——当今的世界是互联网的世界,IT企业之间的竞争是很激烈的,如果一个网页的加载和显示速度,相比别人的站点页面有那么0.1秒的提升,那也是很大的一个成就 ...
- Origin8.0使用心得(不定时更新)
如何画双Y轴坐标? 网上的教程很多,有点鱼龙混杂的感觉,今天整理一下最简单的方法,备忘. 有3组数据,1组自变量,2组因变量.如何在同一张图下使用同一个X轴,但左边的Y轴与右边的Y轴的刻度不一样? 最 ...
- js 数组求和,多种方法,并比较性能
可以借用下面12种方法对数组求和,创建一个长度为10w的数组,进行测试 every() 检测数值元素的每个元素是否都符合条件. filter() 检测数值元素,并返回符合条件所有 ...
- docker 从容器中拷文件到宿主机器中
sudo docker cp 1d051604e0ea:/root/data /home/developer/zhanghui/data
- WebService之XFire和SOAP实例(基于JAVA)
开发环境:jdk1.6 + Tomcat7 + MyEclipse10 源码下载地址张贴在文章最后面:首先是使用WSDL协议实现:这里使用XFire XFire一个免费.开源的SOAP框架,它构建了P ...
- 习惯养成和目标追踪APP推荐
一.习惯和目标的不同 习惯:贵在坚持,每天任务一定,而完成总量不定.坚持时间越久越好. 目标:贵在按时完成,任务总量一定,但是每天完成量不做限制.有一个完成期限,但是越早越好. 上面的差别导致了相关A ...
- linux禁用触摸板驱动
Method 1: 终端输入如下命令: sudo modprobe -r psmouse 如果打开触摸板就是: sudo modprobe psmouse ------- Method 2: 第一步: ...
- PAT 1007 Maximum Subsequence Sum(最长子段和)
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...