#5 Variables in Python

Variable naming rules:

  1. Variables names must start with a letter or an underscore, such as:
    1. _underscore
    2. underscore_
  2. The remainder of your variable name may consist of letters, numbers and underscores.
    1. password1
    2. n00b
    3.    un_der_scores
  3. Names are case sensitive.
    1.  case_sensitive, CASE_SENSITIVE, and Case_Sensitive are each a different variable.




How variables are declared in Python?
C/C++/Java                                                   Python
       int x = 10;                                                   x = 10
      float y = 4.2;                                                y = 4.2
      String z = “hello”;                                       z = “hello”

id(variable_name) :- Gives address of variable
type(variable_name) :- Gives type of variable

Case 1: Print the content, address and type of variable.
Case 2: Check 2 variables addresses with 2 different values.
Case 3: Check 3 variables addresses with same value.

All this 3 Cases are discussed in above video

Q If x=y=z=10 and we destroy ‘y’ then what happen?
Ans:

If y destroy still x and z will point to same memory location because Reference count for the object is now 2.
If Reference count becomes 0 then the object will get removed by garbage collector then that type of object is called as Unreferenced Object.                             


Previous
Next Post »