Solved

If Condition with Strings

Posted on 2009-05-12
3
544 Views
Last Modified: 2012-05-06
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
Comment
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
  • Learn & ask questions
3 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 150 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'

Open in new window

0
 
LVL 8

Assisted Solution

by:LunarNRG
LunarNRG earned 80 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'
    

Open in new window

0
 
LVL 29

Assisted Solution

by:pepr
pepr earned 20 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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the d…
A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question