0% found this document useful (0 votes)
13 views

03python 03 Variables in PYTHON

The code snippets contain syntax errors that will cause them to fail. The variable name "f name" is invalid as it contains a space. Printing "1,00,000" will fail as integers cannot contain commas. Assigning "09" to a variable will cause an error as it is not a valid integer literal in Python.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

03python 03 Variables in PYTHON

The code snippets contain syntax errors that will cause them to fail. The variable name "f name" is invalid as it contains a space. Printing "1,00,000" will fail as integers cannot contain commas. Assigning "09" to a variable will cause an error as it is not a valid integer literal in Python.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

PYTHON Programming (CST310)

Lecture 3: Variables in Python

Department of Computer Science and Engineering


National Institute of Technology, Srinagar, Jammu and Kashmir
August 11, 2021
Variables in Python
• Variables are nothing but reserved memory locations to store some values.
• Variables are the name given to a memory location.
• It is a basic unit of storage in a program.
• Variables in Python are not statically typed.
• The variables are dynamically typed in Python. We do not need to declare variables
before using them or declare their type. A variable is created the moment we first assign
a value to it
• The data-types of a variable is given to that variable during the execution based upon the
value that is stored by the user in that variable.
• Age=10 will create a variable with name age and value 10. Since, value 10 is stored in
variable age, so during the execution, python interpreter will give the type Integer to this
variable.
• To check the datatype of any variable, use the type() function as :
type(variable_name)
• The value stored in a variable can be changed during program execution.
• A variable is only a name given to a memory location, all the operations done on the
variable effects that memory location.
Printing the value of a Variable in Python
A=10
print(A)

Output will be: 10


Similarly,
print(“value of variable A is ”,A)
Output will be: value of variable A is 10
• If we have following two variables:
fname=“John”
lname=“Smith”

How you will get the output as following using the above two variables:
My first name is John and My last name is Smith
Print(“My first name is”,fname,” and my last name is”,lname)
Python is a Case-Sensitive Language
• Age=10
• AGE=20
• age=30
If we declare the above three variables with same name, then what will
be the output of following statement
Print(Age)
Re-declare the Variable:
• We can re-declare the python variable once we have declared the
variable already.
Number = 100
print(“Initial Value: ", Number)

# re-declare the var


Number = 120.3
print("After re-declare:", Number)
Assigning a single value to multiple variables:
• Also, Python allows assigning a single value to several variables
simultaneously with “=” operators.
a = b = c = 10
print(a)
print(b)
print(c)
Assigning different values to multiple
variables:
• Python allows adding different values in a single line with
“,”operators.
a, b, c = 1, 20.2, “Nit Srinagar“
print(a)
print(b)
print(c)
Can we use the same name for different
types?
• If we use the same name, the variable starts referring to a new value
and type.
a = 10
a = “NIT Srinagar"
print(a)
Performing different operations on Variables
a=10
b=20
Print(a+b) # this will add the two integers and give output 30
Performing different operations on Variables
a=10
b=20.5
Print(a+b) # What will be the output?

30 or 30.5
Performing different operations on Variables
a=10
c=“NIT Srinagar”
d=“CSE”
print(c+d) // This will do the concatenation of String
Print(a+c) // this will give us error. (TypeError: unsupported
operand type(s) for +: 'int' and 'str’)
How Variables are stored in memory
• Whenever you write the statement
‘a=100’
Value 100 is getting stored in some memory location and variable with
name “a” is pointing to that memory location. So, variable with name
“a” will point to a memory location where 100 is stored.
You can check the memory location by id(variable_name)
id(a)
How Variables are stored in memory
Suppose, a=100
id(a) gives some memory location
If, we write a=200, then value 200 will be stored in some new memory
location and variable with name a will start pointing to that memory
location of 200.
id(a) will give new value.
How Variables are stored in memory
Suppose, a=100
b=100
So, here b will point to the same memory location where a was
pointing. Because, value 100 was already stored as an object in some
memory location and for proper memory utilization, python does not
store the same value.
So, variable will point to the same memory location.
id(a)
Id(b)
How Variables are stored in memory
There are two types of memory which is used by python
• Stack Memory: used for storing the state of the program
• Heap Memory: used for storing the variables/objects

Python manager uses the concept called Reference counting, whenever a new object is
created it will update the reference count of the object.

A=100
b=100
Here, python memory manager creates only one object i.e "100" and reference count is
"2".
Whenever a new object is created python manager will checks the memory for the
object.
If object already exists python manager will not create new object instead it references
the name to the object and increases the reference counter of the object.
What will be the output?
>>> f name = 5
>>> print(1,00,000)
>>>month = 09

You might also like