04VARIABLES

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

VARIABLES 10

VARIABLES

LESSON - ONE In any programming language, scope refers to the


place or limit or boundaries or the part of the program
INTRODUCTION TO VARIABLES within which the variable is accessible.

What is Variable? 3. Value:

Variable is the name which we give to the memory What do you store in the variable?
location which holds some data. With a variable we
can store the data, access the data and also Each and every variable in a program holds value of
manipulate the data. some type which can be accessed or modified during
the due flow of the program.
PS. Variable is a container where you can store a
value. 4. Location:

To summarize, a variable is a Where you store the variable?


 Name As already stated, the data is stored in memory
 Refers to a value location. The variable is the name which we give to
 Hold some data that location. Each memory location has some
 Name of a memory location address associated with it.
5. Lifetime:
Let us consider librarians, how could they store
and access ton of books? They just make catalogue Till when the variable will be available?
of books based on their genre with their reference
location. That is how variables also works. The lifetime of a variable refers to the time period for
which the memory location associated with the
Properties of a Variable variable stores the data.
1. Type: How to create a variable in Python?
What type of data you store in the variable? In python, to create a variable we just need to specify
Each data have a type whether it is number word or  Name of the variable
something and each variable should belong to one of  Assign value to the variable
the data types. This is called the data type of the
Syntax to create variable:
variable.
name_of_the_variable = value
2. Scope:
Ex. 4.1 ( Create a variable )
Who can access these data?
>>> age = 21
VARIABLES 11

How can I look for my variables? What if we want to submit value of a variable
To see your variable use print() function outside our code like questionnaire?

Syntax to print a variable: In such scenario we use input() function

print(name_of_the_variable) What is input function?

Ex. 4.2 ( Print a variable ) input() is a function that can accept values from a
user (you).
>>> print(age)
Syntax to submit value of a variable:
21
v = input(“Ask user to enter a value”)
What is print() function?
Ex. 4.5 ( Take input from a user )
print() is a function that can print the specified
age = input(“Enter your age? ”)
message to your computer screen.
print(“Your age is ”,age)
Syntax to print a message / text:
print(“write your message here”) Enter your age?

PS. Don’t worry we will discuss more about 19


function later in Function Chapter. Your age is 19
Ex. 4.3 ( Print a message / text ) Note: We can print meaningful text messages along
with variables for better understanding.
>>> print(“I am John”)
 Text message we should keep in within
I am John
double quotes.
 Text message and variable name should be
What if we want to print some message along with
our variable? separated by comma symbol.

PS. For sake of simplicity, from now onward v EXERCISE 4.2


means variable name. Create a variable "bd" and take input birth date
Syntax to print message with a variable: from user then print it?
Hint: Output will be like below
print(“write your message here ”,v)
Tell me your birthdate?
Ex. 4.4 ( Print text along with a variable) 6
>>> print(“My age is ”,age) Your birthdate is 6

My age is 21
Invalid cases for variables
EXERCISE 4.1
While defining variables in python,
Create a variable "date" and store today's date then
print it?  Variable names should be on the left side.
 Value should be on the right side.
Hint: Output will be like below  Violating this will result in syntax error.
Today's date is 6 Ex. 4.6 ( Creating a variable in wrong direction )

>>> 17 = age

SyntaxError: can’t assign to literal


VARIABLES 12

Rules for creating a variable All 33 keywords in python contain only alphabet
symbols. All of them are in lower case except True,
1. must start with letter or underscore False, and None.
Ex. 4.7 ( Creating a variable with underscore ) To see all the keywords –
>>> My_age = 23 >>> import keyword
SyntaxError: invalid syntax >>> keyword.kwlist
2. cannot start with number [‘False’, ‘None’, ‘True’, ‘and’, ‘as’,
Ex. 4.8 ( Creating a variable starts with number ) ‘assert’, ‘break’, ‘class’,
‘continue’, ‘def’, ‘del’, ‘elif’,
>>> 1dollar = 76 ‘else’, ‘except’, ‘finally’, ‘for’,
‘from’, ‘global’, ‘if’, ‘import’,
SyntaxError: invalid syntax
‘in’, ‘is’, ‘lambda’, ‘nonlocal’,
3. cannot use any special symbol ‘not’, ‘or’, ‘pass’, ‘raise’,
‘return’, ‘try’, ‘while’, ‘with’,
Ex. 4.9 ( Creating a variable with special symbol )
‘yield’]
>>> $dollar = 74
PS. You cannot use these keywords as a variable,
SyntaxError: invalid syntax else will result in error.

