Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Checking var is a number?

Reply
Thread Tools

Checking var is a number?

 
 
dav.phillips@ntlworld.com
Guest
Posts: n/a
 
      06-06-2006
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

 
Reply With Quote
 
 
 
 
Laszlo Nagy
Guest
Posts: n/a
 
      06-06-2006
http://www.velocityreviews.com/forums/(E-Mail 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:

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


 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      06-06-2006
(E-Mail Removed) 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>

 
Reply With Quote
 
dav.phillips@ntlworld.com
Guest
Posts: n/a
 
      06-06-2006
> Good luck!
> Laszlo


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

Cheers
David P

 
Reply With Quote
 
dav.phillips@ntlworld.com
Guest
Posts: n/a
 
      06-06-2006
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

 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      06-06-2006
(E-Mail Removed) 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
might be somewhat helpful:

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

</F>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between Session["var"] and Session("var") thomson ASP .Net 10 06-20-2005 01:02 PM
Difference between Session["var"] and Session("var") thomson ASP .Net 0 06-20-2005 10:54 AM
Threads.. Session var lost, App var ok Alvin Bruney ASP .Net 1 12-02-2003 01:56 AM
Invoking constructor : Foo(var) and Foo ins(var) Alex Vinokur C++ 4 09-24-2003 04:01 PM
does "struct_name var = { 0 }; " fill var with 0? Fred C++ 3 08-10-2003 09:44 AM



Advertisments