Module: M4-R5: Internet of Things (IoT)
Chapter: Ch1 Computer Intro
Python provides several built-in numeric functions for calculations, rounding, power operations, and working with integers and floats.
Evaluates a string as a Python expression.
expr = '5 + 10'
print(eval(expr))Output:
15Returns the maximum value among arguments.
print(max(5, 10, 3))Output:
10Returns the minimum value among arguments.
print(min(5, 10, 3))Output:
3Returns the power of a number (x^y).
print(pow(2, 3))Output:
8Rounds a number to nearest integer or specified decimal.
print(round(5.678, 2))Output:
5.68
Converts a number or string to integer.
print(int(5.9))Output:
5Converts a number or string to float.
print(float(5))Output:
5.0Returns a random float between 0 and 1. Requires import random.
import random
print(random.random())Output:
0.374832 (varies)Returns the smallest integer greater than or equal to a number. Requires import math.
import math
print(math.ceil(5.2))Output:
6Returns the largest integer less than or equal to a number. Requires import math.
import math
print(math.floor(5.8))Output:
5
Returns the square root of a number. Requires import math.
import math
print(math.sqrt(16))Output:
4.0