Type Conversion – A Python Programming Question

type-conversion-python-questAndAns01If your not savvy in computer programming, or specifically, savvy in Python, you probably would not be able to figure out the answer to this question. But the answer is simply:

“210” is a String. * means multiply, in which its multiplying in this case to an integer. Int is short for integer. Input means to take from a user’s entered value. The question the user is asked is, “Enter a number:”. In this case, the user enters 2. Since the user’s input is 2, and the math is a String * an integer; in other words, “210” * 2. The answer is 420. But not… “210” is a String, like words, not an actual number in this case. No its not 210210 either. Actually, what about the float? Well, if look at this as a math problem, you do all inner parenthesis first. The >>> doesn’t mean anything here other than awaiting a user’s command. The float means, simply, allow numbers from being solely integers to being values of decimals. So, its not just 210210, its actually:

float(210210)

Therefore, the actual correct answer is:

210210.0

Crazy right?

After going through a series of other questions while learning Python, I decided to start writing up the questions in a python script. This way I can test the code to see what it does before I give my answer. Yea, its sorta cheating, but not if your understanding why the code does what it does.

[The question about was a screenshot from SoloLearn.]

One thought on “Type Conversion – A Python Programming Question

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s