how can i prove that a number is an integer in python?
RMartins
0
Newbie Poster
Recommended Answers
Jump to Postfor Ex.
x = 10
print x
print type (x)it should show you that x ia 10 and its interger "<type 'int'>".
Jump to PostThe function sqrt(x) returns a float:
import math x = 9 y = math.sqrt(x) print y, type(y) # 3.0 <type 'float'>
If you want the result to be an integer use int(y).
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"
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.