Python Arithmetic Operators

Question1: What are Python Arithmetic Operators?

Python supports all of the mathematical operations you would expect. Python Arithmetic Operators are used are used to perform mathematical operations such as addition, subtraction, multiplication, division, floor division, exponent and modulus. All these arithmetic operators in python are binary operators which means they operate on two operands.

Question2: Name and represent all the python arithmetic operators with examples.

The below table helps us name and represent all the python arithmetic operators with their respective examples

Question3: Using Python IDE, show the output of the above operations using python arithmetic operations.

For this Python arithmetic operators example, We are using two variables a and b and their values are 20 and 5. We

are going to use these two variables to perform various arithmetic operations in Python programming.

a=20

b=5

addition=a+b

subtraction=a-b

multiplication=a*b

division=a/b

modulus=a%b

exponent=a**b

floor_division=a//b

print(“Addition of two numbers”,a,”and”,b,”is”,addition)

print(“Subtraction of two numbers”,a,”and”,b,”is”,subtraction)

print(“Multiplication of two numbers”,a,”and”,b,”is”,multiplication)

print(“Division of two numbers”,a,”and”,b,”is”,division)

print(“Modulus of two numbers”,a,”and”,b,”is”,modulus)

print(“Exponent of two numbers”,a,”and”,b,”is”,exponent)

print(“Floor Division of two numbers”,a,”and”,b,”is”,floor_division)

Output:

Addition of two numbers 20 and 5 is 25

Subtraction of two numbers 20 and 5 is 15

Multiplication of two numbers 20 and 5 is 100

Division of two numbers 20 and 5 is 4.0

Modulus of two numbers 20 and 5 is 0

Exponent of two numbers 20 and 5 is 3200000

Floor Division of two numbers 20 and 5 is 4

Note:

I)The exponent operator is simply the first number raised to the second number. In the above example, it means 20 power 5 or 20*20*20*20*20 which is 3200000.

II)When we are using division (/) operator the result will be in float or decimal value. If you want to display the output as an integer value by rounding the value then use floor division (//) operator.

Question4: Can we use Python Arithmetic Operations on strings?

We can only use selected arithmetic operators on a string like we can concatenate two strings with the help of the addition (+) symbol. Likewise we can also multiply the strings by using the multiplication (*) symbol. In Python, only multiplication

(*) and addition(+) are the two operators used for string operations.

Question5: With examples, show the working of Python Arithmetic Operators on strings.

In this Python Arithmetic operators on strings example. We are using two variables a and b of

string datatype.

a=”Fight”

b=”Corona”

First, we declared two variables a and b of type string and they hold the string “Fight” and “Corona” respectively.

I)The following print statement will display the output as “FightCorona” because when we use the addition operator (+) in between string variables, python will concatenate the two strings.

print(a+b)

Output:

FightCorona

II)The following print statement will insert the empty space between the two words “Fight” and

“Corona”. It means the output will be “Fight Corona”.

print(a+” “+b)

Output:

Fight Corona

III)The following print statement will display “Fight” three times.

print(a*3)

Output:

FightFightFight

IV)The following print statement will insert space in between each iteration while displaying “Fight” three times.

print((a+” “)*3)

Output:

Fight Fight Fight

IV)The following print statement will contain the addition (+) operator between a string and an integer

print(a+3)

Output:

TypeError: can only concatenate str (not “int”) to str

The above statement will throw an error because we cannot convert integer object into string object implicitly. To

resolve this issue, we have to convert it explicitly using the following statement.

print(a+str(3))

Output:

Fight3

Tip: Be careful with concatenating the string and integer (Type Casting plays a major role here)

Thank You

Name | Views | Likes |

Python Statistics stdev() | 989 | 0 |

Python Textwrap dedent() | 1179 | 0 |

Python Statistics median() | 1500 | 0 |

Spam Mail Classification using Scikit Learn | 2068 | 0 |

DETECTION OF COLOR OF AN IMAGE USING OpenCV | 1722 | 1 |

Python Textwrap shorten() | 1042 | 0 |

Python Textwrap indent() | 1675 | 1 |

Python Textwrap fill() | 911 | 0 |

COVID-19 Prediction in People Using Machine Learning | 1883 | 5 |

Python Statistics median_low() | 380 | 0 |

Heart Disease Prediction Using Machine Learning | 1421 | 0 |

Python Statistics harmonic_mean() | 835 | 0 |

Python Textwrap wrap() | 481 | 1 |

Python Statistics variance() | 687 | 0 |

Credit Card Risk Assessment Using Machine Learning | 1150 | 0 |

Diabetes Prediction Using Machine Learning | 3060 | 0 |

Python Statistics mode() | 2356 | 0 |

Python Statistics Introduction | 554 | 0 |

Python Statistics median_grouped() | 1021 | 0 |

Python Statistics multimode() | 2975 | 0 |

Python Statistics pvariance() | 867 | 0 |

Python Question – Arithmetic Operators | 5809 | 0 |

House Price Prediction Using Machine Learning | 3855 | 0 |

Python Statistics mean() | 5071 | 0 |

Python Textwrap Introduction | 395 | 0 |

Python Statistics median_high() | 419 | 0 |