-1

There's this guy here who insists that python variables can be assigned like

1 = 'foo'

I need help on what to tell him.

Ptar
  • 374
  • 4
  • 16
  • 2
    tell him to try it. and it wont work...problem solved – Derek Eden May 16 '20 at 05:35
  • 2
    Tell him to prove it – Hai Vu May 16 '20 at 05:36
  • Imagine, for a moment, that you could do that. You would have then broken the number 1. Now you can't add 1 to anything, because 1 equals `'foo'`. You can't set anything equal to 1. What happens if you want to do a `range()` starting at 1? You'd now be starting from `'foo'`, which makes no sense. In reality, Python will not allow this, and even if it did, for the love of all that is sacred, no one should ever do such a thing. – bug_spray May 16 '20 at 05:38

6 Answers6

2

No.

  • A variable name must start with a letter or the underscore character.

  • A variable name cannot start with a number. A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )

https://www.w3schools.com/python/python_variables.asp

Mystical
  • 2,505
  • 2
  • 24
  • 43
2

No it cannot haha. the simplest way to tell him is to show it.

if you run the code

1 = 'foo'

then you will get an error message, that will help him understand.

File "<stdin>", line 1
SyntaxError: can't assign to literal
Prab
  • 464
  • 3
  • 13
1

You can't do that. You get a syntax error.

Also, per PEP8:

Function and Variable Names

Function names should be lowercase, with words separated by underscores as necessary to improve readability.

Variable names follow the same convention as function names.

mixedCase is allowed only in contexts where that's already the prevailing style (e.g. threading.py), to retain backwards compatibility.

BruceWayne
  • 22,923
  • 15
  • 65
  • 110
0

It is impossible.

Name of variable in python should not be started with any number.

Gilseung Ahn
  • 2,598
  • 1
  • 4
  • 11
0

No, you can't. See illegal variable names.

Josh Correia
  • 3,807
  • 3
  • 33
  • 50
0

You can't use digits as the first character of a name. See https://docs.python.org/3/reference/lexical_analysis.html#identifiers

Faris
  • 358
  • 4
  • 10