4. can’t use space ( instead use underscore ) Ex. 4.12 ( Creating a variable with keyword name )

Ex. 4.10 ( Creating a variable using space ) >>> class = 4

>>> My Age = 24 SyntaxError: invalid syntax

SyntaxError: invalid syntax Multiple Variables in a Single Line

5. are case sensitive We can assign multiple variables to multiple values


in a single one line. During assignment the variable
PS. Case sensitive means you can’t use lowercase name on the left hand side should be equal with the
letters in place of uppercase and vice versa. values on the right hand side. If not it results in error.
Ex. 4.11 ( Print a variable using wrong case ) Ex. 4.13 ( Assigning multiple variables)
>>> age = 21 >>> id, age, grade = 4327, 24, 9
>>> print(Age) >>> print(age, grade, id)
NameError: name 'Age' is not defined 24, 9, 4327
6. cannot be a keyword. Ex. 4.14 ( Unequal items on either side )
LESSON - TWO >>> id, age, grade = 4327, 24
KEYWORDS ValueError: not enough values to
unpack (expected 3, got 2)
What is keyword?
Ex. 4.15 ( Unequal items on either side )
Keywords are special reserved words, that have
specific meanings and purposes. >>> id, age = 4327,24,9

ValueError: too many values to unpack


(expected 2)
VARIABLES 13

Single Value for multiple Variables print("MA of dollar in 2019:


",id(dollar))
We can assign a single value to multiple variables
simultaneously. dollar = 72

Ex. 4.16 ( Assign single value to multiple variables) print("In 2020, $1 = ",dollar)

>>> id = age = grade = 10 print("MA of dollar in 2020:


",id(dollar))
>>> print(id, age, grade)
In 2019, $1 = 69
10, 10, 10 MA of dollar in 2019: 342545564567
LESSON - THREE In 2020, $1 = 72
MA of dollar in 2020: 342545564586
VARIABLE OPERATIONS
What if we want to delete a variable (remove from
What if we want to know memory location of a memory)?
variable?
❸ Deleting
❶ Memory Location of a variable
you can delete a variable using the syntax below:
you can get Memory location of a variable using the
syntax below: del v

id(variable_name) Ex. 4.19 ( Deleting a variable )

PS. For sake of simplicity, from now onward MA >>> book = 5


means Memory Address of a variable. >>> del book
Ex. 4.17 ( Get Memory Location of a variable ) >>> print(book)
dollar = 69 NameError: name 'a' is not defined
print(“1$ = ”,dollar)
Finally, What about Constants?
print(“MA of dollar:”,id(dollar))
A constant is a type of variable whose value cannot
1$ = 69 be changed. It is helpful to think of constants as
containers that hold information which cannot be
MA of dollar: 342545564567 changed later.

What if we want to change value of a variable? Example – GRAVITY = 9.81

❷ Reassigning PS. We will discuss more about constants in later


Module Chapter.
you can update / reassign value of a variable using
the syntax below: EXERCISE 4.3

v = new_value Create a variable "ap" and accept "apple price"


from user, then print user’s apple price “ap” and its
PS. While variable is reassigning, its memory memory location, then next reassign apple price to
location also changed. 34, again print apple price and its memory
Ex. 4.18 ( Updating the value of variable ) location, finally delete variable "ap"?
Hint: Final Output will be like below
dollar = 69
NameError: name 'ap' is not defined
print("In 2019, $1 = ",dollar)

You might also like