By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,907 Members | 1,832 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,907 IT Pros & Developers. It's quick & easy.

Multiple conditions in a single IF

P: 16
I want to know whether a number is a multiple of 2 and/or 5
But Python doesn't let me put lots of things after the '=='

Here's my code:
Expand|Select|Wrap|Line Numbers
  1. def asdf(n):
  2.     n=str(m)
  3.     if n==2:
  4.         a=True
  5.     elif n==5:
  6.         a=True
  7.     elif m[-1:]==2 or 4 or 6 or 8 or 0 or 5:
  8.         a=True
I also tried:
Expand|Select|Wrap|Line Numbers
  1. elif m[-1:]==2, 4, 6, 8, 0, 5:
But it doesn't make sense

I wish you could help me
Thank you

PS: I know that the program don't make any sense but that's not the real purpose, it's just to simplify.
Nov 16 '10 #1
Share this Question
Share on Google+
4 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
To determine if a variable is a multiple of 2 or 5, use the modulo operator.
Expand|Select|Wrap|Line Numbers
  1. >>> m = 12
  2. >>> m % 2
  3.  
  4. >>> m % 5
  5. 2
  6. >>> if not m % 2:
  7. ...     print "m is a multiple of 2"
  8. ...     
  9. m is a multiple of 2
  10. >>> 
To check multiple conditions in one statement:
Expand|Select|Wrap|Line Numbers
  1. >>> m = 15
  2. >>> if not m % 2 or not m % 5:
  3. ...     print "m is a multiple of 2 or m is a multiple of 5"
  4. ...     
  5. m is a multiple of 2 or m is a multiple of 5
  6. >>> not m % 2 or not m % 5
  7. True
  8. >>> not m % 2
  9. False
  10. >>> not m % 5
  11. True
  12. >>> 
A variable can also be checked for membership:
Expand|Select|Wrap|Line Numbers
  1. >>> m in [3,6,9,12,15]
  2. True
  3. >>> 
Nov 16 '10 #2

P: 16
Thank you, but I forgot to say that this funcion will recieve huge numbers, and the method with the module would be very inefficient.
Nov 16 '10 #3

Oralloy
Expert 100+
P: 983
@Gonzalo Gonza - build composite conditions, something like this:

Expand|Select|Wrap|Line Numbers
  1. def asdf(n):
  2.   if (((n % 2) == 0) or ((n % 5) == 0)):
  3.     result = True
  4.   else:
  5.     result = False
  6.  
  7. def fdsa(n)
  8.   result = (((n % 2) == 0) or ((n % 5) == 0))
  9.  
If I've a syntax error, please forgive me; I'm looking at python and trying to learn.
Nov 16 '10 #4

Expert 100+
P: 624
Note that the variable "m" in not known to the function
Expand|Select|Wrap|Line Numbers
  1. def asdf(n):
  2.     n=str(m)
Also
Expand|Select|Wrap|Line Numbers
  1.     elif m[-1:]==2 or 4 or 6 or 8 or 0 or 5:
  2. #
  3. #   m would have to be a string or list to do a [-1], so compare 
  4. #   to strings  (compare to integers is m is a list of integers)
  5.     elif m[-1] in ['2', '4', '6', '8', '0', '5']: 
[2, 4, 6, 8, 0, 5] should give you all numbers divisible by 2 or 5, I think.
Nov 17 '10 #5

Post your reply

Sign in to post your reply or Sign up for a free account.