Skip to content
Related Articles

Related Articles

How to assign values to variables in Python and other languages

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 30 Nov, 2022
Improve Article
Save Article

This article discusses methods to assign values to variables. 

Method 1: Direct Initialisation Method 

In this method, you will directly assign the value in python but in other programming languages like C, C++, you have to first initialise the data type of variable. So, In Python, there is no need of explicit declaration  in variables as compare to using some other programming languages. You can start using the variable right away.

C++




// C++ code to demonstrate variable assignment
// upon condition using Direct Initialisation Method
 
#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




// C code to demonstrate variable assignment
// upon condition using Direct Initialisation Method
 
#include <stdio.h>
 
int main()
{
    // initialising variables directly
    int a = 5;
 
    // printing value of a
    printf("The value of a is: %d", a);
}

Java




// Java code to demonstrate variable assignment
// upon condition using Direct Initialisation Method
 
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);
    }
}

Python3




# Python 3 code to demonstrate variable assignment
# upon condition using Direct Initialisation Method
 
# initialising variable directly
a = 5
 
# printing value of a
print ("The value of a is: " + str(a))

C#




// C# code to demonstrate variable assignment
// upon condition using Direct Initialisation Method
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);
}
}
  
// This code is contributed by shivanisinghss2110

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

Output

The value of a is: 5

Assigning multiple values to different variables :

Unlike other languages we can easily assign values to multiple variables in python easily.

Java




/*package whatever //do not write package name here */
 
#Assiging multiple values in java
 
import java.io.*;
 
class GFG {
    public static void main (String[] args) {
         
      String a="geeks",b="for",c="geeks";
      System.out.println(a+b+c);
       
    }
}

Python




# Assigning multiple values in single line
 
a,b,c="geeks","for","geeks"
print(a+b+c)

Javascript




# Assigning multiple values in single line
 
let [a,b,c]=["geeks","for","geeks"]
console.log(a+b+c);

Output

geeksforgeeks

Method 2: Using Conditional Operator (?:)

This method is also called Ternary operators. So Basic Syntax of Conditional Operator is:-

                                                                         condition? True_value : False_Value

Using Conditional Operator, you can write one line code in python. The conditional operator works in a such way that first evaluates the condition, if  condition is true , first expression( True_value) will print else evaluates the second expression(False_Value).

Below is the syntax in other popular languages.

C++




// C++ code to demonstrate variable assignment
// upon condition using Conditional Operator
 
#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




// C code to demonstrate variable assignment
// upon condition using Conditional Operator
 
#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




// Java code to demonstrate variable assignment
// upon condition using Conditional Operator
 
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);
    }
}

Python3




# Python3 code to demonstrate variable assignment
# upon condition using Conditional Operator
 
# Initialising variables using Conditional Operator
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#




// C# code to demonstrate variable assignment
// upon condition using Conditional Operator
 
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>

Output

The value of a is: 1

One liner if-else instead of Conditional Operator (?:) in Python

Python3




# Python 3 code to demonstrate variable assignment
# upon condition using One liner if-else
 
# initialising variable using Conditional Operator
# a = 20 > 10 ? 1 : 0 is not possible in Python
# Instead there is one liner if-else
a = 1 if 20 > 10 else 0
 
# printing value of a
print ("The value of a is: " + str(a))

Output

The value of a is: 1

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!