Variables
Published: Sep 7, 2022
undefined
In this posts we will talking about variables.
To understand what is variable, imagine this example.
You are in a messy room containing various types of things, e.g. books, games, shirts, etc. Here your goal is to clean up room. How will you arrange things in good manner?
One way is that throw everything in some storage box or cupboard. Your initial goal is complete… but what will you do if you need some things from it?. To take things from cupboard or anything you need to take out most of the unnecessary things, which is not good, right?.
To avoid that problem, you can store same type of things in one or two storage boxes/in same place in cupboard. This will solve earlier problem.
And to simply it more you give that varible a lable, saying “hey you will store this specific type of data with you”. In programming that lable is called data type.
Fortunately in Python you don’t have to care about this thing. Like below example.
# declaring varible with value 10
varible_with_value = 10
Well to declare a varible you first write name of varible followed by ” = ” and then value, and done!!
Remember I said “in Python you don’t have to care about which type of data one variable can store”, well this give one drawback of changing value of variable with/without you knowning.
number = 10 # variable with integer value
number_become_string = "10" # now it convert to string
Casting
You can change value of variable by doing this also,
number = 10 # integer
string = str(number) # string will become "10"
floating = float(number) # floating will become 10.0
a = 10
A = 20
print(a) # 10
print(A) # 20