08 Variables
08 Variables
1
Objectives
2
Topics
• Keywords
• Identifiers
• Variables
3
Keywords
• Keywords (Reserved Words) are special words that are understood by the Python
interpreter
5
Identifiers
• An identifier is a name given to a program element
• Examples of identifiers are variable names, class names and function names
• Python is case-sensitive
– FOO is different from foo
6
Identifiers - Examples
• CAN
contain a number from 2nd position h2o
be of mixed cases Xsquared
contain or begin with an underscore _height_
• CANNOT
start with a number 2i
contain any arithmetic operators r*s+t
contain any other punctuation marks #@x%£!!a
be a keyword continue
contain a space my var
7
Variables
8
No Need of Variable Declaration
• No need to declare the variables like other languages such as C,C++ and Java
• Python is a dynamically typed language as there is no advance declaration
associating an identifier with a particular data type
• The message refers to a str object with value " And now for…. “
• The n refers to a integer object with value 17
9
Updating Variables
• One of the most common forms of assignment is an update, where the new value of
the variable depends on the old
• Consider the statement
>>> x = x+1 # get the current value of x, add one, and then update x with the
new value
• The identifier temperature refers to an object instance of the float class having
value 98.6
11
Aliasing
• A programmer can establish an alias by assigning a second identifier to an existing
object
• Aliasing refers to the situation where the same memory location can be accessed
using different names.
• Once an alias has been established, either name can be used to access the
underlying object
12
Aliasing contd.
• The temperature identifier has been assigned a new value, while original continues
to refer to the previously existing value
13
Type of an Item Referred to by a Variable
• Python have a built-in method called type () to figure out the data item referred to
by a variable
• The Syntax
type(object)
14
Example
Output
15
Examples
Output Output
Output Output
16
Exchange the Contents of Two Variables
• Algorithm to exchange the contents of two variables named var1 and var2
Initial configuration
var1 -------------> object with type int and value 100
var2 --------------> object with type int and value 200
Target configuration
var1 -------------> object with type int and value 200
var2 ------------> object with type int and value 100
17
Exchange the Contents of Two Variables contd.
• After step2 is interpreted: var1 -------------> object with type int and value 200;
(object with type int and value 100 is garbage now as no name refers to it
1. After step3 is interpreted: var2 --------------> object with type int and value 200;
(var1 and var2 point to the same object and hence are aliases)
1. >>> var1,var2=(100,200)
2. >>> temp=var1
3. >>> var1=var2
4. >>> var2=temp
5. >>> print(var1,var2)
• After step2 is interpreted: temp ------------ > object with type int and value 100
• After step3 is interpreted: var1 -------------> object with type int and value 200 and
variable temp still refers to object of type int and value 100
• After step4 is interpreted: var2 --------------> object with type int and value 100;
20