To initialize multiple variables in a single line of code in Python, you can use tuple unpacking to initialize multiple variables.
a, b = 1, 2
print(a)
print(b)
#Output:
1
2
You can also use semicolons to create multiple variables.
a = 1; b = 2;
print(a)
print(b)
#Output:
1
2
When programming, variables are fundamental to storing data in our programs.
Sometimes, you might want to initialize multiple variables in one line. To declare multiple variables in a single line, you can use tuple unpacking.
a, b = 1, 2
print(a)
print(b)
#Output:
1
2
Tuple unpacking is the most pythonic way to declare multiple variables in your Python code.
You can also declare multiple variables and separate them with semicolons. This is one of the only uses of semicolons in Python.
This isn’t the most pythonic way to initialize variables but it will work.
Below shows how you can use semicolons to create multiple variables in a single line.
a = 1; b = 2;
print(a)
print(b)
#Output:
1
2
Hopefully this article has been useful for you to learn how to initialize multiple variables in Python.
Leave a Reply