Lecture 2.1: Variables — Programming

Syed Faizan
PythonForKids
Published in
5 min readDec 20, 2018

Before, going on with this, you must have tshe concept of variables cleared in Part 1. Link to Part 1: Click Here!

Another good example is: The Prime Minister of Pakistan is currently Imran Khan. But a few years ago, it was Nawaz Sharif. Later, it could be someone else entirely!

Remember this, from Part 1. Let’s replicate it in programming:

PrimeMinister_Of_Pakistan = "Imran Khan"PrimeMinister_Of_Pakistan -> Identity
"Imran Khan" -> Value

I hope you could see it clearly now, A variable stores a value and the variable is given an identity to be referenced later in a Programme.

VARIABLE DATA TYPES:

Now as we understand, the computer is a dump machine and needs to be told everything by programmers.

A -> Alphabet
FAIZAN -> Collection of Alphabets
120 -> A Number
120.5 -> Number with Decimal Values
-> It's Null/None/Void as Left side has nothing!

As there are a different kind of values, we need to tell the computer individually what the type of value will be. The above are interpreted in programming as follows:

A -> Character (Alphabet)
FAIZAN -> String (Collection of Alphabets)
120 -> Integer(Number without Decimal Values)
120.5 -> Float(Number with Decimal Values)
-> None or Null(It's Null/None/Void as Left side has nothing!)

WHY VARIABLES ARE USEFUL:

Every new prime minister must take an oath(Halaf in Urdu) on a public platform. Imran Khan took the oath, Nawaz Sharif took the same oath before. The oath for Nawaz Sharif was:

I,Nawaz Sharif, do solemnly swear that I am a Muslim and believe in the Unity and Oneness of Almighty Allah, the Books of Allah, the Holy Quran being the last of them, the Prophethood of Muhammad (peace be upon him) as the last of the Prophets and that there can be no Prophet after him, the Day of Judgement, and all the requirements and teachings of the Holy Quran and Sunnah. ....That I,Nawaz Sharif, will strive to preserve the Islamic Ideology which is the basis for the creation of Pakistan:That I,Nawaz Sharif, will not allow my personal interest to influence my official conduct or my official decisions:That I,Nawaz Sharif, will preserve, protect and defend the Constitution of the Islamic Republic of Pakistan:That, in all circumstances, I,Nawaz Sharif, will do right to all manner of people, according to law, without fear or favour, affection or ill-will:

Now as Imran Khan overtook the Office of Prime Minister, he must swear the oath too. For which in the above oath, Nawaz Sharif needs to be replaced by Imran Khan. How many time does the change occur in this small part of the oath? 5 Times.

I,Imran Khan, do solemnly swear that I am a Muslim and believe in the Unity and Oneness of Almighty Allah, the Books of Allah, the Holy Quran being the last of them, the Prophethood of Muhammad (peace be upon him) as the last of the Prophets and that there can be no Prophet after him, the Day of Judgement, and all the requirements and teachings of the Holy Quran and Sunnah. ....That I,Imran Khan, will strive to preserve the Islamic Ideology which is the basis for the creation of Pakistan:That I,Imran Khan, will not allow my personal interest to influence my official conduct or my official decisions:That I,Imran Khan, will preserve, protect and defend the Constitution of the Islamic Republic of Pakistan:That, in all circumstances, I,Imran Khan, will do right to all manner of people, according to law, without fear or favour, affection or ill-will:

Let’s see it with Programming variable approach, we declare a variable with name PrimeMinister_Of_Pakistan and set it to the current Prime Minister’s Name.

PrimeMinister_Of_Pakistan = "Nawaz Sharif"
I,PrimeMinister_Of_Pakistan, do solemnly swear that I am a Muslim and believe in the Unity and Oneness of Almighty Allah, the Books of Allah, the Holy Quran being the last of them, the Prophethood of Muhammad (peace be upon him) as the last of the Prophets and that there can be no Prophet after him, the Day of Judgement, and all the requirements and teachings of the Holy Quran and Sunnah. ....
That I,PrimeMinister_Of_Pakistan, will strive to preserve the Islamic Ideology which is the basis for the creation of Pakistan:That I,PrimeMinister_Of_Pakistan, will not allow my personal interest to influence my official conduct or my official decisions:That I,PrimeMinister_Of_Pakistan, will preserve, protect and defend the Constitution of the Islamic Republic of Pakistan:That, in all circumstances, I,PrimeMinister_Of_Pakistan, will do right to all manner of people, according to law, without fear or favour, affection or ill-will:

Now Anywhere the Variable PrimeMinister_Of_Pakistan appears, Computer would replace it with the value of the Variable which in the above case is “Nawaz Sharif”. Now for Changing it to Imran Khan to get the oath ready for him, We just need to change the value for Variable PrimeMinister_Of_Pakistan:

PrimeMinister_Of_Pakistan = "Imran Khan"
I,PrimeMinister_Of_Pakistan, do solemnly swear that I am a Muslim and believe in the Unity and Oneness of Almighty Allah, the Books of Allah, the Holy Quran being the last of them, the Prophethood of Muhammad (peace be upon him) as the last of the Prophets and that there can be no Prophet after him, the Day of Judgement, and all the requirements and teachings of the Holy Quran and Sunnah. ....
That I,PrimeMinister_Of_Pakistan, will strive to preserve the Islamic Ideology which is the basis for the creation of Pakistan:That I,PrimeMinister_Of_Pakistan, will not allow my personal interest to influence my official conduct or my official decisions:That I,PrimeMinister_Of_Pakistan, will preserve, protect and defend the Constitution of the Islamic Republic of Pakistan:That, in all circumstances, I,PrimeMinister_Of_Pakistan, will do right to all manner of people, according to law, without fear or favour, affection or ill-will:

Now Anywhere the Variable PrimeMinister_Of_Pakistan appears, Computer would replace it with the value of the Variable which in the above case is “Imran Khan”.

VARIABLES IN PYTHON:

Consider it as a Left to Right Equation in Programming for Variables with Identity at Left and Value at Right.

  1. Provide an Identity
  2. Give it a value
Pencil_Holder = 1
MyName = "Faizan"
FavouriteNumber = 5
UnivesityName = 'Comsats'
Percentage = 60.5
print(Pencil_Holder)
print(MyName)
print(FavouriteNumber)
print(UnivesityName)
print(Percentage)

Replace the Values Above with your choice and Execute the Code, Then Put the output in the Comments below!

--

--