how can i prove that a number is an integer in python?

Recommended Answers

All 13 Replies

for Ex.

x = 10
print x
print type (x)

it should show you that x ia 10 and its interger "<type 'int'>".

but how can i prove for example that the result of sqrt(n) is an integer?

The 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).

You 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

but 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"
commented: nice work +7

thank you all for your help... especially jice;)

Or,

if math.sqrt(n) % 1 == 0:
   print "integer square root!"
else:
   print "not."

does anybody knows why this function doesn´t work very well:

f=lambda n: reduce(lambda x,y:x**2+y**2, map(int,str(n)))

I want that this function add the square of the digits of a number

an example of what i want:

f(442)=4**2+4**2+2**2=36

Create 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

hello all,

can some suggest me if i need to fire http request multiple times in python

can some one suggest me below
If i need to fire http request multiple times in python.Please suggest me steps as i am new to python

@pythonBasic Please don't revive or highjack old threads. Start your own new discussion instead if you have questions.

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.