📜  如何在Python和其他语言中为变量赋值

📅  最后修改于: 2022-05-13 01:54:27.887000             🧑  作者: Mango

如何在Python和其他语言中为变量赋值

本文讨论了为变量赋值的方法。

方法一:直接初始化方法

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

// this code is contributed by shivanisinghss2110


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


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


输出:
The value of a is: 5

方法 2:使用条件运算符 (?:)

以下是其他流行语言的语法。

C++

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


输出:
The value of a is: 1

在Python中使用 if-else 代替条件运算符 (?:)

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))
输出:
The value of a is: 1