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 ...
随机推荐
- Ubuntu 启动项、菜单 改动 防止隐藏
因为电脑有多个系统,默认的grub引引导菜单是隐藏的,须要略微改动下方可显示 不要直接改动boot/grub/grub.cfg 要直接改动/etc/default/grub,然后update-gru ...
- 将td中文字过长的部分变成省略号显示的小技巧
首先设置表格的样式table-layout:"fixed"再设置表格的宽度(这步必须) 最后再设置td样式的三个必要属性 代码如下: text-overflow: ellipsis ...
- EasyUI DataGrid可编辑单元格
效果如图: 首先在需要可编辑的列上添加一个editor属性,列定义为numberbox编辑类型 <th field="SCORES" editor="{type:' ...
- css3 一个简单的静态立方体
<meta charset="utf8"> <style> .cube { position: absolute; left: 50%; top: 250p ...
- 从头认识java-17.4 具体解释同步(2)-具体解释竞争条件
这一章节我们来具体讨论一下竞争条件. 1.为什么会引起竞争条件? 因为操作缺失原子性. 2.什么是原子性? 所谓原子操作是指不会被线程调度机制打断的操作:这样的操作一旦開始,就一直运行到结束.中间不会 ...
- vim 命令大全(写的非常全和好)http://blog.csdn.net/woshixiaosimao/article/details/54312738
下文转自链接.由原作者陈皓译自<Learn ViM Progressively>,非常不错. ——————————正文开始—————————— 你想以最快的速度学习人类史上最好的文本编辑器 ...
- 【VBA】制作散点图及打标签VBA
1.散点图的制作必须只选择xy轴对应的数据,不能选中标签. 2.调整xy轴交叉点,改为四个象限. 3.通过菜单移动散点图到新表. 4.运行宏,打上文本标签. VBA如下 Sub AttachLabel ...
- hdu 4067(最小费用最大流)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4067 思路:很神奇的建图,参考大牛的: 如果人为添加t->s的边,那么图中所有顶点要满足的条件都 ...
- Flash Builder 相关
1.Flex SDK 4.1 兼容性 Flex SDK 4.1 兼容 Flash Builder 4.0 ,因此在 Flash Builder 4.0 中使用 4.1 SDK 时可以使用设计视图 Fl ...
- 面试题思考:IO 和 NIO的区别,NIO优点
面试时答: IO是面向流的,NIO是面向缓冲区的 Java IO面向流意味着每次从流中读一个或多个字节,直至读取所有字节,它们没有被缓存在任何地方: NIO则能前后移动流中的数据,因为是面向缓冲区的 ...