| | |

Variables and Assignments:



Variables:

Variables have always been considered as a temporary storage location for some data. Most of the languages, allocate storage for the variables on the main memory. Though python also allocates the same way but here, variables are not storages. Instead, a variable is considered to be a reference to the value stored somewhere in the memory.

Consider the syntax below:

student = ’Vivek’ roll = 12 percentage = 87.46

Here, ‘Vivek’ is a string stored somewhere in the memory and is being referred to the variable student. Similarly, 12 and 87.46 are two numeric values being referred to variables- roll and percentage respectively.




Python provides the following two specific features related to assignment:

Dynamic Typing:

It is the way of assigning values to a variable in which there is no any datatype attached with the variable. We can assign whatever kind of values to the variable.

Consider the syntax below:

roll = 12 print(roll) roll = ‘Twelve’ print(roll)

After the code is executed, we’ll get the output:

12 Twelve

This proves that datatype doesn’t restrict values being assigned to the variable and that is what dynamic typing is all about.

NOTE: In Static Typing, a certain datatype is attached with the variable and it restricts values of other datatypes to be assigned to that variable.




Multiple Assignments:

Python allows some wonderful assignments. Consider the following:

roll1 = roll2 = 12

here, both the variables, roll1 and roll2 are referring to the same value but changing value of one variable will not have any effect on the other.


roll1, roll2 = 12, 13

here roll1 is referring to 12 and roll2 is referring to 13 and both assignments are being done at the same time.


roll1, roll2 = roll2, roll1

here values of roll1 and roll2 are swapped together. To understand this weird kind of assignment, we need to recall the fact that python variables are not storage locations but references to the values.





Who are we?

Where are we found?

Why do we write?

For whom do we write?

Wanna write to us?










Linux Commands

Computer Networks

Python Programming

JavaScript  ReactJS

HTML  CSS  W3.CSS

Copyright 2018. All Rights Reserved.