3

I've code, var a,b,c,d;

I need to rewrite this in python. I'm not sure, since I'm new to python, how to define multiple variables in a sinlge line with no assignment.

I thought of doing

> a=None 
> b=None 
> c=None 
> d=None

But it should be in one line

Aditya Malviya
  • 1,907
  • 1
  • 20
  • 25
  • 1
    You don't need a declaration in Python; use them on the go. – Austin Nov 10 '19 at 09:50
  • 4
    The above could be done in one line like `a = b = c = d = None` – user2390182 Nov 10 '19 at 09:51
  • Does this answer your question? [More elegant way of declaring multiple variables at the same time](https://stackoverflow.com/questions/5495332/more-elegant-way-of-declaring-multiple-variables-at-the-same-time) – Sheri Nov 10 '19 at 09:54

4 Answers4

13

More pythonic way is tuple unpacking:

a, b, c, d = 1, 2, 3, 4

Or if you want to initialize to single value

a = b = c = d = 1

You could also use semi-colon (although not encouraged)

a=1; b=2; c=3; d=4

All of them would work.

Zeeshan
  • 1,078
  • 9
  • 14
6

You could use tuple unpacking:

a, b, c = 1, 2, 3

But to be honest, it would more Pythonic to do the assignments on separate lines.

Joe Halliwell
  • 1,155
  • 6
  • 21
  • and What if I want to just declare 3 variables in one single line without assignment? – Aditya Malviya Nov 10 '19 at 09:53
  • 4
    You don't declare variables in Python. This only makes sense in languages in which variables have a type that can't be changed. Variables in Python are rather like names that you give to objects, and only the object they refer to has a type. See https://nedbatchelder.com/text/names.html for some in-depth explanation. – Thierry Lathuille Nov 10 '19 at 09:54
2

The "to a single value" part warrants a little bit of extra explanation.

a = b = c = d = None
a = 1
print(b)

>>> None

The above statement does set the value of each variable to None, but with some other types of values, this may not always be the case. Python is an object-oriented language and if you replace the value None with another type of value that is a Python object you might get results that you don't expect (because Python sets all four variables to literally the same object). Consider:

a = b = c = d = list()
a.append(1)
print(b)

>>>[1]

The reason that the result is different is because a, b, c, and d are all referring to the same list. This is a fundamental concept in Python and a really important one to remember, and shows why making these types of one-line declarations can be potentially problematic.

As others have said, declaring your variables on the go (as you need them) is probably the better way to go overall, as it helps to avoid these types of "less obvious" declaration issues.

DaveL17
  • 1,673
  • 7
  • 24
  • 38
0

This will do it in a single line and several expressions

a=None; b=None; c=None; d=None

This will do it in a single line declaring all vars at once with the same value

a = b = c = d = None

And as pointed out in comments, you could even do it with 0 line since you can just use your vars on the go without prior declaration.

Zeitounator
  • 38,476
  • 7
  • 53
  • 66
Aditya Malviya
  • 1,907
  • 1
  • 20
  • 25