Solved

If Condition with Strings

Posted on 2009-05-12
3
543 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 May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Installing Python 2.7.3 version on Windows operating system For installing Python first we need to download Python's latest version from URL" www.python.org " You can also get information on Python scripting language from the above mentioned we…
Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we…
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 while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

739 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