How to do Python math at command line?

Python can be used as a powerful calculator at the command line. When you invoke the Python interpreter, you get the (>>>) prompt where any mathematical expression can be entered and evaluated immediately upon pressing ENTER.

Python Math Operators

Python provides several arithmetic operators for mathematical calculations ?

Operation Description
a + b Addition - returns the sum of a and b
a - b Subtraction - returns the difference of a and b
-a Negation - changes the sign of a
+a Identity - returns a unchanged
a * b Multiplication - returns the product of a and b
a / b Division - returns the quotient as float
a // b Floor division - returns the quotient as integer
a % b Modulo - returns the remainder of a / b
a ** b Exponentiation - returns a to the power of b

Addition and Subtraction

Basic Addition

Python addition works just like mathematical addition ?

# Adding two numbers
print(3 + 7)

# Using variables
x = 3
y = 7
print(x + y)
10
10

Working with Different Number Types

Addition works with integers, floats, and negative numbers ?

# Negative and positive numbers
print(-10 + 5)

# Floating-point numbers
print(4.5 + 2.5)
-5
7.0

Subtraction

Subtraction uses the minus sign (-) operator ?

# Basic subtraction
x = 50
y = 30
print(x - y)
20

Unary Operations

Unary operators work with a single operand. The plus (+) sign returns the identity of a value, while the minus (-) sign changes the sign ?

# Identity operator
x = 2.5
print(+x)

# With negative value
y = -10
print(+y)

# Negation operator
print(-x)
print(-y)
2.5
-10
-2.5
10

Multiplication and Division

Multiplication

Multiplication uses the asterisk (*) operator ?

# Multiplication
x = 4
y = 5
print(x * y)
print(2 * 3)
20
6

Division

Python 3 division always returns a float. Floor division (//) returns an integer ?

# Regular division (float result)
print(10 / 3)

# Floor division (integer result)
print(10 // 3)
3.3333333333333335
3

Modulo Operation

The modulo operator (%) returns the remainder after division ?

# Modulo operation
x = 10
y = 3
print(x % y)
1

Exponentiation

The double asterisk (**) operator raises a number to a power ?

# Power operation
x = 2
y = 3
print(x ** y)  # 2 to the power of 3

# Same as 2 * 2 * 2
print(2 * 2 * 2)
8
8

Conclusion

Python serves as an excellent command-line calculator with all standard mathematical operators. You can perform arithmetic operations directly at the Python prompt, making it perfect for quick calculations and mathematical experiments.

Updated on: 2026-03-24T20:15:15+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements