Python provides built-in support for working with numbers, including integers, floating-point numbers, and complex numbers. It also includes a rich set of mathematical functions and methods for performing numerical operations.
Types of Numbers in Python
Python supports three main types of numbers:
# Integer a = 10 print(type(a)) # <class 'int'> # Float b = 3.14 print(type(b)) # <class 'float'> # Complex c = 2 + 3j print(type(c)) # <class 'complex'>
Basic Arithmetic Operations
Python supports standard arithmetic operations:
x = 10 y = 3 print(x + y) # 13 print(x - y) # 7 print(x * y) # 30 print(x / y) # 3.3333333333333335 print(x // y) # 3 print(x % y) # 1 print(x ** y) # 1000
Built-in Number Functions
Python provides several built-in functions for working with numbers.
abs(x) - Absolute Value
print(abs(-5)) # 5 print(abs(3.5)) # 3.5
pow(x, y[, mod]) - Power Function
print(pow(2, 3)) # 8 (2³) print(pow(2, 3, 5)) # 3 (2³ % 5)
round(x, ndigits) - Rounding
print(round(3.14159, 2)) # 3.14 print(round(7.5)) # 8 (rounds to nearest even integer)
divmod(x, y) - Division and Modulus
print(divmod(7, 2)) # (3, 1) → quotient and remainder
Mathematical Functions (math module)
Python's math module provides advanced mathematical functions.
import math print(math.sqrt(16)) # 4.0 (square root) print(math.ceil(3.1)) # 4 (round up) print(math.floor(3.9)) # 3 (round down) print(math.fabs(-10)) # 10.0 (absolute value) print(math.factorial(5)) # 120 (5!) print(math.log(10, 2)) # 3.321928094887362 (log base 2) print(math.sin(math.pi / 2)) # 1.0 (sin 90 degrees)
Working with Random Numbers (random module)
Python’s random module allows generating random numbers.
import random print(random.random()) # Random float between 0 and 1 print(random.randint(1, 10)) # Random integer between 1 and 10 print(random.uniform(1, 10)) # Random float between 1 and 10 print(random.choice([1, 2, 3, 4, 5])) # Random choice from a list
List Methods for Working with Numbers
Python lists are often used to store and manipulate collections of numbers. Here are some useful list methods:
lst = [3, 2, 1, 9, 5] # sum(lst) # Returns sum of all elements print( sum(lst) ) # 10 # min(lst) # Returns the smallest element print( min(lst) ) # 1 # max(lst) # Returns the largest element print( max(lst) ) # 9 # sorted(lst) # Returns a sorted list print( sorted(lst) ) # [1, 2, 3, 5, 9] # len(lst) # Returns the number of elements print( len(lst) ) # 5 # lst.append(x) # Adds an element to the end print( lst.append(10) ) print(lst) # [3, 2, 1, 9, 5, 10] # lst.extend(iterable) # Adds multiple elements lst.extend([4, 5, 6]) print(lst) # [3, 2, 1, 9, 5, 10, 4, 5, 6] # lst.insert(i, x) # Inserts at index i lst.insert(1, 99) print(lst) # [3, 99, 2, 1, 9, 5, 10, 4, 5, 6] # lst.remove(x) # Removes first occurrence lst.remove(3) print(lst) # [99, 2, 1, 9, 5, 10, 4, 5, 6] # lst.pop(i) # Removes and returns element at index i print( lst.pop(2) ) # 1 print(lst) # [99, 2, 9, 5, 10, 4, 5, 6] # lst.index(x) # Returns index of first occurrence print( lst.index(5) ) # 3 # lst.count(x) # Counts occurrences of x print( lst.count(2) ) # 1 # lst.reverse() # Reverses the list lst.reverse() print(lst) # [6, 5, 4, 10, 5, 9, 2, 99]