Emojis are too heavy !!
Do you like emojis..yes you do and the same goes for everyone !
Every day we sent almost 10 billion emojis while chatting !
Come, lets peep inside python’s brain and find how it deals with emojis and other basic data!
World’s most popular emoji is 😂 and lets find its size
We may need some weapons like sys.getsizeof() to make this journey much easier
help(sys.getsizeof)OUTPUTHelp on built-in function getsizeof in module sys:getsizeof(…) getsizeof(object, default) -> int Return the size of object in bytes.
Now as our weapons are ready, lets find the size of 😂
import sys sys.getsizeof(😂)OUTPUT80
One emoji is of 80 bytes !!!
So Just imagine how much memory we spend every day!
Dont worry, We have a solution to decrease the memory usage and increase the benefit 😃
import sysa = '😂'
b = '😂🤔'
c = '😂🤔😶'print(sys.getsizeof(a),sys.getsizeof(b),sys.getsizeof(c))OUTPUT80 84 88
Here one emoji takes 80 bytes and with every new emoji the size gets added up by 4 bytes!
One emoji needs 80 Bytes and 3 emojis need 88 Bytes !
🚶Come, Lets find how our python brain feels about the basic data types !! 🚶
A. Integer data type
import sysa=int()print(a, ’needs’ ,sys.getsizeof(a), ’bytes’)OUTPUT0 needs 24 bytes
Here we see python integer’s Default value is 0 and its Base Size is 24 bytes
As we saw in emoji here also we can save memory by increasing the number of digits
import sysa=1
b=1234
c=123456789# 10 digits
d=1234567890 print(a,'needs' ,sys.getsizeof(a),'bytes')
print(b,'needs' ,sys.getsizeof(b),'bytes')
print(c,'needs' ,sys.getsizeof(c),'bytes')
print(d,'needs' ,sys.getsizeof(d),'bytes')OUTPUT1 needs 28 bytes
1234 needs 28 bytes
123456789 needs 28 bytes
1234567890 needs 32 bytes
In Integer with every 10th digit 4 bytes are added to the base size.
B. Float data type
import sysa = float()
b = int()print(a, ’needs’ ,sys.getsizeof(a), ’bytes’)
print(b, ’needs’ ,sys.getsizeof(b), ’bytes’)OUTPUT0.0 needs 24 bytes
0 needs 24 bytes
Here we see python float’s Default value is 0.0 and its Size is 24 bytes.
And 0.0 is equal to 0 thats why we have both the int and float default value sizes same.
import sys# Whole number: 18 digits, Fractional number : 4 digits
a = 123456789123456789.1234
print(a, ’needs’ ,sys.getsizeof(a), ’bytes’)# Whole number: 16 digits, Fractional number : 3 digits
b = 1234567891234567.234
print(b, ’needs’ ,sys.getsizeof(b), ’bytes’)
c = float(12345678912345678)
print(c, ’needs’ ,sys.getsizeof(c), ’bytes’)OUTPUT1.2345678912345678e+17 needs 24 bytes
1234567891234567.2 needs 24 bytes
1.2345678912345678e+16 needs 24 bytes
5 points about Float value memory usage
- Maximum number digits printed including Whole and Fractional part will be 17
- Maximum number of digits printed individually for Whole or Fractional part will be 16
- First 17 digits starting from whole number part are chosen to be printed.
- There are some approximation rules applied which mostly affects the 16th digit in the fractional part.
- Float has 24 bytes and it dont change
C. Complex data type
import sys a = complex()
print(a,sys.getsizeof(a))a = 123456789123456789+0j
print(a,sys.getsizeof(a))a = 1+123456789123456789j
print(a,sys.getsizeof(a))OUTPUT0j needs 32 bytes
(1.2345678912345678e+17+0j) needs 32 bytes (1+1.2345678912345678e+17j) needs 32 bytes
2 points about Complex value memory usage
- Complex default value is 0+0j and its permanent size is 32 bytes
- Complex number will follow the same 17 digit float value rule if there are enough digits.
🤔In normal maths we used to denote complex numbers like 0 + 7i but here we are using j! Why ? (Find it!)
D. String data type
import sysa=str()
print(a, ‘needs’ ,sys.getsizeof(a), ‘bytes’)a=’1234'
print(a, ‘needs’ ,sys.getsizeof(a), ‘bytes’)a=”aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa”+”aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa”
print(a, ‘needs’ ,sys.getsizeof(a), ‘bytes’)OUTPUTneeds 53 bytes
1234 needs 53 bytes aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa needs 137 bytes
3 points about String value memory usage
- String default value is Empty String and its size is 53 bytes
- Size of any one String object ‘a’ is 50 bytes. Its 3 bytes less than the Empty String space!
- With every new character 1 byte is added and in this way Empty String space equals to memory size of 4 character strings !!
Thats all !
Hope you liked the journey inside Python Brain !!😃
Happy Learning !