Home » Python Arithmetic Operators: A Tutorial

Python Arithmetic Operators: A Tutorial

Java SE 11 Programmer I [1Z0-815] Practice Tests
Java SE 11 Programmer II [1Z0-816] Practice Tests
Java SE 11 Developer (Upgrade) [1Z0-817]
Spring Framework Basics Video Course
1 Year Subscription
Oracle Java Certification

Arithmetic operators in Python are used to perform mathematical operations between variables and values. Python supports several arithmetic operators that allow you to perform basic calculations such as addition, subtraction, multiplication, division, and more.

In this tutorial, we'll cover:

Basic arithmetic operators.
Operator precedence.
Using arithmetic operators with different data types.
Practical examples of using arithmetic operators in programs.

1. Basic Arithmetic Operators

Here are the basic arithmetic operators in Python:

Operator Name Description
+ Addition Adds two numbers
- Subtraction Subtracts the right operand from the left operand
* Multiplication Multiplies two numbers
/ Division Divides the left operand by the right operand (result is a float)
% Modulus Returns the remainder of the division
// Floor Division Divides and returns the integer part (discard the remainder)
** Exponentiation Raises the left operand to the power of the right operand

a) Addition (+)

# Example of addition
a = 10
b = 5

# Addition
result = a + b
print(result)  # Output: 15

b) Subtraction (-)

# Example of subtraction
a = 10
b = 5

# Subtraction
result = a - b
print(result)  # Output: 5

c) Multiplication (*)

# Example of multiplication
a = 10
b = 5

# Multiplication
result = a * b
print(result)  # Output: 50

d) Division (/)

# Example of division
a = 10
b = 3

# Division
result = a / b
print(result)  # Output: 3.3333333333333335

e) Modulus (%)

The modulus operator returns the remainder when one number is divided by another.

# Example of modulus
a = 10
b = 3

# Modulus
result = a % b
print(result)  # Output: 1 (because 10 divided by 3 gives a remainder of 1)

f) Floor Division (//)

Floor division returns the largest integer less than or equal to the result of the division (i.e., it removes the decimal part).

# Example of floor division
a = 10
b = 3

# Floor Division
result = a // b
print(result)  # Output: 3 (since 10 divided by 3 is 3.333, and floor division truncates it to 3)

g) Exponentiation (**)

Exponentiation raises one number to the power of another.

# Example of exponentiation
a = 2
b = 3

# Exponentiation
result = a ** b
print(result)  # Output: 8 (2 raised to the power of 3 is 8)

3. Operator Precedence

In Python, operator precedence determines the order in which operations are performed. Operators with higher precedence are evaluated before operators with lower precedence.

Here’s the precedence of the arithmetic operators, from highest to lowest:

** (Exponentiation)
*, /, //, % (Multiplication, Division, Floor Division, Modulus)
+, – (Addition, Subtraction)

You can use parentheses to group operations and force them to execute in a specific order, regardless of precedence.

Example:

# Without parentheses
result = 2 + 3 * 2
print(result)  # Output: 8 (Multiplication is performed first)

# With parentheses
result = (2 + 3) * 2
print(result)  # Output: 10 (Addition is performed first due to parentheses)

4. Using Arithmetic Operators with Different Data Types

You can use arithmetic operators not only with integers and floats but also with other data types like strings and lists in some cases.

a) Strings and Addition (+)

The + operator can concatenate strings.

# String concatenation
first_name = "John"
last_name = "Doe"

full_name = first_name + " " + last_name
print(full_name)  # Output: John Doe

b) Lists and Addition (+)

The + operator can concatenate lists.

# List concatenation
list1 = [1, 2, 3]
list2 = [4, 5, 6]

result = list1 + list2
print(result)  # Output: [1, 2, 3, 4, 5, 6]

c) Strings and Multiplication (*)

The * operator can repeat a string multiple times.

# String repetition
word = "Hello"
result = word * 3
print(result)  # Output: HelloHelloHello

d) Lists and Multiplication (*)

The * operator can repeat a list multiple times.

# List repetition
numbers = [1, 2, 3]
result = numbers * 2
print(result)  # Output: [1, 2, 3, 1, 2, 3]

5. Practical Examples of Arithmetic Operators

a) Calculate the Area of a Circle

You can use arithmetic operators to calculate the area of a circle given its radius.

import math

# Radius of the circle
radius = 5

# Area of the circle
area = math.pi * radius ** 2
print(f"The area of the circle is: {area}")

b) Convert Temperature from Celsius to Fahrenheit

You can perform a simple arithmetic calculation to convert a temperature from Celsius to Fahrenheit.

# Temperature in Celsius
celsius = 25

# Convert Celsius to Fahrenheit
fahrenheit = (celsius * 9/5) + 32
print(f"The temperature in Fahrenheit is: {fahrenheit}")

c) Calculate Compound Interest

You can use arithmetic operators to calculate compound interest.

# Principal amount
P = 1000

# Annual interest rate (in decimal)
r = 0.05

# Number of times interest is compounded per year
n = 4

# Time in years
t = 5

# Compound interest formula
A = P * (1 + r / n) ** (n * t)

print(f"The compound interest is: {A}")

Summary

+, -, *, /, %, //, **: Basic arithmetic operators in Python.
Operator precedence: Determines the order in which operations are evaluated.
Parentheses: Can be used to force a specific order of operations.
Arithmetic operators can be applied to numbers, strings, and lists.

By mastering Python’s arithmetic operators, you can perform a wide range of mathematical calculations efficiently.

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More