Variable Assignment

In the previous section we discussed about working with numbers in Python but what do these numbers represent?? A variable is a user-defined name that reserves some space in memory in order to store the values assigned to it. This means that whenever you create a variable , you reserve some space in the memory. It is always nice to assign these values a variable name to easily reference them later on in our code.

By community conventions , it is always suggested to use variable name which is somehow related to the value stored in it.

A variable is assigned a value in the following way:
Variablename = valueassigned , here '=' sign represents the assignment operator.
Rules for naming a variable
  • Names can not start with a number.
  • There can be no spaces in the name , use _ instead.
  • Can't use any of the special symbols : "',<>\()@#%$*-~
  • Avoid using built-in keywords like "list" and "str".
Bored of all the theory. Let us run a few codes where we create variables and assign values to it.
w = 5
print(w)
output:
 5 
Alright let us perform few basic mathematical manipulation using variables. What do you think will be the output of the following code?
a = 10
a = a + a
a = a * a
print(a) 
output:
 400 
explanation:
  • 1. variable a is assigned value of 10
  • 2. variable a is reassigned a value of (a+a) = 20
  • 3. variable a is reassigned a value of (a*a) = 400
  • 4. 400 is printed on the output screen
Python is a "Dynamically-Typed" language

A language can be dynamically-typed , statically-typed or hybrid(static and dynamic). The basic difference between them is in terms of the flexibility it provides in reassigning a variable. In a dynamic language , a variable can be reassigned values of different data type at different sections of the code while the same can't be done in a statically-typed language.

STATIC or HYBRID(c++,c...)

int name = 3
name = "john"
DYNAMIC LANGUAGE(Python)

name = 3
name = "john"




Popular Tutorials

Creative Commons Licence
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.