Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics Types and Branching Comparisons

Alan Luo
Alan Luo
223 Points

If I used the wrong type of data, such as Int to Str or Str to Int, how do I check on what exact type of data am I using

Here is the original code from Craig. # This is just in case we have a younger user who can't yet read. age = int(input("How old are you? ")) if age <= 6:

On the second Craig changed it to integer[ age = int(input("How old are you? ")) ], but what if I forgot to change it when I am doing my own code? and I assumed I will have an error in my code. Because string can't compare with the integer. So if I don't know what type of code I wrote, rather than a string or integer, how do I find out what type of code I wrote?

2 Answers

Steven Parker
Steven Parker
231,198 Points

Comparing a string to an integer won't cause a code error, but it probably won't work as expected. But you can usually tell what type something will be based on where it comes from. For example, anything you get from the "input()" function will always be a string.

Python also has a "type()" function for testing a variable type directly, but it should not normally be needed since you can generally determine the type just from examining the code.

Alan Luo
Alan Luo
223 Points

What if you have integer, float, bool, string, list, tuple, and set, on the same code page? My question is more like if I want how do I ask python to tell me what types of data they are? Is there a specific code to let python state what data type that is? For instance: if I wrote c=[10,20,30] then python will tell me that is a LIST.

Steven Parker
Steven Parker
231,198 Points

That's where the "type()" function is handy:

>>> c=[10,20,30]                                                                                   
>>> type(c)                                                                                        
<class 'list'>                                                                                     
Alan Luo
Alan Luo
223 Points

Thanks a lot, man, that's exactly what I wanted.