LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-07-2009, 09:09 AM   #1
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Python variable type


I wonder how to know if a variable in Python is a number, list, or string?
 
Old 12-07-2009, 10:07 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Code:
>>> a = [5]
>>> b = "5"
>>> c = 5

>>> type(a)
<type 'list'>

>>> type(b)
<type 'str'>

>>> type(c)
<type 'int'>

>>> if isinstance(a, list): print "Yes, it is a list!"
... 
Yes, it is a list!
 
Old 12-07-2009, 10:10 AM   #3
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
I thought that Python doesn't tell any difference between int and float!?

Also, I wonder how to split a string into a list of words, and how to check if a variable does not equal any one of the contents of a list?

Last edited by MTK358; 12-07-2009 at 10:12 AM.
 
Old 12-07-2009, 11:02 AM   #4
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by MTK358 View Post
I thought that Python doesn't tell any difference between int and float!?
Python would disagree with you:
Code:
C:\>python
Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 5 / 6
0
>>> 5.0 / 6.0
0.83333333333333337
>>>
 
Old 12-07-2009, 02:49 PM   #5
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 232Reputation: 232Reputation: 232
Quote:
Also, I wonder how to split a string into a list of words, and how to check if a variable does not equal any one of the contents of a list?
Code:
mylist="This is a string to split".split()
is_in_list="unique" in mylist
print "unique is in mylist=",mylist,is_in_list
 
Old 12-07-2009, 05:53 PM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
and finally, head down to the Python documentation site, (my sig) to learn all about it.
 
Old 12-08-2009, 04:11 AM   #7
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by MTK358 View Post
Also, I wonder how to split a string into a list of words, and how to check if a variable does not equal any one of the contents of a list?
When you were looking for how to split a string, I wonder how did you manage not to find the "split" method of a string in the docs?

Code:
>>> someString = "aap noot mies wim zus jet teun"
>>> someList = someString.split()
>>> type(someList)
<type 'list'>
>>> someList
['aap', 'noot', 'mies', 'wim', 'zus', 'jet', 'teun']
>>> if "zus" in someList: print "Yes, it is in the list!"
... 
Yes, it is in the list!

Last edited by Hko; 12-08-2009 at 04:15 AM.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ variable type trick... ArthurHuang Programming 4 02-15-2008 07:41 PM
Python Global Variable? ElementNine Programming 5 11-05-2007 12:53 PM
understand variable type in shell script qrshat Programming 5 09-09-2006 02:12 AM
python variable question bendeco13 Programming 6 02-03-2005 07:22 AM
python global variable? goestin Programming 1 01-20-2005 06:52 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:03 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration