Open In App

Assigning Values to Variables

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

In this article, we will 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);
    }
}


Python3




# 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


Output

The value of a is: 5


Python Variables – Assign Multiple Values

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

Python3




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


Output

geeksforgeeks


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);
    }
}


Python3




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>


Output

The 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.

Python3




#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




Last Updated : 25 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads