5

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

3
  • 1
    You don't need a declaration in Python; use them on the go. Commented Nov 10, 2019 at 9:50
  • 5
    The above could be done in one line like a = b = c = d = None Commented Nov 10, 2019 at 9:51
  • Does this answer your question? More elegant way of declaring multiple variables at the same time Commented Nov 10, 2019 at 9:54

4 Answers 4

16

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.

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

3 Comments

Semi colons in python?
only in such case
Could you elaborate on why it is not encouraged?
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.

2 Comments

and What if I want to just declare 3 variables in one single line without assignment?
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 nedbatchelder.com/text/names.html for some in-depth explanation.
3

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.

Comments

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.

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.