Open In App

Assigning multiple variables in one line in Python

Last Updated : 14 Sep, 2022
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

A variable is a segment of memory with a unique name used to hold data that will later be processed. Although each programming language has a different mechanism for declaring variables, the name and the data that will be assigned to each variable are always the same. They are capable of storing values of data types.

The assignment operator(=) assigns the value provided to its right to the variable name given to its left. Given is the basic syntax of variable declaration:

Syntax: var_name = value

Example:

a = 4

 Assign Values to Multiple Variables in One Line

Given above is the mechanism for assigning just variables in Python but it is possible to assign multiple variables at the same time. Python assigns values from right to left. When assigning multiple variables in a single line, different variable names are provided to the left of the assignment operator separated by a comma. The same goes for their respective values except they should be to the right of the assignment operator.

While declaring variables in this fashion one must be careful with the order of the names and their corresponding value first variable name to the left of the assignment operator is assigned with the first value to its right and so on. 

Example 1:

Variable assignment in a single line can also be done for different data types.

Python3




a, b = 4, 8
print("value assigned to a")
print(a)
print("value assigned to b")
print(b)


Output:

value assigned to a
4
value assigned to b
8

Example 2:

Not just simple variable assignment, assignment after performing some operation can also be done in the same way.

Python3




print("assigning values of different datatypes")
a, b, c, d = 4, "geeks", 3.14, True
print(a)
print(b)
print(c)
print(d)


Output:

assigning values of different datatypes
4
geeks
3.14
True

Example 3:

Assigning different operation results to multiple variable.

Python3




a, b = 8, 3
add, pro = (a+b), (a*b)
print(add)
print(pro)


Output:

11
24

Example 4:

Here, we are storing different characters in a different variables.

Python3




string = "Geeks"
a, b, c = string[0], string[1:4], string[4]
 
print(a)
print(b)
print(c)


Output:

G
eek
s


Similar Reads

