检查变量是否已初始化的哪种方法更好/更正?(假设变量可以包含任何内容(字符串,整数,对象,函数等))
if (elem) { // or !elem
Run Code Online (Sandbox Code Playgroud)
要么
if (typeof(elem) !== 'undefined') {
Run Code Online (Sandbox Code Playgroud)
要么
if (elem != null) {
Run Code Online (Sandbox Code Playgroud) 我如何IF...THEN在SQL SELECT声明中执行?
例如:
SELECT IF(Obsolete = 'N' OR InStock = 'Y' ? 1 : 0) AS Saleable, * FROM Product
Run Code Online (Sandbox Code Playgroud) 可能重复:
Python三元运算符
我刚刚进入Python,我非常喜欢语法的简洁性.然而,有没有写一个更简单的方法if- then- else语句,因此它适合在同一行?
例如:
if count == N:
count = 0
else:
count = N + 1
Run Code Online (Sandbox Code Playgroud)
有没有更简单的写作方式?我的意思是,在Objective-C中我会把它写成:
count = count == N ? 0 : count + 1;
Run Code Online (Sandbox Code Playgroud)
Python有类似的东西吗?
更新
我知道在这种情况下我可以使用count == (count + 1) % N.
我问的是一般语法.
这是我的代码:
def front_back(a, b):
# +++your code here+++
if len(a) % 2 == 0 && len(b) % 2 == 0:
return a[:(len(a)/2)] + b[:(len(b)/2)] + a[(len(a)/2):] + b[(len(b)/2):]
else:
#todo! Not yet done. :P
return
Run Code Online (Sandbox Code Playgroud)
我在IF条件中遇到错误.我究竟做错了什么?
我想在AngularJS模板中做一个条件.我从Youtube API获取视频列表.一些视频的比例为16:9,有些视频的比例为4:3.
我想做一个像这样的条件:
if video.yt$aspectRatio equals widescreen then
element's attr height="270px"
else
element's attr height="360px"
Run Code Online (Sandbox Code Playgroud)
我正在使用迭代视频ng-repeat.我不知道该怎么办这个条件:
有时我会将ifs中的长条件分成几行.最明显的方法是:
if (cond1 == 'val1' and cond2 == 'val2' and
cond3 == 'val3' and cond4 == 'val4'):
do_something
Run Code Online (Sandbox Code Playgroud)
视觉上不是很吸引人,因为动作与条件相融合.但是,这是使用4个空格的正确Python缩进的自然方式.
目前我正在使用:
if ( cond1 == 'val1' and cond2 == 'val2' and
cond3 == 'val3' and cond4 == 'val4'):
do_something
Run Code Online (Sandbox Code Playgroud)
但这不是很漂亮.:-)
你能推荐另一种方式吗?
我正在尝试创建一个函数,将多个变量与一个整数进行比较,并输出一个由三个字母组成的字符串.我想知道是否有办法将其翻译成Python.所以说:
x = 0
y = 1
z = 3
mylist = []
if x or y or z == 0 :
mylist.append("c")
if x or y or z == 1 :
mylist.append("d")
if x or y or z == 2 :
mylist.append("e")
if x or y or z == 3 :
mylist.append("f")
Run Code Online (Sandbox Code Playgroud)
这将返回一个列表
["c", "d", "f"]
Run Code Online (Sandbox Code Playgroud)
这样的事情可能吗?
我正在尝试使用Bash(使用Ubuntu)获得一个if声明:
#!/bin/bash
s1="hi"
s2="hi"
if ["$s1" == "$s2"]
then
echo match
fi
Run Code Online (Sandbox Code Playgroud)
我尝试了各种形式的if语句,使用[["$s1" == "$s2"]],有和没有引号,使用=,==和-eq,但我仍然得到以下错误:
[hi:命令未找到
我查看了各种网站和教程并复制了这些内容,但是它不起作用 - 我做错了什么?
最后,我想说是否$s1包含$s2,所以我该怎么做?
我刚刚解决了空位...:/我怎么说包含?
我试过了
if [[ "$s1" == "*$s2*" ]]
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
我正在使用Angular,我希望*ngIf else在此示例中使用(自版本4起可用):
<div *ngIf="isValid">
content here ...
</div>
<div *ngIf="!isValid">
other content here...
</div>
Run Code Online (Sandbox Code Playgroud)
我怎样才能实现同样的行为ngIf else?
我正在尝试进行简单的条件检查,但它似乎不起作用.
如果$#等于0或大于1那么打个招呼.
我尝试了以下语法但没有成功:
if [ "$#" == 0 -o "$#" > 1 ] ; then
echo "hello"
fi
if [ "$#" == 0 ] || [ "$#" > 1 ] ; then
echo "hello"
fi
Run Code Online (Sandbox Code Playgroud) if-statement ×10
python ×4
bash ×2
and-operator ×1
angular ×1
angularjs ×1
case ×1
coding-style ×1
comparison ×1
javascript ×1
keyword ×1
match ×1
scripting ×1
sh ×1
sql ×1
sql-server ×1
syntax ×1
t-sql ×1
undefined ×1
unix ×1
variables ×1