How to Work with Numbers in Python: Python is a popular programming language that is widely used for data analysis, scientific computing, and other numerical applications. Whether you are a beginner or an experienced developer, understanding how to work with numbers in Python can help you achieve your programming goals.
Working with numbers is an essential part of many Python programs, and the language provides a wide range of tools and libraries for this purpose.
In this article, we will explore the basics of working with numbers in Python, including data types, arithmetic operations, math functions, number formatting and builtin functions.
What is numbers in Python ?
In Python, numbers are a data type used to represent numeric values. There are three main types of numbers in Python: integers, floatingpoint numbers, and complex numbers.
What is Data Types in Python ?
In Python, there are three main types of numbers: integers, floatingpoint and complex numbers.
Integers are whole numbers that do not have a decimal point. They can be positive, negative, or zero. For example, 5, 10, and 0 are all integers.
Floatingpoint numbers are numbers with decimal points. They can also be positive, negative, or zero. For example, 3.14, 2.5, and 0.0 are all floatingpoint numbers.
Complex numbers are numbers that have a real part and an imaginary part. They are represented as a + bj, where a is the real part, b is the imaginary part, and j is the imaginary unit (the square root of 1). For example, 2 + 3j is a complex number.
Arithmetic Operators in Python
Arithmetic operators in Python are symbols that perform basic arithmetic operations on numerical values. Python provides a wide range of arithmetic operations for working with numbers. These operators include addition (+), subtraction (), multiplication (*), division (/), modulus (%), exponentiation (**), and floor division (//).
Addition and Subtraction
Addition and subtraction are straightforward, and work as expected for both integers and floatingpoint numbers:
>>> 5 + 2
Output: 7
>>> 3.14  1.5
Output: 1.64
Multiplication and Division
Multiplication and division are also straightforward:
>>> 3 * 4
Output: 12
>>> 10 / 3
Output: 3.3333333333333335
Note: Division of two integers in Python 3.x returns a floatingpoint number, even if the result is a whole number. For example:
>>> 10 / 2
Output: 5.0
Modulus
The modulus operator returns the remainder of division:
>>> 10 % 3
Output: 1
Exponentiation
Exponentiation raises a number to a power:
>>> 2 ** 3
Output: 8
Floor Division
Floor division returns the quotient of division, rounded down to the nearest integer:
>>> 10 // 3
Output: 3
Math Functions in Python
Python provides a variety of math functions that can be used to perform more complex mathematical operations. These functions are included in the math module, which needs to be imported before use. Here are some examples of math functions in Python:
import math
# Square root
a = math.sqrt(16)
print(a) # Output: 4.0
# Trigonometric functions
b = math.sin(math.pi/2)
print(b) # Output: 1.0
c = math.cos(math.pi/2)
print(c) # Output: 0.0
# Exponential and logarithmic functions
d = math.exp(2)
print(d) # Output: 7.38905609893065
e = math.log(10)
print(e) # Output: 2.302585092994046
Random Numbers in Python
Python also includes a random module that can be used to generate random numbers. These numbers can be used for a variety of purposes, such as simulating a game or generating test data. Here are some examples of generating random numbers in Python:
import random
# Random integer between 0 and 9
a = random.randint(0, 9)
print(a) # Output: random integer between 0 and 9
# Random floatingpoint number between 0 and 1
b = random.random()
print(b) # Output: random floatingpoint number between 0 and 1
# Random choice from a list
c = random.choice(['apple', 'banana', 'cherry'])
print(c) # Output: random choice from the list
Number Formatting in Python
Number formatting in Python allows you to control the display of numbers in your output. This can be useful for presenting data in a clear and readable format. Here are some examples of number formatting in Python:
# Floatingpoint formatting
a = 3.14159265
print("{:.2f}".format(a)) # Output: 3.14
# Integer formatting
b = 12345
print("{:05d}".format(b)) # Output: 12345 (padded with zeros)
# Commaseparated formatting
c = 1000000
print("{:,}".format(c)) # Output: 1,000,000
Complex Numbers in Python
Python also supports complex numbers, which consist of a real and imaginary component. These numbers can be used for a variety of purposes, such as in signal processing or electrical engineering. Here are some examples of complex numbers in Python:
# Creating a complex number
a = 2 + 3j
print(a) # Output: (2+3j)
# Accessing the real and imaginary components
b = a.real
c = a.imag
print(b) # Output: 2.0
print(c) # Output: 3.0
# Complex conjugate
d = a.conjugate()
print(d) # Output: (23j)
Read Also : To read latest information about all Sarkari Yojana and Schemes, you may visit our website from Here.
Decimal and Fraction Modules in Python
Python includes the decimal and fraction modules, which can be used for more precise calculations involving decimals and fractions. These modules can be particularly useful in financial applications where precision is important. Here are some examples of using the decimal and fraction modules in Python:
# Decimal module
from decimal import Decimal
# Adding two decimals with different precision
a = Decimal('1.23')
b = Decimal('4.56')
c = a + b
print(c) # Output: 5.79
# Fraction module
from fractions import Fraction
# Adding two fractions
d = Fraction(1, 3)
e = Fraction(2, 3)
f = d + e
print(f) # Output: 1
Builtin Functions in Python
Python provides many builtin functions for working with numbers. Some important builtin functions are:
 abs(): returns the absolute value of a number
 round(): rounds a number to a specified number of decimal places
 max(): returns the maximum value of a set of numbers
 min(): returns the minimum value of a set of numbers
 pow(): raises a number to a power
Here are some examples of how to use these functions:
>>> abs(5)
Output: 5
>>> round(3.14159, 2)
Output: 3.14
>>> max(1, 2, 3, 4, 5)
Output: 5
>>> min(1, 2, 3, 4, 5)
Output: 1
>>> pow(2, 3)
Conclusion
Working with numbers in Python can be a powerful tool in achieving your programming goals. Whether you need to perform basic arithmetic operations, more complex math functions, generate random numbers, or use complex numbers, Python has you covered. Additionally, number formatting, as well as the decimal and fraction modules, can help ensure that your output is clear and precise.
Related articles:
 What is Type Conversion in Python ?
 Learn How to Work with Strings in Python ?
 What is Sequence in Python in Hindi ?
 What is tuple in Python Programming ?
FAQs on Numbers in Python

What data types can Python handle for numbers?
Python can handle integers, floatingpoint numbers, and complex numbers.

How do I perform basic arithmetic operations in Python?
Basic arithmetic operations such as addition, subtraction, multiplication, and division can be performed using the +, , *, and / operators, respectively.

How can I generate random numbers in Python?
The random module can be used to generate random integers, floatingpoint numbers, and choices from a list.

What is number formatting in Python?
Number formatting in Python allows you to control the display of numbers in your output. This can be useful for presenting data in a clear and readable format.

What are the decimal and fraction modules in Python used for?
The decimal and fraction modules can be used for more precise calculations involving decimals and fractions, particularly in financial applications.