4

I'm trying to understand the below snippet.

import sys

variable = 30
print(sys.getsizeof(variable)) #prints 28

If I change integer value with string

import sys

variable = "30"
print(sys.getsizeof(variable)) #prints 51

So does it means integer object makes use of less memory than string object.

Please let me know how Python makes use of memory.

3
  • Yes, that's exactly what it means. Commented Sep 21, 2019 at 15:36
  • 3
    "does it means integer object makes use of less memory than string object" - 28 bytes of memory is less than 51 bytes of memory, so... Although, when talking about more complex objects, be careful with this kind of reasoning because sys.getsizeof [doesn't take into account the memory attributed to objects the given object refers to]. Commented Sep 21, 2019 at 15:37
  • Article on Python memory usage for variables: code.tutsplus.com/tutorials/… Commented Sep 21, 2019 at 16:34

1 Answer 1

5

Yes integer object use 28 byte of memory while string object is getting bigger size of memory.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.