How does python evaluate conditional statements?

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Aug 2009
Posts: 35
Reputation: nunos is an unknown quantity at this point 
Solved Threads: 0
nunos nunos is offline Offline
Light Poster

How does python evaluate conditional statements?

 
0
  #1
Aug 14th, 2009
(a | b):
For what I have read in other forum post, if a is true, python doesn't evaluate b an returns a.

(a & b):
What happens with logical and? If a is false, does python evaluates b or not?

Thanks for your help.
Last edited by nunos; Aug 14th, 2009 at 4:30 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,072
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 268
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: How does python evaluate conditional statements?

 
1
  #2
Aug 14th, 2009
Originally Posted by nunos View Post
(a & b):
What happens with logical and? If a is false, does python evaluates b or not?
No. Here's the code I just used to check this:
  1. >>> def a():
  2. ... print 'a'
  3. ... return False
  4. ...
  5. >>> def b():
  6. ... print 'b'
  7. ... return True
  8. ...
  9. >>> a() and b()
  10. a
  11. False
  12. >>> b() and a()
  13. b
  14. a
  15. False
  16. >>>
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 35
Reputation: nunos is an unknown quantity at this point 
Solved Threads: 0
nunos nunos is offline Offline
Light Poster

Re: How does python evaluate conditional statements?

 
0
  #3
Aug 14th, 2009
The reason I asked about the evaluation of the logical and was because of this error I keep getting.

Here's the code:
  1. import re
  2.  
  3. m = "03-AA-12"
  4.  
  5. x = re.compile("[A-Z]{2,2}-[0-9]{2,2}-[0-9]{2,2}")
  6.  
  7. objx = x.match(m)
  8.  
  9. if (objx != None) & (objx.group() == m):
  10. print "yes"
  11. else:
  12. print "no"

Here's the error:
Traceback (most recent call last):
  File "D:\Workspace\py.LAB\src\lab.py", line 9, in <module>
    if (objx != None) & (objx.group() == m):
AttributeError: 'NoneType' object has no attribute 'group'

According to what was mentioned before, I can't understand why this error occurs.

Python should only evaluate objx.group() == m if objx != None is True, since it is False it should not evaluate it and this error would not occur.

To overcome this I nested two if statements, like so:
  1. if objx != None:
  2. if objx.group() == m:
  3. print "yes"

Even though this last piece of code works I still would like to know why the error occurs.

Thanks.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 984
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 222
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark

Re: How does python evaluate conditional statements?

 
0
  #4
Aug 14th, 2009
The logical and in python is the and operator, not the & which is bitwise and.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 35
Reputation: nunos is an unknown quantity at this point 
Solved Threads: 0
nunos nunos is offline Offline
Light Poster

Re: How does python evaluate conditional statements?

 
0
  #5
Aug 14th, 2009
Originally Posted by Gribouillis View Post
The logical and in python is the and operator, not the & which is bitwise and.
Thanks. I changed '&' to 'and' and it now works as it should.

Cheers.
Last edited by nunos; Aug 14th, 2009 at 7:32 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 470 | Replies: 4
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC