How to assign values to variables in Python and other languages

This articles discusses methods to assign values to variables.

Method 1: Direct Initialisation Method

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


C

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


C++

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

The value of a is: 5

Method 2: Using Conditional Operator (?:)

Below is syntax in other popular languages.



C

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


C++

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

The value of a is: 1

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

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output:

The value of a is: 1


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : VartikaBhatnagar1



Article Tags :

18


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.