Complete the code to evaluate the expression '2 + 3' using eval().
result = eval([1]) print(result)
The eval() function expects a string expression to evaluate. So the expression must be passed as a string, like "2 + 3".
Complete the code to evaluate the expression stored in the variable 'expr' using eval().
expr = '10 / 2' result = eval([1]) print(result)
Since expr is a variable holding the string expression, we pass it directly to eval() without quotes.
Fix the error in the code to evaluate the expression 'df["A"] + df["B"]' using pandas.eval().
import pandas as pd df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]}) result = pd.eval([1]) print(result)
pandas.eval() expects the expression as a string. So the expression must be passed as a string with proper quotes.
Fill both blanks to create a dictionary comprehension that uses eval() to filter words longer than 3 characters.
words = ['cat', 'lion', 'dog', 'elephant'] lengths = {word: [1] for word in words if [2]
The dictionary comprehension maps each word to its length using len(word). The filter keeps words where len(word) > 3.
Fill all three blanks to create a pandas.eval() expression that calculates the sum of columns 'X' and 'Y' for rows where 'Z' > 5.
import pandas as pd df = pd.DataFrame({'X': [1, 2, 3], 'Y': [4, 5, 6], 'Z': [6, 4, 7]}) result = df.loc[df.eval([1]), [2] + [3]] print(result)
The eval() expression filters rows where Z > 5. Then columns 'X' and 'Y' are selected and summed.