本文概述
- Python3
- C
- C ++
- Java
- C
- C ++
- Java
- Python3
方法1:直接初始化方法
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#include <
stdio.h>
int main()
{
// initialising variables directly
int a = 5;
// printing value of a
printf ( "The value of a is: %d" , a);
}
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;
}
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);
}
}
输出如下:
The value of a is: 5
方法2:使用条件运算符(?:)
以下是其他流行语言的语法。
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);
}
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;
}
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);
}
}
输出如下:
The value of a is: 1
一个内联if-else代替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))
输出如下:
The value of a is: 1
注意怪胎!巩固你的基础Python编程基础课程和学习基础知识。
【如何使用Python和其他语言为变量赋值】首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。
推荐阅读
- 算法(一个检查字符串是否相互旋转的程序)
- JavaScript 严格模式解析和使用示例
- 5步骤简单处理XP系统响应速度慢
- 让xp文件下各种安全警告提示统统消失的妙招
- 让win xp系统定时关机的小妙招
- 迅速找到xp系统下bits服务的小攻略
- 让xp简单拥有win7任务栏的小妙招
- 调整显卡分辨率,让xp远离黑屏干扰!
- 图文详细说明:xp系统字体安装遇到的那些事