Details of the Python arithmetic operator instance

  • 2020-06-03 06:59:54
  • OfStack

Python arithmetic operator

Assume the following variable a is 10 and b is 20:

运算符 描述 实例
+ 加 - 两个对象相加 a + b 输出结果 30
- 减 - 得到负数或是1个数减去另1个数 a - b 输出结果 -10
* 乘 - 两个数相乘或是返回1个被重复若干次的字符串 a * b 输出结果 200
/ 除 - x除以y b / a 输出结果 2
% 取模 - 返回除法的余数 b % a 输出结果 0
** 幂 - 返回x的y次幂 a**b 输出结果 20
// 取整除 - 返回商的整数部分 9//2 输出结果 4 , 9.0//2.0 输出结果 4.0

The following example illustrates the operation of all the arithmetic operators of Python:


#!/usr/bin/python
 
a = 21
b = 10
c = 0
 
c = a + b
print "Line 1 - Value of c is ", c
 
c = a - b
print "Line 2 - Value of c is ", c
 
c = a * b
print "Line 3 - Value of c is ", c
 
c = a / b
print "Line 4 - Value of c is ", c
 
c = a % b
print "Line 5 - Value of c is ", c
 
a = 2
b = 3
c = a**b
print "Line 6 - Value of c is ", c
 
a = 10
b = 5
c = a//b
print "Line 7 - Value of c is ", c

Output results of the above examples:


Line 1 - Value of c is 31
Line 2 - Value of c is 11
Line 3 - Value of c is 210
Line 4 - Value of c is 2
Line 5 - Value of c is 1
Line 6 - Value of c is 8
Line 7 - Value of c is 2

Thank you for reading, I hope to help you, thank you for your support to this site!


Related articles: