Solved

If Condition with Strings

Posted on 2009-05-12
3
537 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
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 28

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
New to Python, how do I import? 4 49
run a set of files in Python 2 52
Use of python .format to prefill  form. 5 65
Python 3.5.2 32 virtualenv problems 3 35
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…
Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
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 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…

776 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