22 September 2017

Python Variable

Variable and Value

  • A variable is a memory location where a programmer can store a value. Example : roll_no, amount, name etc.
  • Value is either string, numeric etc. Example : "Sara", 120, 25.36
  • Variables are created when first assigned.
  • Variables must be assigned before being referenced.
  • The value stored in a variable can be accessed or updated later.
  • No declaration required
  • The type (string, int, float etc.) of the variable is determined by Python
  • The interpreter allocates memory on the basis of the data type of a variable.

Python Variable Name Rules

  • Must begin with a letter (a - z, A - B) or underscore (_)
  • Other characters can be letters, numbers or _
  • Case Sensitive
  • Can be any (reasonable) length
  • There are some reserved words which you cannot use as a variable name because Python uses them for other things.

Good Variable Name

  • Choose meaningful name instead of short name. roll_no is better than rn.
  • Maintain the length of a variable name. Roll_no_of_a-student is too long?
  • Be consistent; roll_no or or RollNo
  • Begin a variable name with an underscore(_) character for a special case.

Python Assignment Statements

The assignment statement creates new variables and gives them values. Basic assignment statement in Python is :
Syntax
<variable> = <expr>
Where the equal sign (=) is used to assign value (right side) to a variable name (left side). See the following statements :
  1. >>> Item_name = "Computer" #A String  
  2. >>> Item_qty = 10 #An Integer  
  3. >>> Item_value = 1000.23 #A floating point  
  4. >>> print(Item_name)  
  5. Computer  
  6. >>> print(Item_qty)  
  7. 10  
  8. >>> print(Item_value)  
  9. 1000.23  
  10. >>>   
One thing is important, assignment statement read right to left only.
Example :
a = 12 is correct, but 12 = a does not make sense to Python, which creates a syntax error. Check it in Python Shell.
  1. >>> a = 12  
  2. >>> 12 = a  
  3. SyntaxError: can't assign to literal  
  4. >>>   

Multiple Assignment

The basic assignment statement works for a single variable and a single expression. You can also assign a single value to more than one variables simultaneously.
Syntax
var1=var2=var3...varn= = <expr>
Example :
x = y = z = 1 
Now check the individual value in Python Shell.
  1. >>> x = y = z = 1  
  2. >>> print(x)  
  3. 1  
  4. >>> print(y)  
  5. 1  
  6. >>> print(z)  
  7. 1  
  8. >>>   
Here is an another assignment statement where the variables assign many values at the same time.
Syntax
<var>, <var>, ..., <var> = <expr>, <expr>, ..., <expr>
Example :
x, y, z = 1, 2, "abcd"
In the above example x, y and z simultaneously get the new values 1, 2 and "abcd".
  1. >>> x,y,z = 1,2,"abcd"  
  2. >>> print(x)  
  3. 1  
  4. >>> print(y)  
  5. 2  
  6. >>> print(z)  
  7. abcd   
You can reuse variable names by simply assigning a new value to them :
  1. >>> x = 100  
  2. >>> print(x)  
  3. 100  
  4. >>> x = "Python"  
  5. >>> print(x)  
  6. Python  
  7. >>>    

Swap variables

Python swap values in a single line and this applies to all objects in python.
Syntax
var1, var2 = var2, var1
Example :
  1. >>> x = 10  
  2. >>> y = 20  
  3. >>> print(x)  
  4. 10  
  5. >>> print(y)  
  6. 20  
  7. >>> x, y = y, x  
  8. >>> print(x)  
  9. 20  
  10. >>> print(y)  
  11. 10  
  12. >>>  

Local and global variables in Python

In Python, variables that are only referenced inside a function are implicitly global. If a variable is assigned a value anywhere within the function’s body, it’s assumed to be a local unless explicitly declared as global.
Example :
  1. var1 = "Python"  
  2. def func1():  
  3.     var1 = "PHP"  
  4.     print("In side func1() var1 = ",var1)  
  5.   
  6. def func2():  
  7.     print("In side func2() var1 = ",var1)  
  8. func1()  
  9. func2()  
Output :
In side func1() var1 =  PHP
In side func2() var1 =  Python
You can use a global variable in other functions by declaring it as global keyword :
Example :
  1. def func1():  
  2.     global var1  
  3.     var1 = "PHP"  
  4.     print("In side func1() var1 = ",var1)  
  5.   
  6. def func2():  
  7.     print("In side func2() var1 = ",var1)  
  8. func1()  
  9. func2()  
Output :
In side func1() var1 =  PHP
In side func2() var1 =  PHP

Start typing and press Enter to search