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 Numeric

Zhendong Chen
Zhendong Chen
401 Points

what is the difference between float(numbers) and float("numbers") and the difference between int(numbers) and int(" ")?

what is the difference between float(numbers) and float("numbers") and the difference between int(numbers) and int("numbers")?

1 Answer

Louise St. Germain
Louise St. Germain
19,424 Points

Hello Zhendong,

In Python, anything in quotation marks ("") is a string literal. This means it will evaluate what is inside the quotation marks exactly as it is written. If there are no quotation marks, then it means the variable. Python will evaluate using the contents of the variable, not the name.

As a simple example, to illustrate the difference:

# Assign the text "Good morning" to a variable named "hello"
hello = "Good morning"
# Notice the variable name has no quotation marks.

print("hello") 
# Result: >>> hello
# because I told it to literally print the word "hello"

print(hello) 
# Result: >>> Good morning
# because now it is using the variable named hello, and prints
# the contents of that variable.

In a similar way, for your float(numbers) example:

numbers = "12"
float(numbers)
# Result >>> 12.0
int(numbers)
# Result >>> 12

float("numbers")
# Result: ValueError, because it it tries to turn the word "numbers" 
# into an actual number, and this is not possible because it is a word.

I hope this helps to clarify!