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

Michael Werlinger
Michael Werlinger
1,259 Points

Python Basics - Numbers class - What am I doing wrong?!

In Workspaces, make a float() variable (e.g. float_a = 1.2) and use dir() on it. Which of the following is not a method of the float class?

Choose the correct answer below:

  • A to_int()
  • B as_integer_ratio
  • C is_integer
  • D conjugate

** I found the answer but I shouldn't have to beat my head into a wall this much to find an answer, it shouldn't be this hard. All help and explanation is appreciated.

Alright so lets hit the obvious

>>> float('2')                                                                                                                                                                                                   
2.0                                                                                                                                                                                                              
>>> dir('float')                                                                                                                                                                                                 
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getat                                                                                         
tribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__'                                                                                         
, '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setat                                                                                         
tr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', '                                                                                         
expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'isl                                                                                         
ower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'pa                                                                                         
rtition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith',                                                                                          
'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']                                                                                                                                                     
>>> dir(float)                                                                                                                                                                                                   
['__abs__', '__add__', '__bool__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getformat__', '__getnewa
rgs__', '__gt__', '__hash__', '__init__', '__int__', '__le__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__pos__', '__pow__', '__radd__', '__rdivmod__', '__reduce__', '__reduce_ex__', '_
_repr__', '__rfloordiv__', '__rmod__', '__rmul__', '__round__', '__rpow__', '__rsub__', '__rtruediv__', '__setattr__', '__setformat__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '_
_trunc__', 'as_integer_ratio', 'conjugate', 'fromhex', 'hex', 'imag', 'is_integer', 'real']                                                                                                                      
>>> float(2)                                                                                                                                                                                                     
2.0        

hmmm, lets not give up yet! ONWARD!!!

>>> float_a = 1.2                                                                                                                                                                                                
>>> dir('float_a')                                                                                                                                                                                               
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getat                                                                                         
tribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__'                                                                                         
, '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setat                                                                                         
tr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', '                                                                                         
expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'isl                                                                                         
ower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'pa                                                                                         
rtition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith',                                                                                          
'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

Ummmm HUSTON, We have confusion.

  1. >>Why is dir('float') and dir(float) getting two different answers?!
  2. >>What is the difference between float(2) and float('2')
  3. >>At that, what is the difference if I have a (" ") in there at all?

HELP!

[edit formating -cf]

Michael Werlinger
Michael Werlinger
1,259 Points

by utilizing

>>>float(2)
2.0
>>>dir(float) 
# the correct answer can be found! :-D

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points
  • Why is dir('float') and dir(float) getting two different answers?!

dir() lists the attributes of an object. dir('float') lists the attributes of the string object "float", while dir(float) lists the attributes of the builtin function float. Try typing just float at the command prompt:

>>> float
<class 'float'>
  • What is the difference between float(2) and float('2')

The first creates a float from the integer 2, the second from the string "2". The end result is is the same: both will return the float value 2.0.

  • At that, what is the difference if I have a (" ") in there at all?

The function float() will try to create a floating point number, if it can, from whatever you provide. Usually you would be creating the float from a variable and there would not be quotes, though the variable could be a string or an integer. In this case, since if you're typing float(2) or float("2") you would just type 2.0 instead.