First time here? Check out the FAQ!

Ask Your Question
8

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

asked 13 years ago

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.

Preview: (hide)

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 ( 13 years ago )
slelievre gravatar imageslelievre ( 4 years ago )

2 Answers

Sort by » oldest newest most voted
7

answered 13 years ago

niles gravatar image

updated 13 years ago

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!

Preview: (hide)
link

Comments

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

kcrisman gravatar imagekcrisman ( 13 years ago )
3

answered 13 years ago

Juanlu001 gravatar image

updated 13 years ago

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.

Preview: (hide)
link

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: 13 years ago

Seen: 6,251 times

Last updated: Jun 17 '11