import math
math
<module 'math' from '/usr/lib64/python3.12/lib-dynload/math.cpython-312-x86_64-linux-gnu.so'>
id(math)
140177537558144
type(math)
module
math.pi
3.141592653589793
math.e
2.718281828459045
math.sqrt
<function math.sqrt(x, /)>
help(math.sqrt)
Help on built-in function sqrt in module math: sqrt(x, /) Return the square root of x.
math.sqrt(2)
1.4142135623730951
math.sqrt(2 ** 2)
2.0
math.sqrt(sum([99, 100, 101]) / 3)
10.0
import random
random
<module 'random' from '/usr/lib64/python3.12/random.py'>
random.random
<function Random.random()>
help(random.random)
Help on built-in function random: random() method of random.Random instance random() -> x in the interval [0, 1).
random.random()
0.782609162553633
random.choice
<bound method Random.choice of <random.Random object at 0x561db35d9a60>>
help(random.choice)
Help on method choice in module random: choice(seq) method of random.Random instance Choose a random element from a non-empty sequence.
numbers = [7, 11, 8, 5, 3, 12, 2, 6, 9, 10, 1, 4]
random.choice(numbers)
4
random.seed(42)
random.random()
0.6394267984578837
random.seed(42)
random.random()
0.6394267984578837
!pip install numpy
Requirement already satisfied: numpy in /home/alexander/Repositories/intro-to-python/.venv/lib64/python3.12/site-packages (1.26.4)
import numpy as np
np
<module 'numpy' from '/home/alexander/Repositories/intro-to-python/.venv/lib64/python3.12/site-packages/numpy/__init__.py'>
vec = np.array(numbers)
vec
array([ 7, 11, 8, 5, 3, 12, 2, 6, 9, 10, 1, 4])
type(vec)
numpy.ndarray
2 * vec
array([14, 22, 16, 10, 6, 24, 4, 12, 18, 20, 2, 8])
2 * numbers # surprise, surprise
[7, 11, 8, 5, 3, 12, 2, 6, 9, 10, 1, 4, 7, 11, 8, 5, 3, 12, 2, 6, 9, 10, 1, 4]
sum(vec)
78
random.choice(vec)
7
!pwd
/home/alexander/Repositories/intro-to-python/02_functions
import sample_module as mod
mod
<module 'sample_module' from '/home/alexander/Repositories/intro-to-python/02_functions/sample_module.py'>
mod.average_evens
<function sample_module.average_evens(numbers, *, scalar=1)>
help(mod.average_evens)
Help on function average_evens in module sample_module: average_evens(numbers, *, scalar=1) Calculate the average of all even numbers in a list. Args: numbers (list of int's/float's): numbers to be averaged; if non-whole numbers are provided, they are rounded scalar (float, optional): multiplies the average; defaults to 1 Returns: scaled_average (float)
mod.average_evens(numbers)
7.0
mod.average_evens(numbers, scalar=2)
14.0