Variables

Table of Contents

VARIABLES

Variable is the name given to a reserved memory locations to store values. It is also known as Identifier in python.

Need for variable:

Sometimes certain parameters will take different values at different time. Hence, in order to know the current value of such parameter we need to have a temporary memory which is identified by a name that name is called as variable. For example, our surrounding temperature changesfrequently.In order to know the temperature at a particular time, we need to have a variable.

Naming and Initialization of a variable

  1. A variable name is made up of alphabets (Both upper and lower cases) and digits and is case sensitive.
  2. No reserved words.
  3. Initialize before calling.
  4. Multiple variables initialized.
  5. Dynamic variable initialization.

Consist of upper and lower case alphabets, Numbers (0-9).E.g. X2

  • In the above example, a memory space is assigned to variable X2. The value of X2 is stored in this space.
  • Reserved words should not be used as variables names.

In the above example “and” is a reserved word, which leads to Syntax error

  • Variables must be initialized before it called , else it reports “is not defined ” error message as below E.g.: a=5 print(a)

In the above example “a” is called before it initialized. Hence, the python interpreter generates the error message: NameError: ‘a’ is not defined.

  • Multiple variables can be initialized with a common value. E.g.: x=y=z=25

In the above three variables x, y, z is assigned with same value 25.

  • Python also supports dynamic variable initialization. E.g.: x,y,z=1,2,3

Proper spacing should be given

  • print(10+20+30) -> bad style
  • print(20 + 30 + 10) -> good style

Add Your Heading Text Here

You May Like to Browers More