Python在if语句中相当于&&(逻辑)和

时间:2022-11-11 20:48:12

Here's my code:

这是我的代码:

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

I'm getting an error in the IF conditional. What am I doing wrong?

如果条件允许,我就会出错。我做错了什么?

9 个解决方案

#1


981  

You would want and instead of &&.

你会想要而不是&&。

#2


161  

Python uses and and or conditionals.

Python使用和和或条件。

i.e.

即。

if foo == 'abc' and bar == 'bac' or zoo == '123':
  # do something

#3


28  

Two comments:

两个评论:

  • Use and and or for logical operations in Python.
  • 在Python中使用和或用于逻辑操作。
  • Use 4 spaces to indent instead of 2. You will thank yourself later because your code will look pretty much the same as everyone else's code. See PEP 8 for more details.
  • 使用4个空格来缩进,而不是2。稍后您将感谢自己,因为您的代码看起来与其他所有人的代码几乎相同。有关更多细节,请参见PEP 8。

#4


11  

I went with a purlely mathematical solution:

我得到了一个普适的数学解:

def front_back(a, b):
  return a[:(len(a)+1)//2]+b[:(len(b)+1)//2]+a[(len(a)+1)//2:]+b[(len(b)+1)//2:]

#5


6  

Probably this is not best code for this task, but is working -

可能这不是这个任务的最佳代码,但是正在工作

def front_back(a, b):

 if len(a) % 2 == 0 and len(b) % 2 == 0:
    print a[:(len(a)/2)] + b[:(len(b)/2)] + a[(len(a)/2):] + b[(len(b)/2):]

 elif len(a) % 2 == 1 and len(b) % 2 == 0:
    print a[:(len(a)/2)+1] + b[:(len(b)/2)] + a[(len(a)/2)+1:] + b[(len(b)/2):] 

 elif len(a) % 2 == 0 and len(b) % 2 == 1:
     print a[:(len(a)/2)] + b[:(len(b)/2)+1] + a[(len(a)/2):] + b[(len(b)/2)+1:] 

 else :
     print a[:(len(a)/2)+1] + b[:(len(b)/2)+1] + a[(len(a)/2)+1:] + b[(len(b)/2)+1:]

#6


6  

You use and and or to perform logical operations like in C, C++. Like literally and is && and or is ||.

使用和或执行逻辑操作,如在C、c++中。就像字面上的and is & or是||。


Take a look at this fun example,

Say you want to build Logic Gates in Python:

如果你想用Python构建逻辑门:

def AND(a,b):
    return (a and b) #using and operator

def OR(a,b):
    return (a or b)  #using or operator

Now try calling them:

现在试着叫他们:

print AND(False, False)
print OR(True, False)

This will output:

False
True

Hope this helps!

希望这可以帮助!

#7


0  

Use of "and" in conditional. I often use this when importing in Jupyter Notebook:

在条件句中使用“and”。我经常在进口Jupyter笔记本时使用这个:

def find_local_py_scripts():
    import os # does not cost if already imported
    for entry in os.scandir('.'):
        # find files ending with .py
        if entry.is_file() and entry.name.endswith(".py") :
            print("- ", entry.name)
find_local_py_scripts()

-  googlenet_custom_layers.py
-  GoogLeNet_Inception_v1.py

#8


0  

A single & (not double &&) is enough or as the top answer suggests you can use 'and'. I also found this in pandas

一个单独的&(不是双&&)就足够了,或者正如上面的答案所示,你可以使用“and”。我在熊猫身上也发现了这个

cities['Is wide and has saint name'] = (cities['Population'] > 1000000) 
& cities['City name'].apply(lambda name: name.startswith('San'))

if we replace the "&" with "and", it won't work.

如果我们将“&”替换为“和”,它就不工作了。

#9


-1  

maybe with & instead % is more fast and mantain readibility

也许用&代替%更快捷,也更容易保持易读性

other tests even/odd

其他测试偶数/奇数

x is even ? x % 2 == 0

x是?x % 2 = 0

x is odd ? not x % 2 == 0

x是奇数?不是x % 2 = 0

maybe is more clear with bitwise and 1

也许用位和1更清楚

x is odd ? x & 1

x是奇数?x & 1

x is even ? not x & 1 (not odd)

x是?不是x & 1(不是奇数)

def front_back(a, b):
    # +++your code here+++
    if not len(a) & 1 and not len(b) & 1:
        return a[:(len(a)/2)] + b[:(len(b)/2)] + a[(len(a)/2):] + b[(len(b)/2):] 
    else:
        #todo! Not yet done. :P
    return

#1


981  

You would want and instead of &&.

你会想要而不是&&。

#2


161  

Python uses and and or conditionals.

Python使用和和或条件。

i.e.

即。

if foo == 'abc' and bar == 'bac' or zoo == '123':
  # do something

#3


28  

Two comments:

两个评论:

  • Use and and or for logical operations in Python.
  • 在Python中使用和或用于逻辑操作。
  • Use 4 spaces to indent instead of 2. You will thank yourself later because your code will look pretty much the same as everyone else's code. See PEP 8 for more details.
  • 使用4个空格来缩进,而不是2。稍后您将感谢自己,因为您的代码看起来与其他所有人的代码几乎相同。有关更多细节,请参见PEP 8。

#4


11  

I went with a purlely mathematical solution:

我得到了一个普适的数学解:

def front_back(a, b):
  return a[:(len(a)+1)//2]+b[:(len(b)+1)//2]+a[(len(a)+1)//2:]+b[(len(b)+1)//2:]

#5


6  

Probably this is not best code for this task, but is working -

可能这不是这个任务的最佳代码,但是正在工作

def front_back(a, b):

 if len(a) % 2 == 0 and len(b) % 2 == 0:
    print a[:(len(a)/2)] + b[:(len(b)/2)] + a[(len(a)/2):] + b[(len(b)/2):]

 elif len(a) % 2 == 1 and len(b) % 2 == 0:
    print a[:(len(a)/2)+1] + b[:(len(b)/2)] + a[(len(a)/2)+1:] + b[(len(b)/2):] 

 elif len(a) % 2 == 0 and len(b) % 2 == 1:
     print a[:(len(a)/2)] + b[:(len(b)/2)+1] + a[(len(a)/2):] + b[(len(b)/2)+1:] 

 else :
     print a[:(len(a)/2)+1] + b[:(len(b)/2)+1] + a[(len(a)/2)+1:] + b[(len(b)/2)+1:]

#6


6  

You use and and or to perform logical operations like in C, C++. Like literally and is && and or is ||.

使用和或执行逻辑操作,如在C、c++中。就像字面上的and is & or是||。


Take a look at this fun example,

Say you want to build Logic Gates in Python:

如果你想用Python构建逻辑门:

def AND(a,b):
    return (a and b) #using and operator

def OR(a,b):
    return (a or b)  #using or operator

Now try calling them:

现在试着叫他们:

print AND(False, False)
print OR(True, False)

This will output:

False
True

Hope this helps!

希望这可以帮助!

#7


0  

Use of "and" in conditional. I often use this when importing in Jupyter Notebook:

在条件句中使用“and”。我经常在进口Jupyter笔记本时使用这个:

def find_local_py_scripts():
    import os # does not cost if already imported
    for entry in os.scandir('.'):
        # find files ending with .py
        if entry.is_file() and entry.name.endswith(".py") :
            print("- ", entry.name)
find_local_py_scripts()

-  googlenet_custom_layers.py
-  GoogLeNet_Inception_v1.py

#8


0  

A single & (not double &&) is enough or as the top answer suggests you can use 'and'. I also found this in pandas

一个单独的&(不是双&&)就足够了,或者正如上面的答案所示,你可以使用“and”。我在熊猫身上也发现了这个

cities['Is wide and has saint name'] = (cities['Population'] > 1000000) 
& cities['City name'].apply(lambda name: name.startswith('San'))

if we replace the "&" with "and", it won't work.

如果我们将“&”替换为“和”,它就不工作了。

#9


-1  

maybe with & instead % is more fast and mantain readibility

也许用&代替%更快捷,也更容易保持易读性

other tests even/odd

其他测试偶数/奇数

x is even ? x % 2 == 0

x是?x % 2 = 0

x is odd ? not x % 2 == 0

x是奇数?不是x % 2 = 0

maybe is more clear with bitwise and 1

也许用位和1更清楚

x is odd ? x & 1

x是奇数?x & 1

x is even ? not x & 1 (not odd)

x是?不是x & 1(不是奇数)

def front_back(a, b):
    # +++your code here+++
    if not len(a) & 1 and not len(b) & 1:
        return a[:(len(a)/2)] + b[:(len(b)/2)] + a[(len(a)/2):] + b[(len(b)/2):] 
    else:
        #todo! Not yet done. :P
    return