Difference between var('x') and x=var('x')?

77 views
Skip to first unread message

Jacare Omoplata

unread,
Jun 17, 2011, 10:54:08 AM6/17/11
to sage-support
In some tutorials, when a variable is declared, it is done like,

var('x')

In some others, it is done like,

x = var('x')

What is the difference between the two, if any?

Also, is it better to ask this kind of basic question in the Asksage
forum or the sage-support mailing list?

Thanks for your patience.

Harald Schilly

unread,
Jun 17, 2011, 11:10:26 AM6/17/11
to sage-s...@googlegroups.com
the difference is, that var(..) itself already injects a variable with that name into the global namespace, where the other one is an assignment.
i.e.

z = var('y')
gives you a variable y (named "y") and a variable z with the name "y"

sage: z = var('y')
sage: z
y
sage: y
y


you can also create invalid variable names, i.e. the ones starting with a number:

sage: var('2x')
2x

sage: 2x
------------------------------------------------------------
   File "<ipython console>", line 1
     2x
      ^
SyntaxError: invalid syntax

sage: globals()['2x']
2x

sage: type(globals()['2x'])
<type 'sage.symbolic.expression.Expression'>


H

kcrisman

unread,
Jun 17, 2011, 11:18:08 AM6/17/11
to sage-support
Harald explained this :)

> Also, is it better to ask this kind of basic question in the Asksage
> forum or the sage-support mailing list?

Either one is fine.

Burcin Erocal

unread,
Jun 17, 2011, 1:07:38 PM6/17/11
to sage-s...@googlegroups.com
On Fri, 17 Jun 2011 08:10:26 -0700 (PDT)
Harald Schilly <harald....@gmail.com> wrote:

> you can also create invalid variable names, i.e. the ones starting
> with a number:
>
> sage: var('2x')
> 2x
>
> sage: 2x
> ------------------------------------------------------------
> File "<ipython console>", line 1
> 2x
> ^
> SyntaxError: invalid syntax
>
> sage: globals()['2x']
> 2x
>
> sage: type(globals()['2x'])
> <type 'sage.symbolic.expression.Expression'>

Note that this behavior is not going to last. Only valid identifiers
will be allowed in variable names. We have 2 tickets on this:

#7496 symbolic variable names should be valid identifiers
#9724 Sage allows creation of variables with empty name

The second should be closed as a duplicate. :)


Cheers,
Burcin

William Stein

unread,
Jun 17, 2011, 2:42:31 PM6/17/11
to sage-s...@googlegroups.com
On Fri, Jun 17, 2011 at 7:54 AM, Jacare Omoplata
<walkey...@gmail.com> wrote:
> In some tutorials, when a variable is declared, it is done like,
>
> var('x')
>
> In some others, it is done like,
>
> x = var('x')
>
> What is the difference between the two, if any?

If you are writing code to include in .py files, then x=var('x') will
work, but var('x') will totally fail.
For purely interactive use in the notebook or command line, var('x')
is fine. But for writing programs, you should always use

x = var('x')

-- William

>
> Also, is it better to ask this kind of basic question in the Asksage
> forum or the sage-support mailing list?
>
> Thanks for your patience.
>

> --
> To post to this group, send email to sage-s...@googlegroups.com
> To unsubscribe from this group, send email to sage-support...@googlegroups.com
> For more options, visit this group at http://groups.google.com/group/sage-support
> URL: http://www.sagemath.org
>

--
William Stein
Professor of Mathematics
University of Washington
http://wstein.org

Volker Braun

unread,
Jun 18, 2011, 1:14:28 AM6/18/11
to sage-s...@googlegroups.com
I just fixed it in the patch on #7496:


sage: var('2x')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)

/home/vbraun/opt/sage-4.7.1.alpha2/devel/sage-main/<ipython console> in <module>()

/home/vbraun/Sage/sage/local/lib/python2.6/site-packages/sage/calculus/var.so in sage.calculus.var.var (sage/calculus/var.c:687)()

/home/vbraun/Sage/sage/local/lib/python2.6/site-packages/sage/symbolic/ring.so in sage.symbolic.ring.SymbolicRing.var (sage/symbolic/ring.cpp:6276)()

/home/vbraun/Sage/sage/local/lib/python2.6/site-packages/sage/symbolic/ring.so in sage.symbolic.ring.SymbolicRing.var (sage/symbolic/ring.cpp:6048)()

ValueError: The name "2x" is not a valid Python identifier.
sage: 

Jacare Omoplata

unread,
Jun 19, 2011, 11:22:49 AM6/19/11
to sage-support
Thanks for the replies.
Reply all
Reply to author
Forward
0 new messages