This is the third of Python Tutorials for Beginners. In order to get started, you can see Python Tutorial 1 | Introduction and Python tutorial 2 | Handling Strings. Let us learn about arithmetic operators in Python programming language. This tutorial explains the arithmetic operators in Python programming. Please view my Python video tutorial 3 or read on...

What is an operator? It is a construct for processing one or more data values. In Python programming language, an operator is shown by a special character or a keyword.

There are several arithmetic operators in Python. These are + for addition, - for subtraction, * for multiplication, / for division, // for floor division or integer division, % for remainder of integer division and ** for power. Now, let us see examples of integer add, float add, integer subtract, float subtract, integer multiply, float multiply, division, floor division, remainder and power arithmetic operations in Python.

There are several arithmetic operators in Python. These are + for addition, - for subtraction, * for multiplication, / for division, // for floor division or integer division, % for remainder of integer division and ** for power. Now, let us see examples of integer add, float add, integer subtract, float subtract, integer multiply, float multiply, division, floor division, remainder and power arithmetic operations in Python.

You can try these Python commands at the Python prompt. Note that comments follow the # symbol (they are only for your understanding).

>>> 1 + 2 # this Python statement adds the two values (also called operands) and echoes the result.

>>> 1 + 1.5 + 10 # works like the previous statement but the addition result is a float

>>> 3 - 2 # subtracts the second value from the first and echoes the result

>>> 3 - 1.5 # works like the previous statement but the subtraction result is a float

>>> 10 * 2 * 3 # multiplies the values

>>> 10.1 * 3 # works like the previous statement but the float result may not be exact!

*See how to write code to handle this problem in the*Python video tutorial 3

>>> 6 / 2 # divides the first value with the second value resulting in a float

>>> 10 // 3 # does the integer division (floor division) resulting in an integer

>>> 10 % 3 # gets the remainder of the floor division

>>> 2 ** 3 # raises the first value to the power of second value

It is possible to have different arithmetic operators in a single expression. If so, Python does the arithmetic operations according to operator precedence. In other words, some arithmetic operations have a higher priority than others. See more of above examples and operator precedence in action in my Python video tutorial 3. Thank you.

Nice article. Full of knowledge and also very helpful in programming related work.

ReplyDeleteBest Data Scrubbing Services Solutions India