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

Determine if a number is divisble by 2

P: 1
How do i write an if statement that determines if a number is divisble by 2? Please help
Sep 4 '10 #1
Share this Question
Share on Google+
3 Replies


Expert 100+
P: 621
If you don't pay attention in class it's going to be a long, long semester. http://lmgtfy.org/?q=python+%22even+number%22
Sep 5 '10 #2

bvdet
Expert Mod 2.5K+
P: 2,851
Learn about the % (modulo) operator here. Example:
Expand|Select|Wrap|Line Numbers
  1. >>> var = 67
  2. >>> not var%2
  3. False
  4. >>> var = 66
  5. >>> not var%2
  6. True
  7. >>> 
Sep 5 '10 #3

Thekid
100+
P: 145
What about something like this:

Expand|Select|Wrap|Line Numbers
  1. n = raw_input('give a number: ')
  2. x=(int(n))
  3. if x%2==0:
  4.    print '%i is divisible by 2' % (x)
  5. else:
  6.    print '%i is not divisible by 2' % (x)
  7.  
Sep 10 '10 #4

Post your reply

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