Basic Operations with Numbers
Arithmetic Operations
Python supports basic arithmetic operations like addition, subtraction, multiplication, and division.
Examples
Python
# Addition
sum = 10 + 5
# Subtraction
difference = 10 - 5
# Multiplication
product = 10 * 5
# Division
quotient = 10 / 5

You can also try this code with Online Python Compiler
Run Code
Output
15
5
50
2.0
Comparison Operations
Comparison operations compare two values and return a Boolean.
Examples
Python
a = 10
b = 5
print(a > b)
print(a == b)

You can also try this code with Online Python Compiler
Run Code
Output
True
False
Logical Operations
Logical operations combine Boolean values.
Examples
Python
x = True
y = False
print(x and y)
print(x or y)
print(not x)

You can also try this code with Online Python Compiler
Run Code
Output
False
True
False
Bitwise Operations
Bitwise operations work on bits and perform bit-by-bit operations.
Bitwise operations work on bits and perform bit-by-bit operations.
Examples:
Python
a = 10 # Binary: 1010
b = 4 # Binary: 0100
print(a & b)
print(a | b)
print(a ^ b)

You can also try this code with Online Python Compiler
Run Code
Output
0 (Binary: 0000)
14 (Binary: 1110)
14 (Binary: 1110)
Type Conversion
Python allows converting numbers from one type to another using built-in functions.
Examples
Python
# Converting float to int
x = 10.5
y = int(x)
# Converting int to float
a = 5
b = float(a)

You can also try this code with Online Python Compiler
Run Code
Output
10
5.0
Mathematical Functions and Constants
Python's math module provides various mathematical functions and constants.
Examples
Python
import math
# Square root
print(math.sqrt(16))
# Power
print(math.pow(2, 3))
# Constants
print(math.pi)
print(math.e)

You can also try this code with Online Python Compiler
Run Code
Output
4.0
8.0
3.141592653589793
2.718281828459045
Decimal Module
The decimal module provides support for fast correctly-rounded decimal floating point arithmetic.
Example
Python
from decimal import Decimal
dec = Decimal('10.5')
print(dec)

You can also try this code with Online Python Compiler
Run Code
Output
10.5
Random Numbers
The random module provides various functions to generate random numbers.
Example:
Python
import random
print(random.randint(1, 10)) # Random integer between 1 and 10
print(random.random()) # Random float between 0.0 and 1.0

You can also try this code with Online Python Compiler
Run Code
Complex Numbers
Python supports complex numbers with built-in complex type.
Example
Python
z = 1 + 2j
print(z.real)
print(z.imag)

You can also try this code with Online Python Compiler
Run Code
Output
1.0
2.0
Frequently Asked Questions
How do I convert a string to a number in Python?
Use int(), float(), or complex() to convert a string to a number.
Can I perform arithmetic operations on Boolean values?
Yes, True is treated as 1 and False as 0 in arithmetic operations.
Conclusion
Python's robust support for various number types and operations makes it a versatile language for a wide range of applications. Whether you're performing basic arithmetic, scientific computations, or financial calculations, Python's number handling capabilities are up to the task.
You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.