# Checking var is a number?

Discussion in 'Python' started by dav.phillips@ntlworld.com, Jun 6, 2006.

1. ### Guest

Hi,
I am very new to all this and need to know how to check
a variable to see if it is a number or not. Also can anyone
recommend a reference book apart from dive into python
preferably a reference with good examples of how to impliment
code.

The project i have been given to work in is all CGI written
in Python.

Many Thanks
David Phillips

, Jun 6, 2006

2. ### Laszlo NagyGuest

írta:
> Hi,
> I am very new to all this and need to know how to check
> a variable to see if it is a number or not. Also can anyone
> recommend a reference book apart from dive into python
> preferably a reference with good examples of how to impliment
> code.
>

There are different types of number in Python.

Integers and Long integers (type: 'int', 'long')
Decimal numbers (class 'decimal.Decimal')
Floating point numbers (type 'float')

You can check this way:

import decimal

def print_type(var):
if isinstance(var,int):
print "this is an integer"
elif isinstance(var,long):
print "this is a long integer"
elif isinstance(var,decimal.Decimal):
print "this is a decimal"
elif isinstance(var,float):
print "this is a float"
else:
print "this is something else..."

Test this:

....
>>> print_type(12)

this is an integer
>>> print_type(12L)

this is a long integer
>>> print_type(3.5)

this is a float
>>> print_type('hello world')

this is something else...
>>> print_type('44')

this is something else...
>>> d = Decimal('123')
>>> print_type(d)

this is a decimal
>>>

> The project i have been given to work in is all CGI written
> in Python.
>

Probaby you wanted to convert a string into a number? For example:

>>> int(s)

1234
>>> s = 'Foo'
>>> int(s)

Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: invalid literal for int(): Foo
>>>

Or you can catch the exception:

>>> s = 'Foo2'
>>> try:

.... intval = int(s)
.... except ValueError:
.... print "This cannot be converted to an int."
....
This cannot be converted to an int.
>>>

Good luck!

Laszlo

Laszlo Nagy, Jun 6, 2006

3. ### Fredrik LundhGuest

wrote:

> I am very new to all this and need to know how to check
> a variable to see if it is a number or not.

assuming that "variable" means "string object" and "number" means
"integer", you can use the isdigit predicate:

if var.isdigit():
print "all characters in", var, "are digits"

if you want to check for anything that can be converted to a float, the
best way is to do the conversion and trap any ValueError that may occur:

try:
value = float(var)
except ValueError:
print "not a valid float"

if you want an integer instead, replace "float" with "int".

if you had something else in mind, let us know.

> Also can anyone recommend a reference book apart from dive into python
> preferably a reference with good examples of how to impliment code.

you can find an extensive list of available books here:

http://wiki.python.org/moin/PythonBooks

some on-line code collections:

http://aspn.activestate.com/ASPN/Python/Cookbook/
http://effbot.org/zone/librarybook-index.htm

and don't forget the core references:

http://docs.python.org/lib/
http://docs.python.org/ref/

</F>

Fredrik Lundh, Jun 6, 2006
4. ### Guest

> Good luck!
> Laszlo

I actually managed to get it sorted but i like that way of
doing it much better actually

Cheers
David P

, Jun 6, 2006
5. ### Guest

I took a variable to mean a container for diffirent kinds of
information
either strings or integers etc, as i am mainly a asp, php, asp.net
developer.

Thanks for the list of references, that will come in very handy

Cheers Guys
David P

, Jun 6, 2006
6. ### Fredrik LundhGuest

wrote:

> I took a variable to mean a container for diffirent kinds of
> information either strings or integers etc, as i am mainly a
> asp, php, asp.net developer.

in python, a variable is a name that refers to a specific object. it's
the object that has a type and a value, not the variable. this article