Assigning Values to Variables
In this article, we will learn how to assign values to variables in Python and other languages also. Assigning Values to Variables in PythonBelow are the steps and methods by which we can assign values to variables in Python and other languages: Direct Initialization MethodUsing Conditional OperatorAssign Values to Variables Direct Initialisation M
4 min read
Swap Two Variables in One Line
We have discussed different approaches to swap two integers without the temporary variable. How to swap into a single line without using the library function?1) Python: In Python, there is a simple and syntactically neat construct to swap variables, we just need to write "x, y = y, x".2) C/C++: Below is one generally provided classical solution: //
4 min read
Create integer variable by assigning binary value in Python
Given a binary value and our task is to create integer variables and assign values in binary format. To assign value in binary format to a variable, we use the 0b suffix. It tells the compiler that the value (suffixed with 0b) is a binary value and assigns it to the variable. Input: Var = 0b1010 Output: 10 Input: Var = 0b11001 Output: 25 Note: To p
2 min read
How to input multiple values from user in one line in Python?
For instance, in C we can do something like this: C/C++ Code // Reads two values in one line scanf("%d %d", &x, &y) One solution is to use raw_input() two times. C/C++ Code x, y = input(), input() Another solution is to use split() C/C++ Code x, y = input().split() Note that we don't have to explicitly specify spli
2 min read
How to Catch Multiple Exceptions in One Line in Python?
There might be cases when we need to have exceptions in a single line. In this article, we will learn about how we can have multiple exceptions in a single line. We use this method to make code more readable and less complex. Also, it will help us follow the DRY (Don't repeat code) code method. Generally to handle exceptions we use try/Except metho
2 min read
PyQt5 QCalendarWidget - Assigning Base Size value
In this article, we will see how we can set the base size value to the QCalendarWidget. By default base size has a value zero for both width and height, base size is used to calculate a proper calendar size if the calendar defines sizeIncrement i.e its size change when window size changes. Base size is the initial size of the calendar. In order to
1 min read
PyQt5 QCommandLinkButton - Assigning Object Name
In this article we will see how we can set the object name of the QCommandLinkButton. Object name is given for searching the specific button inside the window programmatically, also object name can be used for setting stylesheet using object name. By default developer has not set any object name to the command link although we can set it any time.
1 min read
PyQt5 QCommandLinkButton - Assigning Cursor
In this article we will see how we can set i.e assign cursor to the QCommandLinkButton. Assigning cursor means the mouse cursor will assume the new shape when it's over the command link button. Cursor shape is basically cursor icons these are used to classify the actions. In order to do this we use setCursor method with the command link button obje
1 min read
PyQt5 QDateEdit - Assigning Description
In this article we will see how we can assign a description to the QDateEdit. Description property holds the date edit's description as seen by assistive technologies. The accessible description of a date edit should convey what date edit does. While the accessible name should be a short and concise string (e.g. Office Date), the description should
2 min read
PyQt5 QDateEdit - Assigning Name Property
In this article we will see how we can assign name to the QDateEdit. Name property holds the date edit's name as seen by assistive technologies. This is the primary name by which assistive technology such as screen readers announce the given date edit. In order to do this we use setAccessibleName method with the QDateEdit object Syntax : date.setAc
1 min read
MoviePy – Assigning Audio Clip to Video File
In this article we will see how we can add external audio to the video file clip in MoviePy. MoviePy is a Python module for video editing, which can be used for basic operations on videos and GIF’s. Video is formed by the frames, combination of frames creates a video each frame is an individual image. An audio file format is a file format for stori
2 min read
How to check multiple variables against a value in Python?
Given some variables, the task is to write a Python program to check multiple variables against a value. There are three possible known ways to achieve this in Python: Method #1: Using or operator This is pretty simple and straightforward. The following code snippets illustrate this method. Example 1: C/C++ Code # assigning variables a = 100 b = 0
2 min read
Python - Scipy curve_fit with multiple independent variables
Curve fitting examines the relationship between one or more predictors (independent variables) and a response variable (dependent variable), with the goal of defining a "best fit" model of the relationship. It is the process of constructing a mathematical function, that has the best fit to a series of data points possibly subject to constraints. Cu
3 min read
Compare two Files line by line in Python
In Python, there are many methods available to this comparison. In this Article, We'll find out how to Compare two different files line by line. Python supports many modules to do so and here we will discuss approaches using its various modules. This article uses two sample files for implementation. Files in use: file.txt file1.txt Method 1: Using
3 min read
Read a file line by line in Python
Prerequisites: Open a file Access modes Close a file Python provides inbuilt functions for creating, writing, and reading files. There are two types of files that can be handled in python, normal text files and binary files (written in binary language, 0s, and 1s). In this article, we are going to study reading line by line from a file. Learning Ef
7 min read
Break a long line into multiple lines in Python
Break a long line into multiple lines, in Python, is very important sometime for enhancing the readability of the code. Writing a really long line in a single line makes code appear less clean and there are chances one may confuse it to be complex. Example: Breaking a long line of Python code into multiple lines Long Line: a = 1 + 2 + 3 + 4 - 5 * 2
4 min read
PyQtGraph - Setting Symbol of Line in Line Graph
In this article we will see how we can set symbols of line in line graph of the PyQtGraph module. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for displaying data (plots, video, etc.) A li
3 min read
PyQtGraph - Setting Shadow Pen of Line in Line Graph
In this article, we will see how we can set shadow pen of the line in line graph of the PyQtGraph module. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for displaying data (plots, video, et
3 min read
PyQtGraph - Setting Pen of Line in Line Graph
In this article we will see how we can set pen of the line in line graph of the PyQtGraph module. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for displaying data (plots, video, etc.) A li
4 min read
PyQtGraph - Setting Alpha Value of Line in Line Graph
In this article we will see how we can set alpha value of the line in line graph of the PyQtGraph module. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for displaying data (plots, video, et
4 min read
PyQtGraph - Clearing the Line in Line Graph
In this article, we will see how we can clear the line of line graph in the PyQtGraph module. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for displaying data (plots, video, etc.) A line c
3 min read
PyQtGraph - Getting Data Bounds of the Line in Line Graph
In this article we will see how we can get data bounds of the line of line graph in the PyQtGraph module. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for displaying data (plots, video, et
4 min read
PyQtGraph - Getting Data of Line in Line Graph
In this article we will see how we can get data of the line of line graph in the PyQtGraph module. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for displaying data (plots, video, etc.) A l
3 min read
PyQtGraph - Getting Name of Line in Line Graph
In this article we will see how we can get name of the line of line graph in the PyQtGraph module. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for displaying data (plots, video, etc.) A l
4 min read
PyQtGraph - Setting Symbol Pen of Line in Line Graph
In this article we will see how we can set symbol pen of line in line graph of the PyQtGraph module. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for displaying data (plots, video, etc.) A
4 min read
PyQtGraph - Setting Symbol Size of Line in Line Graph
In this article we will see how we can set symbol size of line in line graph of the PyQtGraph module. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for displaying data (plots, video, etc.)
4 min read
PyQtGraph - Getting Pixel Padding of Line in Line Graph
In this article we will see how we can get pixel padding of the line of line graph in the PyQtGraph module. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for displaying data (plots, video,
4 min read
PyQtGraph - Setting Symbol Brush of Line in Line Graph
In this article we will see how we can set symbols brush of line in line graph of the PyQtGraph module. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for displaying data (plots, video, etc.
3 min read
PyQtGraph - Getting Starting Co-ordinates of Line in Line Graph
In this article, we will see how we get the starting coordinates of the line i.e origin of the line of line graph in the PyQtGraph module. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for
4 min read
PyQtGraph - Setting X and Y Co-ordinates of Line in Line Graph
In this article, we will see how we can set X & Y coordinates of the line i.e origin of the line of line graph in the PyQtGraph module. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for
3 min read