100% found this document useful (1 vote)
8 views3 pages

Python Assignment Statement Algorithm

The document outlines the algorithm for assignment statements in Python, detailing the evaluation of the right-hand side (RHS) and the handling of variable names in the symbol table. It describes the steps involved in checking for literal expressions, variable names, and managing reference counts for objects. The general syntax and examples of assignment statements in Python are also provided.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
8 views3 pages

Python Assignment Statement Algorithm

The document outlines the algorithm for assignment statements in Python, detailing the evaluation of the right-hand side (RHS) and the handling of variable names in the symbol table. It describes the steps involved in checking for literal expressions, variable names, and managing reference counts for objects. The general syntax and examples of assignment statements in Python are also provided.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Core Programming Academy Python Notes 1: 1

ASSIGNMENT STATEMENT ALGORITHM IN PYTHON

• Diagram:
[1] Evaluate Right Hand Side of statement

[2] Literal expression encountered [2] Variable name encountered

[3] Literal expression itself is a value [3] Lookup variable name in symbol
table

Not Present Present

[4] Raise [4] Get ID of


NameError object and
Exception fetch value

[5] Values in the expression are


obtained

[6] Infer the type of object & yield object of that type as
evaluation of RHS

[7] Check whether Left Hand Side is present

YES NO

[8] Look for variable name in STOP


Symbol Table and check if it is
already present.
Core Programming Academy – An academy by Yogeshwar Shukla
Mobile: +91 956 154 7043
Email: coreprogrammingacademy@[Link]
Core Programming Academy Python Notes 1: 2

[8]th Step

YES NO

[9] Mark variable associated the [9] Prepare next Symbol Table entry
with object as ‘old’ and associate variable name with
ID of object

[10] Replace the ID of old object [10] Increment reference count of


with the new ID of RHS object object given by RHS by 1

[11] Decrement reference count of


old object by 1 and increment
reference count of new object by 1

STOP

• General Syntax:
Left Hand Side = Right Hand Side
where right hand side is an expression and left hand is a variable name.

• Examples:
n1 = 10
n2 = 20
sum = n1 + n2
s = “Hello”
etc.

Core Programming Academy – An academy by Yogeshwar Shukla


Mobile: +91 956 154 7043
Email: coreprogrammingacademy@[Link]
Core Programming Academy Python Notes 1: 3

• Generalized algorithm for evaluating any assignment statement in


Python.
o STEP I: Evaluate right hand side expression.
1. If literal expression is encountered, then that literal itself is a value.
2. If literal expression is a variable name, then look up the variable name
in the symbol table.
1. If not found: raise NameError Exception.
2. If found, then get the ‘id’ of an object associated with the
variable name and fetch value component from it.
3. Applying STEP I – 1 (in case of literal) and STEP I – 2 in case variable
name, all the values in the expression are available now.
o STEP II:
1. Apply all the operators in the expressions on the obtained values in
STEP – I and get the final value of an expression.
2. Infer the type of the value obtained in STEP I – 4.
3. Search the object with obtained value in STEP II – 1 and obtained type
in STEP II – 2 in the object pool.
1. If found, then get the ‘id’ of that object.
2. If not found, then allocate object with the obtained value and
obtained type and get it’s ‘id’
4. Check if Left Hand Side variable name is present or not.
1. If not, then STOP.
2. If yes, then go to STEP III.
o STEP III:
1. Look up for the Left Hand Side variable in the symbol table. If not
found, then execute STEP III – 2 and if found then execute STEP III – 3
2. Handler of the case – “LHS variable name not found” –
1. Create a new symbol table entry which binds obtained LHS
variable name and ‘id’ of an object in STEP III – 3.
2. Increment the reference count of an object by 1.
3. STOP
3. Handler of the case – “LHS variable already present”
1. Get the ‘id’ of associated object in the symbol table and term it
as an ‘old id’. And let’s term id obtained in STEP II -3 as a ‘new id’
2. In the existing symbol table entry of LHS variable name, replace
‘old id’ by ‘new id’.
3. Decrement the reference count of ‘old id’ by 1 and increment
the reference count of ‘new id’ by 1.
4. STOP.

Core Programming Academy – An academy by Yogeshwar Shukla


Mobile: +91 956 154 7043
Email: coreprogrammingacademy@[Link]

You might also like