0% found this document useful (0 votes)
2 views17 pages

08 Variables

Uploaded by

Darshanjr Dachhu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views17 pages

08 Variables

Uploaded by

Darshanjr Dachhu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 17

Lecture 08: Variables

18ESC108A Elements of Computer Science and Engineering


B. Tech. 2018

Course Leader(s):
Prabhakar A.
Roopa G.
Jishmi Jos Choondal
Pujitha V.
Department of Computer Science and Engineering
Faculty of Engineering and Technology
Ramaiah University of Applied Sciences

1
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Objectives

• At the end of this lecture, student will be able to


– understand how to use variables in Python

2
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Topics

• Keywords
• Identifiers
• Variables

3
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Keywords
• Keywords (Reserved Words) are special words that are understood by the Python
interpreter

• A list of the reserved words in Python is given below

• Keywords cannot be used as variable names


4
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Identifiers
• An identifier is a name given to a program element
• Examples of identifiers are variable names, class names and function names

Rules for Naming Identifiers


 The permissible characters are alphabets (lower or upper case), digits and
underscore
 First character must be an underscore, a lower case letter or an upper case letter
 An identifier cannot have the same name as a keyword

• Python is case-sensitive
– FOO is different from foo
5
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
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
6
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Variables

• A variable is a name that refers to a value (actually that refers to an object)

• The values of variables can be changed whenever required


>>> x=10 # x is created here
>>> x=20 # the value of x changes here

7
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
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

• Consider the following three assignments


>>> message = 'And now for something completely different'
>>> n = 17

• The message refers to a str object with value " And now for…. “
• The n refers to a integer object with value 17

8
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
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

• Updating a variable by adding 1 is called an increment; subtracting 1 is called a


decrement

• Before updating a variable, initialize it, usually with a simple assignment


>>> x = 0 # initialisation
>>> x = x+1 # increment
>>> x = x-1 # decrement 9
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Assignment Statement in Python
• Consider the assignment statement
>>> temperature = 98.6 # establishes temperature as an identifier, and then
associates it with the object expressed on the right-
hand side of the equal sign; in this case a floating-
point object with value 98.6

• The identifier temperature refers to an object instance of the float class having
value 98.6

10
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Aliasing

• A programmer can establish an alias by assigning a second identifier to an existing


object

• Continuing with the earlier example, consider the subsequent assignment


>>> original = temperature

• Once an alias has been established, either name can be used to access the
underlying object

11
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Aliasing contd.

• Continuing with the concrete example, consider the command:


>>> temperature = temperature + 5.0 # The execution of this command begins with
the evaluation of the expression on the right-
hand side of the ‘=‘ operator

• The temperature identifier has been assigned a new value, while original continues
to refer to the previously existing value

12
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
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)

>>> type('Hello, World!')


<type 'str'>
>>> number = 10
>>> type(number)
<type ‘int’>
13
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
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

14
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Exchange the Contents of Two Variables contd.

1. >>> var1,var2 = (100,200)


2. >>> var1=var2
3. >>> var2=var1
4. >>> print(var1,var2)

• 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)

• The algorithm is incorrect as it fails to achieve the target or expected configuration


• Question: What has gone wrong and how to rectify the solution or algorithm?
15
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Exchange the Contents of Two Variables contd.

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;

• The algorithm is correct as it achieves the target or expected configuration


16
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Summary
• Keywords (Reserved Words) are special words that are understood by the Python
interpreter
• An identifier is a name given to a program element such as variable names, class
names and function names
• A variable is a name that refers to a value
• An assignment statement creates new variables and gives them values
• Multiple variables can hold references to the same object
• A programmer can establish an alias by assigning a second identifier to an existing
object

17
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences

You might also like