Solved

# If Condition with Strings

Posted on 2009-05-12
Medium Priority
546 Views
Hi,

Very short question.

I want to check this condition.

list = ['3', '5', '8']

if list[0] != NA & list[0] != NA & list[0] != NA:
print 'No NAs'
else:
print 'there is at least one NA'

But i get the following error:
TypeError: unsupported operand type(s) for &: 'str' and 'str'

What's the shortest way to do what i want to do without nested if else???! don't wanna use nested if else.
0
Question by:dfernan
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 53

Accepted Solution

Infinity08 earned 600 total points
ID: 24362115
& is the bitwise AND operator. I guess you intended to use the logical AND operator :

``````if list[0] != NA and list[1] != NA and list[2] != NA:
print 'No NAs'
else:
print 'there is at least one NA'
``````
0

LVL 8

Assisted Solution

LunarNRG earned 320 total points
ID: 24362840
You should avoid rebinding built-in names. In other words, don't use 'list' as a variable name.

Here's another option ...

``````l = ['3', '5', '8']
NA = 'NA'

if NA not in l:
print 'No NAs'
else:
print 'there is at least one NA'

# ... or ...

if NA in l:
print 'there is at least one NA'
else:
print 'No NAs'

``````
0

LVL 29

Assisted Solution

pepr earned 80 total points
ID: 24363667
l (small L) is also not very practical identifier. It can be considered I or 1 depending on the used font. The lst identifier is quite usual in the cases. Otherwise, LunarNRG's solution is fine. Notice also that there is less need to assign

NA = 'NA'

as the 'NA' is used on a single place.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
###### Suggested Courses
Course of the Month12 days, 7 hours left to enroll