03 Chapter Three - Variables
03 Chapter Three - Variables
CHAPTER THREE
Variables
Objectives
What is a variable?
What are the properties of a variable?
How can we create and use variables in Python?
What is keyword?
What operations can be performed on variables?
To summarize, a variable is a
Name
Refers to a value
Hold some data
Name of a memory location
💡 Let us consider librarians, how could they storeand access ton of books? They just make catalogue of
books based on their genre with their reference location. That is how variables also works.
Properties of a Variable
1. Type:
Each data have a type whether it is number word or something and each variable should belong to one of the
data types. This is called the data type of the variable.
2. Scope:
In any programming language, scope refers to the place or limit or boundaries or the part of the program
within which the variable is accessible.
3. Value:
Each and every variable in a program holds value of some type which can be accessed or modified during the
due flow of the program.
4. Location:
As already stated, the data is stored in memory location. The variable is the name which we give to that
location. Each memory location has some address associated with it.
5. Lifetime:
The lifetime of a variable refers to the time period for which the memory location associated with the variable
stores the data.
name_of_the_variable = value
1 age = 21
How can I look for my variables?
To see your variable use print() function
print(name_of_the_variable)
1 print(age)
21
PS. Don’t worry we will discuss more about function later in Function Chapter.
1 print("I am John")
I am John
My age is 21
EXERCISE 3.1
Create a variable "date" and store today's date then print it?
Today's date is 6
💡 What if we want to submit value of a variable outside our code like questionnaire?
Note: We can print meaningful text messages along with variables for better understanding.
EXERCISE 3.2
Create a variable "bd" and take input birth date from user then print it?
Your birthdate is 6
.
1 17 = age
1 _age = 23
2 print(_age)
23
1 1dollar = 76
1 $dollar = 74
1 My Age = 24
PS. Case sensitive means you can’t use lowercase letters in place of uppercase and vice versa.
1 age = 21
2 print(Age)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[13], line 2
1 age = 21
----> 2 print(Age)
6. cannot be a keyword.
.
keyboard_arrow_down Lesson Two : Keywords
What is keyword?
Keywords are special reserved words, that have specific meanings and purposes.
All 33 keywords in python contain only alphabet symbols. All of them are in lower case except True, False,
and None.
1 import keyword
2 keyword.kwlist
['False',
'None',
'True',
'and',
'as',
'assert',
'async',
'await',
'break',
'class',
'continue',
'def',
'del',
'elif',
'else',
'except',
'finally',
'for',
'from',
'global',
'if',
'import',
'in',
'is',
'lambda',
'nonlocal',
'not',
'or',
'pass',
'raise',
'return',
'try',
'while',
'with',
'yield']
PS. You cannot use these keywords as a variable, else will result in error.
Ex. 3.12 ( Creating a variable with keyword name )
1 class = 4
24 9 4327
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[17], line 1
----> 1 Sid, age, grade = 4327, 24
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[18], line 1
----> 1 Sid, age = 4327, 24, 9
10 10 10
you can get Memory location of a variable using the syntax below:
id(variable_name)
PS. For sake of simplicity, from now onward MA means Memory Address of a variable.
1 dollar = 69
2 print("1$ = ",dollar)
3 print("MA of dollar:",id(dollar))
1$ = 69
MA of dollar: 140719801867176
2. Reassigning
you can update / reassign value of a variable using the syntax below:
variable_name = new_value
1 dollar = 69
2 print("In 2019, $1 =",dollar)
3 print("MA of dollar in 2019:",id(dollar))
4 dollar = 72
5 print("In 2020, $1 = ",dollar)
6 print("MA of dollar in 2020:",id(dollar))
In 2019, $1 = 69
MA of dollar in 2019: 140719801867176
In 2020, $1 = 72
MA of dollar in 2020: 140719801867272
3. Deleting
del variable_name
1 book = 5
2 del book
3 print(book)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[22], line 3
1 book = 5
2 del book
----> 3 print(book)
Create a variable "ap" and accept "apple price" from user, then print user’s apple price
“ap” and its memory location, then next reassign apple price to 34, again print apple
price and its memory location, finally delete variable "ap"?
Key Takeaways
Variable is the name which we give to the memory location which holds some data.
Type, Scope, Value, Location and Lifetime are properties of a variable.
print() is a function that can print the specified message to your computer screen.
input() is a function that can accept values from a user (you).
Keywords are special reserved words, that have specific meanings and purposes.
Checking memory location, reassigning and deleting are operations you can perform on variables.