• 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
  • Ruby
  • 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.  Using Python to Convert Tuple to String
  • 2.  Python to_bytes() – Create Bytes Object from Integer
  • 3.  Python atan – Find Arctangent and Inverse Tangent of Number
  • 4.  Using Python to Check if Number is Divisible by Another Number
  • 5.  Check if Variable is Datetime in Python
  • 6.  Python Replace Space With Dash Using String replace() Function
  • 7.  Flatten List of Tuples in Python
  • 8.  Convert String to Boolean Value in Python
  • 9.  Python sin – Find Sine of Number in Radians Using math.sin()
  • 10.  Python os.sep – Create Operating System Path Seperator Character

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

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

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 © 2023 · The Programming Expert · About · Privacy Policy