Home  >  Article  >  Backend Development  >  Detailed explanation of examples of arithmetic operators in Python

Detailed explanation of examples of arithmetic operators in Python

黄舟
黄舟Original
2017-06-04 10:19:491798browse

This article mainly introduces the relevant information of Pythonarithmetic operatorsdetailed examples. Friends in need can refer to

Python arithmetic operators

The following assumes that variable a is 10 and variable b is 20:

Part9//2 Output result 4, 9.0//2.0 Output result 4.0##The following The output result of the above example:
Operator Description Instance
+ Add - Add two objects Adding a + b outputs the result 30
- minus-gets a negative number or one number minus another number a - b outputs the result-10
* Multiplication - Multiply two numbers or return a string repeated several times a * b Output result 200
/ divided - x divided by y b / a Output result 2
% Modulo - Returns the remainder of division b % a Output result 0
** Power - Returns the y power of Integer
example demonstration The operation of all arithmetic operators in 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
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

The above is the detailed content of Detailed explanation of examples of arithmetic operators in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn