Python 'getsizeof' Command the Real Purpose
In Python, there are many objects. Out of those List (arrays) is one type. To get the number of elements present in a List is not easy. So how to get this? To do you need the 'getsizeof' command.
i = int(100)
print(sys.getsizeof(i))
Import 'sys' Library.
You need to import the 'sys' library to use the SIZE commands.
>>> import sys
Congrats to you since you have now imported the SYS library. The command name is "getsizeof".
Check the below example.
print("An Integer:")i = int(100)
print(sys.getsizeof(i))
The result will be: 100
What is the size command? - is to get the size of elements. It gives the result based on the object you are being used. Suppose, if you use Strings, you will get String length.
How to use the 'getsizeof' command?
Example:1print("Empty string:")
empty_string = ""
print(sys.getsizeof(empty_string))
s = "Hello world"
print(sys.getsizeof(s))
for i in range(0,5):
empty_string = empty_string + chr(i)
print(sys.getsizeof(empty_string))
empty_string = ""
print(sys.getsizeof(empty_string))
Example:2
print("A normal string")s = "Hello world"
print(sys.getsizeof(s))
Example:3
print("Increasing string:")for i in range(0,5):
empty_string = empty_string + chr(i)
print(sys.getsizeof(empty_string))
Sources
Comments
Post a Comment
Thanks for your message. We will get back you.