CoCalc -- Collaborative Calculation in the Cloud
SharedJupyterNotebooks / Courses / func-table-of-values.ipynbOpen in CoCalc

Creating a Table of Values using Sympy and Tabulate

# You might need to install the tabulate package
# so that you can print tables
# !pip install tabulate

# Import the Packages
from sympy import *
from tabulate import tabulate

# Defined our Variables using Sympy
x = Symbol('x')
f = Function('f')(x)

# Define our function using an expression
f = 2*x - 9

# Define the Domain Set
Domain_Set = [ i for i in range(-3,4)]

# Generate the Range Set
Range_Set = [f.subs(x,Domain_Set[i]) for i in range(len(Domain_Set))]

# Generate the Graph Set
Graph_Set = list(zip(Domain_Set, Range_Set))

# Print the Table of Values to the Screen
print(tabulate(Graph_Set, headers=['x', 'y'], tablefmt="fancy_grid"))

╒═════╤═════╕ │ x │ y │ ╞═════╪═════╡ │ -3 │ -15 │ ├─────┼─────┤ │ -2 │ -13 │ ├─────┼─────┤ │ -1 │ -11 │ ├─────┼─────┤ │ 0 │ -9 │ ├─────┼─────┤ │ 1 │ -7 │ ├─────┼─────┤ │ 2 │ -5 │ ├─────┼─────┤ │ 3 │ -3 │ ╘═════╧═════╛