Assigning Values to Variables
Last Updated :
04 Jul, 2024
In this article, we will learn how to assign values to variables in Python and other languages also.
Assigning Values to Variables in Python
Below are the steps and methods by which we can assign values to variables in Python and other languages:
Assign Values to Variables Direct Initialisation Method
In this method, we will directly assign the value in Python but in other programming languages like C, and C++, we have to first initialize the data type of the variable. So, In Python, there is no need for explicit declaration in variables as compared to using some other programming languages. We can start using the variable right away.
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
// initialising variables directly
int a = 5;
// printing value of a
cout << "The value of a is: " << a;
}
C
#include <stdio.h>
int main()
{
// initialising variables directly
int a = 5;
// printing value of a
printf("The value of a is: %d", a);
}
Java
import java.io.*;
class GFG {
public static void main(String args[])
{
// initialising variables directly
int a = 5;
// printing value of a
System.out.println("The value of a is: " + a);
}
}
Python
# initialising variable directly
a = 5
# printing value of a
print ("The value of a is: " + str(a))
C#
using System;
class GFG{
public static void Main(String []args)
{
// Initialising variables directly
int a = 5;
// Printing value of a
Console.Write("The value of a is: " + a);
}
}
JavaScript
<script>
// JavaScript code to demonstrate variable assignment
// upon condition using Direct Initialisation Method
// initialising variables directly
var a = 5;
// printing value of a
document.write("The value of a is: " + a);
</script>
// this code is contributed by shivanisinghss2110
OutputThe value of a is: 5
Python Variables - Assign Multiple Values
Unlike other languages, we can easily assign values to multiple variables in python easily.
Python
# Assigning multiple values in single line
a,b,c="geeks","for","geeks"
print(a+b+c)
Assign Values to Variables Using Conditional Operator
This method is also called Ternary operators. So Basic Syntax of a Conditional Operator is:-
condition? True_value : False_Value
Using Conditional Operator, we can write one line code in python. The conditional operator works in such a way that first evaluates the condition, if the condition is true, the first expression( True_value) will print else evaluates the second expression(False_Value).
Below is the syntax in other popular languages.
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
// initialising variables using Conditional Operator
int a = 20 > 10 ? 1 : 0;
// printing value of a
cout << "The value of a is: " << a;
}
C
#include <stdio.h>
int main()
{
// initialising variables using Conditional Operator
int a = 20 > 10 ? 1 : 0;
// printing value of a
printf("The value of a is: %d", a);
}
Java
import java.io.*;
class GFG {
public static void main(String args[])
{
// initialising variables using Conditional Operator
int a = 20 > 10 ? 1 : 0;
// printing value of a
System.out.println("The value of a is: " + a);
}
}
Python
a = 1 if 20 > 10 else 0
# Printing value of a
print("The value of a is: " , str(a))
# This code is contributed by shivanisinghss2110
C#
using System;
class GFG {
public static void Main(String []args)
{
// initialising variables using Conditional Operator
int a = 20 > 10 ? 1 : 0;
// printing value of a
Console.Write("The value of a is: " + a);
}
}
// this code is contributed by shivanisinghss2110
JavaScript
<script>
// JavaScript code to demonstrate variable assignment
// upon condition using Conditional Operator
// initialising variables using Conditional Operator
var a = 20 > 10 ? 1 : 0;
// printing value of a
document.write("The value of a is: " + a);
// This code is contributed by shivanisinghss2110
</script>
OutputThe value of a is: 1
Python One liner Conditional Statement Assigning
In the another example, we have used one liner if-else conditional statement in Python.
Python
#one liner if-else
a = 1 if 20 > 10 else 0
# printing value of a
print ("The value of a is: " + str(a))
OutputThe value of a is: 1
Similar Reads
How to use Variables in Python3? Variable is a name for a location in memory. It can be used to hold a value and reference that stored value within a computer program. the interpreter allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to the variables, you can store
3 min read
Assigning multiple variables in one line in Python 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 val
2 min read
R Variables - Creating, Naming and Using Variables in R A variable is a memory location reserved for storing data, and the name assigned to it is used to access and manipulate the stored data. The variable name is an identifier for the allocated memory block, which can hold values of various data types during the programâs execution.In R, variables are d
5 min read
Setting and Retrieving values of Tkinter variable - Python In Tkinter, control variables are special variables used to link Python values with widgets like Entry, Label and Checkbutton. They act like regular variables but are designed to work with the GUI. These special variables are wrappers around Python data types and can be linked to widget values using
4 min read
Python Variables In Python, variables are used to store data that can be referenced and manipulated during program execution. A variable is essentially a name that is assigned to a value. Unlike many other programming languages, Python variables do not require explicit declaration of type. The type of the variable i
6 min read