• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / Python / Initialize Multiple Variables in Python

Initialize Multiple Variables in Python

June 13, 2022 Leave a Comment

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.

Other Articles You'll Also Like:

  • 1.  How to Remove Vowels from a String in Python
  • 2.  Print Object Attributes in Python using dir() Function
  • 3.  Transpose DataFrame pandas – Using the pandas transpose Function
  • 4.  Using Python to Check If a Number is a Perfect Square
  • 5.  Using Python to Split String into Dictionary
  • 6.  Reverse a List in Python Without Reverse Function
  • 7.  Pandas Crosstab on Multiple Columns
  • 8.  Using Python to Print Environment Variables
  • 9.  Remove Leading and Trailing Characters from String with strip() in Python
  • 10.  How to Check if String Contains Lowercase Letters in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy