整理Python中的赋值运算符

yipeiwu_com6年前Python基础

下表列出了所有Python语言支持的赋值运算符。假设变量a持有10和变量b持有20,则:

2015513121006816.jpg (592×562)

 例如:

试试下面的例子就明白了所有在Python编程语言可供选择的赋值运算符:

#!/usr/bin/python

a = 21
b = 10
c = 0

c = a + b
print "Line 1 - Value of c is ", c

c += a
print "Line 2 - Value of c is ", c 

c *= a
print "Line 3 - Value of c is ", c 

c /= a 
print "Line 4 - Value of c is ", c 

c = 2
c %= a
print "Line 5 - Value of c is ", c

c **= a
print "Line 6 - Value of c is ", c

c //= a
print "Line 7 - Value of c is ", c

当执行上面的程序,它会产生以下结果:

Line 1 - Value of c is 31
Line 2 - Value of c is 52
Line 3 - Value of c is 1092
Line 4 - Value of c is 52
Line 5 - Value of c is 2
Line 6 - Value of c is 2097152
Line 7 - Value of c is 99864

相关文章

ansible作为python模块库使用的方法实例

前言 ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量...

Opencv+Python实现图像运动模糊和高斯模糊的示例

Opencv+Python实现图像运动模糊和高斯模糊的示例

运动模糊:由于相机和物体之间的相对运动造成的模糊,又称为动态模糊 Opencv+Python实现运动模糊,主要用到的函数是cv2.filter2D(): # coding: utf-...

Python3.5实现的罗马数字转换成整数功能示例

Python3.5实现的罗马数字转换成整数功能示例

本文实例讲述了Python3.5实现的罗马数字转换成整数功能。分享给大家供大家参考,具体如下: 问题概述: 给定一个罗马数字 ,将罗马数字转换成整数。 如罗马数字I,II,III,IV,...

Python内置模块hashlib、hmac与uuid用法分析

本文实例讲述了Python内置模块hashlib、hmac与uuid用法。分享给大家供大家参考,具体如下: 一、hashlib md5和sha算法通过消息摘要算法生成定长的消息摘要,消息...

使用python实现男神女神颜值打分系统(推荐)

使用python实现男神女神颜值打分系统(推荐)

先给大家展示效果图,感觉不错,请参考实现代码。 具体代码如下所示: #!/usr/bin/env python # -*- coding:utf-8 -*- """ pip in...