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

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?

edit retag close merge delete

Harald answered this on sage-support, so I won't here. Also, either location is just fine for asking questions; some people like this, some like the list.

Sort by » oldest newest most voted

Hi @omoplata!

This kind of question is fine either here or on sage-support -- which you use is just a matter of your preference.

Practically speaking, there is no difference between the two formats -- they both create a variable x. I think the different usage has to do with the fact that var both creates and returns the variable. So if you type

var('x')


you will get output

x


If you type

x = var('x')


then you are assigning the return value of var('x') to something (x in this case), and hence nothing is printed. This is just like the difference between

sage: 1 + 1
2


and

sage: w = 1 + 1
<nothing printed here>


An amusing side note is that you can make your code more confusing by assigning the return value of var('x') to a different variable:

t = var('x')


Then you can use the variable t, but it will always be printed as x.

sage: y = t^2 + t + 1
sage: y
x^2 + x + 1


I wouldn't really recommend using this ;)

Edit: Oh, I see that this question has been answered numerous times since I started composing my overly-long answer; sorry!

more

Eh, it's good to have both at both locations. This one will be easy to find. From the help:

"The new variable is both returned and automatically injected into the global namespace."

So, I think there is really no difference, unless you want a variable whose identifier and name are different.

Edit: Damn on me, Harald is right.

more