When to use quotes for numeric variables



I’m noticing that some variables use quotes on the right side of the equation and others don’t. When do you want to your quotes? I understand that you should use quotes for a string of characters. However, when do you want to (or NOT want to) use it for numbers?


If you want to do math with the numbers No quotes
If you want the numbers to represent text, quotes


@vfaldasz the reason they use a number inside of a string in that exercise is because they are using it as a piece of data to describe a characteristic of his age. They aren’t intending to use it for any numerical operations, its just a description that can represent an idea of the variable.


There are cases where they will be stored as strings (input data, transmitted data (eg. CSV, XML, JSON, etc.) and assembly of print data going out) but for quick and easy access to that data through our program, storing numbers preserves their type and puts less overhead on our program. (not proven)

We will be casting strings to numbers and numbers to strings on many occasions. Our job as learners is to make this task one we understand and know how and when to use.

Bottom line, it really doesn’t matter how we store numbers, it’s how we parse them out of the data that counts.

a = raw_input("Enter a number: ")

a is the name given to the string value stored in memory when the user presses Enter.

We can permanently store it as is, for convenience and, something to be looked into, space.

Understand How Much Memory Your Python Objects Use

@ionatan has been trying to get this point across to me for months, and even after reading this am still struggling, though much less. It will take a little ironing out, but in the end you will know when best to leave a number as a number, or a string. Hopefully, eventually, I will be able to say the same.


@mtf True, but regarding the exercise he linked, I assume he is just referring to the basic fundamentals of a variable with an integer inside of a string compared to a variable that assigns an integer used for basic mathematical operations.


Yeah, I’ve been getting a lot of flack on that, lately. Just no longer know where I am, sometimes.

Intuition is what drives this bus.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.