Learning Python- 3rd Day (Noon - 2)

 Just finished this tutorial: Click here


People often ask programmers about how complicated and powerful python is.

Well, I think I got the answer today. Python's one special feature, that I learnt today will blow your mind.

Lets say I am declaring a variable named "myVar" in python.

Input: myVar = 10

Okay I have the value 10 which is assigned as myVar.

But do you know? By default, python has its own ID or address name for each numbers?

Yes thats what I learnt today. 

Input: id(myVar)

Output: 2557133914640

There is a twist, if you put

Input: id(10)

Output: 2557133914640

You can only get the ID of a value which is already assigned. 

I have not assigned any values except 10, so any other value will not have an address.

For example:

Input: id(9)

Output: Error

But if I do this:

Input: myVar2 = 9

Input: id(9)

Output: 2557133914608


Another cool thing is, once a value is assigned to a variable, you can change the value of the variable anytime later but the id of 10 still stays.

Like this:

Input: myVar = 8

Input: id(myVar)

Output: 2557133914576

Input: id(10)

Output: 2557133914640

This is called garbage storing in python.

Note: Every time you start a python program from scratch, the address of any value will be different.

 More about it:

id

Description

Returns the “identity” of an object.

Syntax

id (object)

object
Required. An object which id is to be returned.

Return Value

#TODO

Time Complexity

#TODO

Note

CPython implementation detail: This is the address of the object in memory.

Remarks

This is an integer (or long integer) which is guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id() value.

Example

>>> l1 = [1, 2]
>>> l2 = [1, 2]
>>> id(l1)
46449768
>>> id(l2)
46210984
>>> id(l1) == id(l2)# note that objects with the same value can have different ids
False
>>> l1 == l2
True

Comments

Popular posts from this blog

Apple iPad Air (2022) Specifications and Review in 2023

17-year-old suspect in fatal stabbing at Taylor Swift-themed dance event ID’d

Exercise vs Diet