What is Computer

Module: M2-R5: Web Design & Publishing

Chapter: Ch1 Computer Intro

🔹 Introduction to Numeric Functions

Python provides several built-in numeric functions for calculations, rounding, power operations, and working with integers and floats.

🔹 Numeric Functions with Examples
🔹 eval()

Evaluates a string as a Python expression.

expr = '5 + 10'
print(eval(expr))

Output:

15
🔹 max()

Returns the maximum value among arguments.

print(max(5, 10, 3))

Output:

10
🔹 min()

Returns the minimum value among arguments.

print(min(5, 10, 3))

Output:

3
🔹 pow()

Returns the power of a number (x^y).

print(pow(2, 3))

Output:

8
🔹 round()

Rounds a number to nearest integer or specified decimal.

print(round(5.678, 2))

Output:

5.68
🔹 int()

Converts a number or string to integer.

print(int(5.9))

Output:

5
🔹 float()

Converts a number or string to float.

print(float(5))

Output:

5.0
🔹 random()

Returns a random float between 0 and 1. Requires import random.

import random
print(random.random())

Output:

0.374832 (varies)
🔹 ceil()

Returns the smallest integer greater than or equal to a number. Requires import math.

import math
print(math.ceil(5.2))

Output:

6
🔹 floor()

Returns the largest integer less than or equal to a number. Requires import math.

import math
print(math.floor(5.8))

Output:

5
🔹 sqrt()

Returns the square root of a number. Requires import math.

import math
print(math.sqrt(16))

Output:

4.0
✅ Summary
  • Python numeric functions help perform calculations, rounding, and mathematical operations.
  • Functions like eval(), max(), min(), pow(), round() simplify numeric computations.
  • Modules like math and random extend numeric functionalities.
Quick Links