Table of contents
1.
Introduction
2.
Types of Numbers in Python
2.1.
Integers
2.1.1.
Syntax
2.2.
Floating-Point Numbers
2.2.1.
Syntax
2.3.
Complex Numbers
2.3.1.
Syntax
2.4.
Boolean
2.4.1.
Syntax
3.
Basic Operations with Numbers
3.1.
Arithmetic Operations
3.2.
Python
3.3.
Comparison Operations
3.4.
Python
3.5.
Logical Operations
3.6.
Python
3.7.
Bitwise Operations
3.8.
Python
4.
Type Conversion
4.1.
Python
5.
Mathematical Functions and Constants
5.1.
Python
6.
Decimal Module
6.1.
Python
7.
Random Numbers
7.1.
Python
8.
Complex Numbers
8.1.
Python
9.
Frequently Asked Questions
9.1.
How do I convert a string to a number in Python?
9.2.
Can I perform arithmetic operations on Boolean values?
10.
Conclusion
Last Updated: Jul 26, 2024
Easy

Python Numbers: A Comprehensive Guide

Author Sinki Kumari
0 upvote

Introduction

Numbers are fundamental in programming, used for everything from basic arithmetic to complex scientific computations. 

Python Numbers: A Comprehensive Guide

In Python, numbers are an essential part of the language, supporting a variety of types and operations.

Types of Numbers in Python

Integers

Integers are whole numbers, both positive and negative, without any decimal point.

Syntax

a = 10
b = -5

Floating-Point Numbers

Floating-point numbers are numbers with a decimal point.

Syntax

x = 10.5
y = -3.14

Complex Numbers

Complex numbers have a real part and an imaginary part, represented as a + bj.

Syntax

z = 2 + 3j

Boolean

Booleans represent True or False values and are a subtype of integers.

Syntax

is_valid = True
is_empty = False

Basic Operations with Numbers

Arithmetic Operations

Python supports basic arithmetic operations like addition, subtraction, multiplication, and division.

Examples

  • Python

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

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

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

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

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

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

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

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

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 AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass