Variables In Python

A variable is a memory location where a programmer can store a value. The value stored in a variable can be accessed or updated later. The interpreter allocates memory on the basis of data type of a variable.

Python Variable Naming Convention


  • 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.

Giving a 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 RollNo
  • Begin a variable name with underscore(_) character for special case.

Python Assignment Statements

  1. The assignment statement creates new variables and gives them values.
  2. Basic assignment statement in Python is : <variable> = <expr>
    Where the equal sign (=) is used to assign value (right side) to a variable name (left side). See the following statements.


3. Run the code on clicking Run.


4. One thing is important, assignment statement read right to left only.
a = 12 is correct, but 12 = a does not make sense to Python, which creates a syntax error. Check it in Python Shell.


Multiple Assignment In Python

1. 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. See the following statement.
var1=var2=var3…varn= = <expr>
Example :
x = y = z = 1
2. Here is an another assignment statement where the variables assign many values at the same time.
<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”.
Now check the individual value in Python Shell.



Leave A Reply

Your email address will not be published. Required fields are marked *