[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

If Condition with Strings

Posted on 2009-05-12
3
Medium Priority
?
550 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 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'

Open in new window

0
 
LVL 8

Assisted Solution

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

Open in new window

0
 
LVL 29

Assisted Solution

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
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 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 …

650 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