codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐍

Python Programming

8 / 68 topics
7Python Data Types Overview8Python Numbers & Math9Python Type Conversion (Casting)10Python Booleans & None11Python Strings & Methods12Python String Formatting13Python Operators & Precedence
Tutorials/Python Programming/Python Numbers & Math
🐍Python Programming

Python Numbers & Math

Updated 2026-05-15
30 min read

Python Numbers & Math

In this tutorial, we will explore the various numeric data types available in Python, including integers, floats, and complex numbers. We'll also delve into performing mathematical operations using these types and how to utilize built-in modules like math and random for more advanced calculations.

2. Data Types & Operators

Integer Type

An integer is a whole number without any fractional or decimal component. Python's integers can be of arbitrary precision, meaning they can grow as large as your system's memory allows.

Example:

integers.py
1# Define some integers
2a = 10
3b = -3
4
5# Perform basic arithmetic operations
6sum_result = a + b
7difference_result = a - b
8product_result = a * b
9quotient_result = a / b # This will result in a float
10
11# Print the results
12print("Sum:", sum_result)
13print("Difference:", difference_result)
14print("Product:", product_result)
15print("Quotient:", quotient_result)
Output
Sum: 7
Difference: 13
Product: -30
Quotient: -3.3333333333333335

Float Type

A float, short for floating-point number, is a numeric data type that represents real numbers and is written with a decimal point dividing the integer and fractional parts.

Example:

floats.py
1# Define some floats
2x = 3.14
3y = -0.5
4
5# Perform arithmetic operations
6sum_result = x + y
7difference_result = x - y
8product_result = x * y
9quotient_result = x / y
10
11# Print the results
12print("Sum:", sum_result)
13print("Difference:", difference_result)
14print("Product:", product_result)
15print("Quotient:", quotient_result)
Output
Sum: 2.64
Difference: 3.64
Product: -1.57
Quotient: -6.28

Complex Number Type

A complex number is a number that can be expressed in the form \(a + bj\), where \(a\) and \(b\) are real numbers, and \(j\) represents the imaginary unit, which satisfies the equation \(j^2 = -1\).

Example:

complex_numbers.py
1# Define some complex numbers
2z1 = 3 + 4j
3z2 = 1 - 2j
4
5# Perform arithmetic operations
6sum_result = z1 + z2
7difference_result = z1 - z2
8product_result = z1 * z2
9quotient_result = z1 / z2
10
11# Print the results
12print("Sum:", sum_result)
13print("Difference:", difference_result)
14print("Product:", product_result)
15print("Quotient:", quotient_result)
Output
Sum: (4+2j)
Difference: (2+6j)
Product: (11+2j)
Quotient: (0.44+1.08j)

Mathematical Operations

Python supports a wide range of mathematical operations, including addition, subtraction, multiplication, division, exponentiation, and modulus.

Example:

math_operations.py
1# Define some numbers
2a = 10
3b = 3
4
5# Perform various operations
6sum_result = a + b
7difference_result = a - b
8product_result = a * b
9quotient_result = a / b
10remainder_result = a % b
11exponent_result = a ** b
12
13# Print the results
14print("Sum:", sum_result)
15print("Difference:", difference_result)
16print("Product:", product_result)
17print("Quotient:", quotient_result)
18print("Remainder:", remainder_result)
19print("Exponentiation:", exponent_result)
Output
Sum: 13
Difference: 7
Product: 30
Quotient: 3.3333333333333335
Remainder: 1
Exponentiation: 1000

Math Module

Python's math module provides access to mathematical functions that are not directly available in the basic arithmetic operations. Some of the commonly used functions include:

  • sqrt(x): Returns the square root of x.
  • ceil(x): Returns the smallest integer greater than or equal to x.
  • floor(x): Returns the largest integer less than or equal to x.
  • pi: A mathematical constant representing the value of π.

Example:

math_module.py
1import math
2
3# Define some numbers
4a = 16
5b = 3.7
6
7# Use math module functions
8sqrt_result = math.sqrt(a)
9ceil_result = math.ceil(b)
10floor_result = math.floor(b)
11pi_value = math.pi
12
13# Print the results
14print("Square root of", a, "is:", sqrt_result)
15print("Ceiling of", b, "is:", ceil_result)
16print("Floor of", b, "is:", floor_result)
17print("Value of pi is:", pi_value)
Output
Square root of 16 is: 4.0
Ceiling of 3.7 is: 4
Floor of 3.7 is: 3
Value of pi is: 3.141592653589793

Random Module

The random module in Python provides functions to generate random numbers. Some of the commonly used functions include:

  • randint(a, b): Returns a random integer between a and b, inclusive.
  • random(): Returns a random floating-point number between 0 and 1.

Example:

random_module.py
1import random
2
3# Generate random numbers
4random_int = random.randint(1, 10)
5random_float = random.random()
6
7# Print the results
8print("Random integer between 1 and 10:", random_int)
9print("Random float between 0 and 1:", random_float)
Output
Random integer between 1 and 10: 7
Random float between 0 and 1: 0.34567890123456789

Practical Example

Let's create a simple program that generates random numbers, calculates their square roots using the math module, and prints the results.

Example:

practical_example.py
1import math
2import random
3
4# Generate 5 random integers between 1 and 100
5random_numbers = [random.randint(1, 100) for _ in range(5)]
6
7# Calculate the square root of each number
8sqrt_results = [math.sqrt(num) for num in random_numbers]
9
10# Print the results
11for i in range(len(random_numbers)):
12 print(f"Random number: {random_numbers[i]}, Square root: {sqrt_results[i]:.2f}")
Output
Random number: 47, Square root: 6.86
Random number: 93, Square root: 9.64
Random number: 15, Square root: 3.87
Random number: 22, Square root: 4.69
Random number: 78, Square root: 8.83

Summary

ConceptDescription
IntegerWhole numbers without a fractional component
FloatReal numbers with a decimal point
Complex NumberNumbers in the form \(a + bj\) where \(j^2 = -1\)
Math ModuleProvides mathematical functions like sqrt, ceil, floor, and constants
Random ModuleGenerates random numbers for simulations and other applications

What's Next?

In the next tutorial, we will explore how to convert between different data types in Python using type casting. This knowledge is essential for handling various data inputs and ensuring that your programs can process them correctly.

Stay tuned!


PreviousPython Data Types OverviewNext Python Type Conversion (Casting)

Recommended Gear

Python Data Types OverviewPython Type Conversion (Casting)