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
17,948 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!