[go: up one dir, main page]

0% found this document useful (0 votes)
13 views20 pages

08 Variables

Uploaded by

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

08 Variables

Uploaded by

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

Lecture 08: Variables

Course Leader: Dr.Vinutha H


Department of Computer Science and Engineering
Faculty of Engineering and Technology
M. S. Ramaiah University of Applied Sciences

1
Objectives

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


– understand how to use variables in Python

2
Topics

• Keywords
• Identifiers
• Variables

3
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
Keywords

• Keywords in Python are reserved


words that can not be used as a
variable name, function name, or
any other identifier.

5
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
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

• 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

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

• 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

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

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

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.

• 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

12
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

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)

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


<type 'str'>
>>> number = 10
>>> type(number)
<type ‘int’>

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.

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?
18
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


19
Thank You

20

You might also like