VARIABLES IN PYTHON

 

VARIABLES

DECLARING A VARIABLE

A variable holds a value that may change. The process of writing the variable name is called Declaring the variable. In Python, variables do not need to be declared explicitly in order to reserve memory spaces as in other programming languages like C, java etc. When we initialize the variable in Python, Python interpreter automatically does the declaration process.

 

INITIALIZING A VARIABLE

The general format of assignment statement is as follows:

The equal sign (=) is known as Assignment operator. An expression is any value, text or arithmetic expression, whereas variable is the name of the variable. The value of the expression will be stored in the variable.

Examples of variable initialization:

The above statements reserve two memory spaces with variable names year and name. 2020 and John are stored respectively in these memory spaces as shown below:

Whenever we want to display the value of the variable, simply type these variable name on console.

Example:

We can also assign one variable value into another variable.

For example, to assign the value of name1 variable into name2 variable:

Whenever two values are successively assigned to a variable, the interpreter will forget the previous value assigned to it and store the latest value in the variable memory space.



In the given example, we first assigned 2019 to the variable year and then assigned 2020 to the same variable. The interpreter will forget the value 2019 and will display 2020 as the value of the year.

We can also assign different types of values to the same variable. For example, we can assign a text value where there previously was a numeric value. Even in such a case only the last assigned value remains as the value of that variable.

For example:



 

Comments

Popular posts from this blog

Computer Science Lecture Notes for Degree Students

DATA TYPES IN PYTHON

COMMENTS, IDENTIFIERS AND KEYWORDS IN PYTHON