Mapa mental
Introduction to Python modules
Conteúdo do mapa
Introduction to Python modules
Module
- Definition
- Separately saved unit whose functionality can be reused at will
- Definition
Purpose of modules.
- Code reusability.
Importing Modules
- Using the import statement.
- Syntax: import module_name.
- Example: import math.
- Importing specific functions or classes.
- Syntax: from module_name import function_name.
- Example: from math import sqrt.
- Using the import statement.
Commonly Used Modules
- math module
- Purpose of math modules
- Provide mathematical functions
- Contants
- pi
- mathematical constant
- used in geometry
- syntax: math.pi
- e
- The math. e constant returns the Eular's number: 2.718281828459045
- pi
- Functions
- ceil()
- Rounds a number up to the nearest integer
- print(math.ceil(4.2)) Output: 5
- print(math.ceil(-4.2)) Output: -4
- Rounds a number up to the nearest integer
- sqrt()
- Function to calculate the square root Part of the math module Returns a float
- Access using 'math.sqrt()'
- Function to calculate the square root Part of the math module Returns a float
- exp()
- Returns e raised to the power of a given number
- syntax: math.exp(x)
- Returns e raised to the power of a given number
- fabs()
- Function to compute the absolute value
- Returns the non-negative value
- Example
- math.fabs(-5) returns 5.0
- fabs(3.2) returns 3.2
- Function to compute the absolute value
- floor()
- Rounds down to nearest integer
- Example
- floor(3.7) returns 3
- floor(-3.7) returns -4
- pow()
- Computes power of a number
- math.pow(2, 3) returns 8
- sin()
- Returns sine of an angle
- ceil()
- Purpose of math modules
- random module
- random()
- Function to generate random numbers
- random.random()
- Function to generate random numbers
- randint()
- Function to generate random integers
- Example: random.randint(1, 10)
- Function to generate random integers
- randrange()
- A function to generate random numbers
- Syntax: randrange(start, stop[, step])
- Example: result = randrange(0, 20, 5)
- Generates 0, 5, 10, 15 (exclusive of 20).
- Example: result = randrange(0, 20, 5)
- Syntax: randrange(start, stop[, step])
- A function to generate random numbers
- random()
- statistics module
- mean()
- Calculates the average of numerical values
- statistics.mean(data)
- Calculates the average of numerical values
- mode()
- Most frequently occurring value
- Example
- data = [1, 2, 2, 3]
- `statistics.mode(data)` returns `2`
- data = [1, 2, 2, 3]
- Example
- Most frequently occurring value
- median()
- Median is the middle value
- statistics.median(data)
- Median is the middle value
- mean()
- math module
Informações
- Descrição
- Mapa Mental sobre Introduction to Python modules, criado por Suja Nair em 17-05-2025.
- Criado por
- Suja Nair
- Visualizações
- 0
- Avaliação