So when I was teaching at my class, I was making a simple programme and there in the code there was a step, where we hade to make the programme sleep for 5 minutes. so if you don't know how to make the programmed sleep, on in simple words, stop for some time here is the code.

import time
print("Before sleep")
time_to_sleep =60*5
time.sleep(time_to_sleep )
print("After Sleep")

Here we import the time module and run the sleep method, with a parameter, which is the number of seconds to sleep.

So the code looked like this, but as the kids were smart a question was raised, why can't we write 300 instead of 60*5. as when we call the sleep method, it will have to calculate the value of 60*5, and if we have written 300, the programme won't have to calculate so it will make the programme faster. The question was smart but fortunately, python doesn't work this way.

What Python does is, it will calculate all the values and store it in the variable, so when we store 60*5 it will automatically store 300. So we won't be wasting any time to calculate the number. But will it take more space? well before proceeding any further, we should spend some time with writing code. So go ahead and make a function something like this. Done copy and paste,  Write it and Play with it.

def test_function():
    test_num = 50*50*50
    TestTupple = (8, 6) *7
    TestString = ("test") * 2
    TestLargeString = ("This Is a large sentence, and we have to make it  larger. so just write anything random") * 100

and run this line of code after defining the function.


When you run it the output will look like this. 

(None, 125000, (8, 6, 8, 6, 8, 6, 8, 6, 8, 6, 8, 6, 8, 6), 'testtest', 'This Is large, and we have to make it larger. so just write anything random', 100)

So let's ignore the None, So the test_num is stored as 125000, and the tuple of number has also been calculated and so with the tuple of string.  But the TestLargeString don't look like something we expected? So the answer here is python is really smart, Python is really good at balancing between the storage and calculation, as the sentence will be too big in terms of memory to python stored it in another format, which will take less time to calculate when we access the variable.

How Python Do It?

Its always better to learn something extra, so how does python do all this. Writing it down in short, python does it by converting the mutable data types in immutable data types. Let's learn it by example.

if you define a list  list1 like this

list1 = ['h','e','l','l','o']

then this will be converted to  

list1 = ('h','e','l','l','o')


Let's write some code again, define a function like this

def test_function(element):
    if element in ("h", "e", "l", "l", "o"):

Now run this


And now you will see a result like this 

(None, (h', 'e', 'l', 'l', 'o'))

So basically python made things easier and faster most of the time by itself, so the first focus should be writing a code that a human can understand without any issue. 

Continue Reading