当前位置:网站首页>Four methods of exchanging the values of a and B
Four methods of exchanging the values of a and B
2022-07-06 14:38:00 【Wang yeqiang】
In exchange for a,b Value , You may think it's very simple , But if you suddenly think of 4 Methods , Maybe the most basic method of temporary storage with the third variable appears in your mind for a moment , Now I summarize four methods :
initialization a and b:
# About python Replace a b It's worth it Method
a = 3
b = 5
print(" At the beginning of the :a = {} , b = {}".format(a,b))1. Use the third variable c The staging :
def ChangeFirst(a,b): # Take the third variable C The staging
c = a
a = b
b = c
print("ChangeFirst After method exchange :a = {} , b = {}".format(a,b))2. Exchange sum and difference ( The staging ):
def ChangeSecond(a,b): # Use sum and difference
a = a+b
b = a-b
a = a-b
print("ChangeSecond After method exchange :a = {} , b = {}".format(a,b))3. Using bitwise XOR :
def ChangeThird(a,b): # Bitwise XOR ( In binary , Same as 0 Different for 1)
a = a^b
b = a^b
a = a^b
print("ChangeThird After method exchange :a = {} , b = {}".format(a,b))4.Python Proprietary approach :
def ChangeFourth(a,b): #python Proprietary approach
a,b = b,a
print("ChangeFourth After method exchange :a = {} , b = {}".format(a,b))Code runs :
ChangeFirst(a,b)
ChangeSecond(a,b)
ChangeThird(a,b)
ChangeFourth(a,b)Code output :

边栏推荐
- How to earn the first pot of gold in CSDN (we are all creators)
- c语言学习总结(上)(更新中)
- Mysql的事务是什么?什么是脏读,什么是幻读?不可重复读?
- 刷视频的功夫,不如看看这些面试题你掌握了没有,慢慢积累月入过万不是梦。
- Circular queue (C language)
- Harmonyos application development -- address book management system telmanagesys based on listcontainer [phonebook][api v6]
- 安全面试之XSS(跨站脚本攻击)
- 【指针】求字符串的长度
- 《统计学》第八版贾俊平第十四章指数知识点总结及课后习题答案
- New version of postman flows [introductory teaching chapter 01 send request]
猜你喜欢

Apache APIs IX has the risk of rewriting the x-real-ip header (cve-2022-24112)
![Transplant hummingbird e203 core to Da Vinci pro35t [Jichuang xinlai risc-v Cup] (I)](/img/85/d6b196f22b60ad5003f73eb8d8a908.png)
Transplant hummingbird e203 core to Da Vinci pro35t [Jichuang xinlai risc-v Cup] (I)

Statistics 8th Edition Jia Junping Chapter 3 after class exercises and answer summary

JDBC事务、批处理以及连接池(超详细)

Uibutton status exploration and customization

Fundamentals of digital circuit (IV) data distributor, data selector and numerical comparator

《统计学》第八版贾俊平第四章总结及课后习题答案

Intranet information collection of Intranet penetration (2)

captcha-killer验证码识别插件

线程的实现方式总结
随机推荐
How to earn the first pot of gold in CSDN (we are all creators)
《统计学》第八版贾俊平第五章概率与概率分布
Statistics 8th Edition Jia Junping Chapter IX summary of knowledge points of classified data analysis and answers to exercises after class
关于超星脚本出现乱码问题
Intranet information collection of Intranet penetration (I)
《统计学》第八版贾俊平第十一章一元线性回归知识点总结及课后习题答案
Statistics 8th Edition Jia Junping Chapter 3 after class exercises and answer summary
JDBC看这篇就够了
Data mining - a discussion on sample imbalance in classification problems
[pointer] delete all spaces in the string s
Numpy Quick Start Guide
This article explains in detail how mockmvc is used in practical work
5分钟掌握机器学习鸢尾花逻辑回归分类
Function: string storage in reverse order
Hcip -- MPLS experiment
【指针】求字符串的长度
《统计学》第八版贾俊平第九章分类数据分析知识点总结及课后习题答案
On the idea of vulnerability discovery
函数:字符串反序存放
flask实现强制登陆