Skillversitas

Numbers

Python supports three numeric types: int (whole numbers), float (decimal numbers), and complex (complex numbers). Python also provides the math module for advanced mathematical operations.

Numbers are the fuel of most programs. From calculating purchase totals, determining discounts, to running scientific simulations — everything depends on numeric operations. Python handles numbers very well because its numeric types adapt automatically — you don't need to declare whether a variable is an int or a float. Python can also handle integers of any size without worrying about overflow, unlike many other languages.

For more serious mathematical needs, the math module provides dozens of functions such as trigonometry, logarithms, and constants like pi. Combined with the built-in round(), abs(), min(), and max() functions, that is enough for everyday needs.

Code Example

import math

# Numeric types
a = 10          # int
b = 3.14        # float
c = 2 + 3j      # complex

# Built-in math functions
print(round(3.14159, 2))  # 3.14
print(abs(-7))           # 7
print(max(1, 5, 3))      # 5

# math module
print(math.sqrt(16))    # 4.0
print(math.pi)          # 3.141592653589793
print(math.ceil(4.1))   # 5
print(math.floor(4.9))  # 4

Code Explanation

The variables a, b, and c demonstrate the three numeric types. a = 10 is an int, a whole number without decimals. b = 3.14 is a float, a decimal number with a dot. c = 2 + 3j is a complex number with an imaginary part (rarely used in everyday applications).

The built-in round(3.14159, 2) rounds a number to 2 decimal places. abs(-7) returns the absolute value (distance from zero). max(1, 5, 3) finds the largest value.

From the math module: sqrt(16) computes the square root, math.pi returns the pi constant, ceil(4.1) rounds up (5), and floor(4.9) rounds down (4).

Everyday Example

A discount calculator for an online store. The user enters the original price and a discount percentage. The program calculates the final price using basic numeric operations and then rounds it.

import math

original_price = 149000
discount = 25  # percent

discount_amount = original_price * discount / 100
final_price = original_price - discount_amount
final_price = round(final_price)

print(f"Original price: ${original_price}")
print(f"Discount {discount}%: ${round(discount_amount)}")
print(f"Final price: ${final_price}")

Summary

Python's numeric types are simple and flexible. With support for int, float, and the math module, we can handle all calculation needs — from basic arithmetic to complex scientific computations. Numbers are the foundation of quantitative logic in a program.