0
speed_of_light = 299792458.0
cycles_per_second = 2700000000.0
speed_of_light/cycles_per_second = x
print(x)

The above works but when i do this i get an error? the X is a string here.

speed_of_light = 299792458.0
cycles_per_second = 2700000000.0
speed_of_light/cycles_per_second = 'x'
print('x')

%run "/tmp/tmpMFc4pM.py"
File "/tmp/tmpMFc4pM.py", line 5
'x' = speed_of_light/cycles_per_second
> SyntaxError: can't assign to literal
0

3 Answers 3

2

Maybe you mean:

speed_of_light = 299792458.0
cycles_per_second = 2700000000.0
x = speed_of_light/cycles_per_second
print (x)

As everybody else has pointed out by now, the variable should be on the left, then the equality sign and then (on the right) the thing that you want to put into the variable.

Sign up to request clarification or add additional context in comments.

Comments

2

You can't assign a value to a computation.

speed_of_light/cycles_per_second = 'x' is an assignment, not an equality in the mathematical sense.

You probably meant to do the reverse, set x to speed_of_light/cycles_per_second. In other words assign speed_of_light/cycles_per_second to x:

x = speed_of_light/cycles_per_second

Note that x isn't put into quotes (which would make it a character literal) but rather simply x, which implies it's a variable and can hold any value assigned to it.

E.g., you can say something like

age = 20

but not

20 = age

while in terms of mathematics they are equivalent, the 2nd assignment would give you the error message you received (SyntaxError: can't assign to literal). In other words you were trying to assign a value to a literal (20) which can't hold any other value other than what it represents.

Comments

1

As the error says, you can't assign a variable amount to a string literal like x. That's like saying the string "ice cream" is now equal to 2.71. It doesn't make sense.

Also, you have assignment syntax reversed. Change it to

x = speed_of_light/cycles_per_second

This says to take the value on the right and assign it to the variable on the left.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.