Home > Commands -> var

EXAMPLES ON MY SAGE PAGE - Variable Names vs. Object Names

  • Variables are names that have not been assigned a value.
    • Objects names are names that have been assigned a value.
  • Variables must be declared using the command var (see x in example 1).
    • Object names do not have to be declared (see h in example 1)

YouTube Video - pdf

Syntax: var('x') or var('theta rho z') where x or theta, rho and z are (sample) names that will be used to define function arguments or constants.

     After declaring a variable name, the name can be used to define a function.

Example 1 - Create a function $f(x)=x^2+3x-2$ and an object h with value 1. Create/print a list of values of f(x) for x=0 to 5 step h.
list_f=[f(j) for j in [0..5,step=h] ]
print list_f
[-2.00000000000000, 2.00000000000000, 8.00000000000000, 16.0000000000000, 26.0000000000000, 38.0000000000000]

Example 2
#Look carefully at this. x is the variable in the function. w is just a "variable name". We need to declare both.
#Here we define a explicit (regular) function f(x) with one independent variable x, a constant w and then evaluate it at x=4.
var('x w')
print a

Keywords: variable, object, name, object name, declare