Complete the code to import the root function from scipy.optimize.
from scipy.optimize import [1] result = root(lambda x: x**2 - 4, 1.0) print(result.x)
The root function is imported from scipy.optimize to find roots of equations.
Complete the code to find the root of the function f(x) = x^3 - 1 using root with initial guess 0.5.
from scipy.optimize import root def f(x): return x**3 - 1 sol = root(f, [1]) print(sol.x)
The initial guess for the root is 0.5 as given in the instruction.
Fix the error in the code to correctly find the root of f(x) = cos(x) - x using brentq between 0 and 1.
from scipy.optimize import brentq import numpy as np def f(x): return np.cos(x) - x root_value = brentq(f, [1], 1) print(root_value)
The brentq method requires an interval where the function changes sign. The interval [0,1] is correct, so the first argument must be 0.
Fill both blanks to create a dictionary comprehension that maps words to their lengths only if length is greater than 3.
words = ['data', 'science', 'ai', 'ml'] lengths = {word: [1] for word in words if [2]
The dictionary comprehension maps each word to its length using len(word). The condition filters words with length greater than 3 using len(word) > 3.
Fill all three blanks to create a dictionary comprehension that maps uppercase words to their lengths only if length is greater than 2.
words = ['cat', 'dog', 'a', 'elephant'] result = {{ [1]: [2] for w in words if [3] }}
The dictionary comprehension uses w.upper() as keys, len(w) as values, and filters words with length greater than 2 using len(w) > 2.