how can i prove that a number is an integer in python?
RMartins 0 Newbie Poster
Recommended Answers
Jump to PostYou can also use
isinstance()
method to check whether an object is an instance or type of a class[everything in Python is an object]>>> v=9 >>> if isinstance(v, int): print 'int' int
Jump to Postbut how can i prove for example that the result of sqrt(n) is an integer?
You can do this (maybe is there more elegant way)
if math.sqrt(n) == float(int(math.sqrt(n))): print "sqrt(n) is an integer"
Jump to PostOr,
if math.sqrt(n) % 1 == 0: print "integer square root!" else: print "not."
Jump to PostCreate a new thread for this question. People with questions like yours will often search on the post name, and it's helpful to have it separate from the lead question.
Jeff
All 13 Replies
Dr_Pepper 0 Newbie Poster
RMartins 0 Newbie Poster
sneekula 969 Nearly a Posting Maven
katharnakh 7 Posting Whiz in Training
jice 53 Posting Whiz in Training
vegaseat commented: nice work +7
RMartins 0 Newbie Poster
jrcagle 77 Practically a Master Poster
RMartins 0 Newbie Poster
jrcagle 77 Practically a Master Poster
pythonBasic 0 Newbie Poster
pythonBasic 0 Newbie Poster
Gribouillis 1,391 Programming Explorer Team Colleague
pythonBasic 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.