Checking var is a number?

D

dav.phillips

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
 
L

Laszlo Nagy

(e-mail address removed) í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:

....
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:
Traceback (most recent call last):

Or you can catch the exception:
.... intval = int(s)
.... except ValueError:
.... print "This cannot be converted to an int."
....
This cannot be converted to an int.
Good luck!

Laszlo
 
F

Fredrik Lundh

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>
 
D

dav.phillips

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
 
F

Fredrik Lundh

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
might be somewhat helpful:

http://effbot.org/zone/python-objects.htm

</F>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top