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 ...
随机推荐
- ajax 传递参数中文乱码解决办法
/********Start***********/ /*获取地址栏参数*/ function getRequest(){ var url = location.search; //获取url中&qu ...
- ubuntu16.0.4 update git
Ubuntu 16.04 comes with Git 2.7.x, which is a little old now. As versions 2.8 & 2.9 are not part ...
- 如何Vue-cli开始使用在Vue.js项目中启动TDD(测试驱动开发)
通常,使用测试驱动开发(TDD)最困难的部分是开始.你必须下载带有奇怪依赖项的软件包,让测试套件与你的构建系统协同工作,然后你必须弄清楚如何编写一个测试!难怪这么多的开发者在你提起它的时候就开始跑开了 ...
- 第二百三十七节,Bootstrap图标菜单按钮组件
Bootstrap图标菜单按钮组件 学习要点: 1.小图标组件 2.下拉菜单组件 3.按钮组组件 4.按钮式下拉菜单 本节课我们主要学习一下 Bootstrap 的三个组件功能:小图标组件.下拉菜单组 ...
- String类的常用成员方法
1. 构造方法: String(byte[] byte,int offset,int length);这个在上面已经用到. 2. equalsIgnoreCase:忽略大小写的比较,上例中如果您输 ...
- Accept-Encoding 使用
[总结] 想要获得正确网页内容,而非乱码的话,就有两种方式了: 1.不要设置Accept-Encoding的Header //req.Headers.Add("Accept-Encoding ...
- WPF 开源项目 【watcher】 守望者,一款监控,统计,分析你每天在自己电脑上究竟干了什么的软件
时隔多年(两年),天天沉迷写PHP的我在连续加薪了逐渐发现自己不怎么写代码了. 甚至有一天我发现我连IDE 都没有打开,实在是太堕落了 为了及时悬崖勒马,回头是岸.为了鼓励自己专心写代码,我决定写一款 ...
- FireBug与FirePHP
a.FireBug,平时用的比较多.就是在客户端调试代码.如:hTML ,CSS,JS等 b.FireBug安装较容易. b-1,打开火狐浏览器 b-2,打开“附加组件” b-3.搜索“firebug ...
- JavaScript修改IE注册表
http://www.cnblogs.com/zmc/p/3373812.html <script type="text/javascript"> var obj = ...
- flask配置加载几种方式
方法一.直接配置 app.config['HOST']='xxx.a.com' print(app.config.get('HOST')) 方法二.通过环境变量加载配置 环境变量:export MyA ...