Advertisement
#Arithmetic Operators Example in Python
x=2 # variable1
y=10 # variable2
s1 = 'hi' # variable3
s2 = ' there' # variable4
# addition of two integers
sum = x + y
# Overloading + operator performing concatenation of two string values
s1 = s1 + s2
# subtraction of two integers
subtract = x - y
# multiplication of two integers
multiply = x * y
# Overloading * operator performing multipliction of an integer with a string
result = 2 * s2
print("Sum of two integers = ", sum)
print("Concatenation of two strings = ", s1)
print("Subtraction of two integers = ", subtract)
print("Multplication of two integers = ", multiply)
print("Multplication of an int with a string = ", result)
Sum of two integers = 12
Concatenation of two strings = hi there
Subtraction of two integers = -8
Multplication of two integers = 20
Multplication of an int with a string = there there
# Division and Modulus operator in Python
i=40;
j=10;
floating_quotient = i/j
integer_quotient = i//j
remainder = i%j
print("Floating Quotient value after dividing 40 by 10 : ", floating_quotient)
print("Integer Quotient value after dividing 40 by 10 : ", integer_quotient )
print("Remainder after diviiding 40 by 10 : ", remainder)
a = 70
b = 20
floating_quotient = a/b
integer_quotient = a//b
floating_remainder = a%b
print("Floating Quotient value after dividing 70 by 20 : ", floating_quotient)
print("Integer Quotient value after dividing 70 by 20 : ", integer_quotient )
print("Remainder after dividing 70 by 20 : ", remainder)
k=20;
l=2.0
floating_quotient = k/l
integer_quotient = k//l
remainder = k%l
print("Floating Quotient value after dividing 40 by 10 : ", floating_quotient)
print("Integer Quotient value after dividing 40 by 10 : ", integer_quotient )
print("Remainder after diviiding 40 by 10 : ", remainder)
Floating Quotient value after dividing 40 by 10 : 4.0
Integer Quotient value after dividing 40 by 10 : 4
Remainder after diviiding 40 by 10 : 0
Floating Quotient value after dividing 70 by 20 : 3.5
Integer Quotient value after dividing 70 by 20 : 3
Remainder after dividing 70 by 20 : 0
Floating Quotient value after dividing 40 by 10 : 10.0
Integer Quotient value after dividing 40 by 10 : 10.0
Remainder after diviiding 40 by 10 : 0.0
Advertisement
# Arithmetic expressions with equal and unequal priority operators
result = 3+2-4+2; # + - operators have equal priorities
print("Result of 3+2-1+2 is : ", result)
result = 3+2*4+2; # + * have unequal priorities
print("Result of 3+2*4+2 is : ", result)
result = 3+2*4/2; # + * / operators have unequal priorities
print("Result of 3+2*4/2 is : ", result)
result = 3%2*4/2; # % * / operators have equal priorities
print("Result of 3%2*4/2 is : ", result)
Result of 3+2-1+2 is : 3
Result of 3+2*4+2 is : 13
Result of 3+2*4/2 is : 7.0
Result of 3%2*4/2 is : 2.0
#Arithmetic operator example
result = (3+2)*4+2;
print("Result of (3+2)*4+2 is : ", result);
result = 3%(2*4)/2;
print("Result of 3%(2*4)/2 is : ", result);
Result of (3+2)*4+2 is : 22
Result of 3(2*4)/2 is : 1.5
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement