How to assign values to variables in Python and other languages
This article discusses methods to assign values to variables.
Method 1: Direct Initialisation Method
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 Methodimport 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 directlya = 5# printing value of aprint ("The value of a is: " + str(a)) |
C#
// C# code to demonstrate variable assignment// upon condition using Direct Initialisation Methodusing 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
Method 2: Using Conditional Operator (?:)
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 Operatorimport 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 Operatora = 1 if 20 > 10 else 0# Printing value of aprint("The value of a is: " , str(a))# This code is contributed by shivanisinghss2110 |
C#
// C# code to demonstrate variable assignment// upon condition using Conditional Operatorusing 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-elsea = 1 if 20 > 10 else 0# printing value of aprint ("The value of a is: " + str(a)) |
Output:
The value of a is: 1
Attention reader! Don’t stop learning now. Join the First-Step-to-DSA Course for Class 9 to 12 students , specifically designed to introduce data structures and algorithms to the class 9 to 12 students


