When to use quotes for numeric variables


#1

https://www.codecademy.com/courses/learn-python/lessons/strings--console-output/exercises/strings?action=resume_content_item

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?


#2

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


#3

@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.


#4

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.


#5

@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.


#6

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.


#7

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