Ask Your Question
8

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

asked 2011-06-17 11:53:03 +0200

omoplata gravatar image

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.

edit retag flag offensive close merge delete

Comments

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.

kcrisman gravatar imagekcrisman ( 2011-06-17 12:23:30 +0200 )edit
slelievre gravatar imageslelievre ( 2020-04-18 14:35:48 +0200 )edit

2 Answers

Sort by ยป oldest newest most voted
7

answered 2011-06-17 12:30:56 +0200

niles gravatar image

updated 2011-06-17 12:33:54 +0200

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!

edit flag offensive delete link more

Comments

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

kcrisman gravatar imagekcrisman ( 2011-06-17 15:44:35 +0200 )edit
3

answered 2011-06-17 12:27:33 +0200

Juanlu001 gravatar image

updated 2011-06-17 12:30:35 +0200

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2011-06-17 11:53:03 +0200

Seen: 5,804 times

Last updated: Jun 17 '11