How to do Python math at command line?

PythonServer Side ProgrammingProgramming

In this article, we will show you how to do python math at the command line.

Python is an interpreter-based language. When you invoke the Python interpreter, (>>>) Python prompt appears. Any Python statement can be entered in front of it.

As soon as you press ENTER, the statement is executed. Hence a mathematical expression using operators defined in Python will be evaluated at the command line.

What are Operators?

A symbol or function that represents an operation is called an operator. For instance, in mathematics, the addition operator is represented by the plus sign (+).

While some of the operators we use in Python are familiar with math, other operators are unique to computer programming.

Here is a list of Python math operators for quick reference.

Operation Description
a+b returns the sum of a and b
a-b returns the difference of a and b
-a changes the sign of a
+a Indentity of a
a*b returns the product of a and b
a/b returns the quotient of a and b
a//b returns the quotient from floor division of a and b(as integer)
a%b returns the remainder of a / b
a**b returns a to the power of b

Perform Addition and subtraction

Python's addition and subtraction operators function similarly to mathematics. In fact, you can use the Python programming language as a calculator.

Addition

Example

# adding two numbers print(3 + 7)

Output

On executing, the above program will generate the following output −

10

Example

We can initialize variables to represent integer values rather than passing integers directly to the print statement –

# initializing numbers x = 3 y = 7 # printing the addition of two numbers print(x + y)

Output

On executing, the above program will generate the following output −

10

Example

We can combine a negative number with a positive number because integers can be both positive and negative numbers (as well as 0) –

# initializing numbers x = -10 y = 5 # printing the addition of negative and a postive number print(x + y)

Output

On executing, the above program will generate the following output −

-5

Example

When using floats, addition will operate similarly –

# initializing numbers x = 4.5 y = 2.5 # printing the addition of floating-point numbers print(x + y)

Output

On executing, the above program will generate the following output −

7.0

Subtraction

Similar to addition, subtraction uses the same syntax, but instead of using the plus sign (+) as your operator, you'll use the minus sign (-) instead:

Example

# initializing numbers x = 50 y = 30 # printing the subtraction(diffrence) of two numbers print(x-y)

Output

On executing, the above program will generate the following output −

20

Unary Arithmetic Operations

A unary mathematical expression contains only one component or element.

In Python, the plus and minus signs can be used as a single element combined with a value to return the value's identity (+) or change the sign of the value (-).

The + sign, though not usually used, denotes the identity of the value. With positive values, we can use the plus sign –

Example

x = 2.5 print(+x)

Output

On executing, the above program will generate the following output −

2.5

Example

When we use the plus sign with a negative value, it also returns the identity(similar) of that value, which is a negative value in this case:

x = -10 print(+x)

Output

On executing, the above program will generate the following output −

-10

With a negative value the + sign returns the same negative result.

The negative sign(-), on the other hand, changes the sign of a value. So, if we pass a positive value, the minus sign before the value returns a negative value

Example

x = 10 print(-x)

Output

On executing, the above program will generate the following output −

-10

Alternatively, when we apply the minus sign(-) unary operator with a negative value, we get a positive value:

Example

x = -10 print(-x)

Output

On executing, the above program will generate the following output −

10

The unary arithmetic operations denoted by the plus(+) and minus(-) signs will return the identity/similar of value in the case of +x or the value's opposite sign in the case of - x.

Perform Multiplication and Division

Multiplication and division, like addition and subtraction, will seem quite similar to how they do in mathematics.

In Python, the sign for multiplication is *, and the sign for division is /.

Multiplication

Example

# initializing numbers x = 4 y = 5 # printing multiplication/product of two numbers print(x*y) # printing multiplication of two numbers directly without initializing variables print(2*3)

Output

On executing, the above program will generate the following output −

20
6

Division

In Python 3, when we divide, the quotient will always be returned as a float, even if though we use two integers:

Example

# initializing numbers x = 10 y = 3 # printing the division of two numbers(returns as float) print(x/y)

Output

On executing, the above program will generate the following output −

3.3333333333333335

In Python 3, you can use double division operator // to perform floor division. Floor division can be used when you need a quotient to be in whole numbers. It returns the division result as an integer.

Example

# initializing numbers x = 10 y = 3 # printing the floor division of two numbers(returns as an integer) print(x//y)

Output

On executing, the above program will generate the following output −

3

Perform Modulo

The modulo operator(%) returns the remainder rather than the quotient after division. This is helpful for determining multiples of the same number.

Example

# initializing numbers x = 10 y = 3 # printing the remainder when x is divideb by y using modulo operator print(x % y)

Output

On executing, the above program will generate the following output −

1

Power

In Python, the ** operator is used to raise the number on the left to the power of the exponent of the right.

With other words, in the expression 5 ** 3, 5 is raised to the 3rd power. This formula is sometimes written as 53 in mathematics, however what it really means is that 5 is multiplied by itself three times.

In Python, we would obtain the same 125 result by doing 5 ** 3 or 5 * 5 * 5.

Example

# initializing numbers x = 2 y = 3 # printing the value of x raised to the power y print(x ** y)

Output

On executing, the above program will generate the following output −

8

Conclusion

We learned how to do Python math at the command line in this article. We used Python to perform several math operations such as addition, subtraction, multiplication, division, and power calculations.

raja
Updated on 28-Oct-2022 11:29:37

Advertisements