SlideShare a Scribd company logo
C463 / B551
C463 / B551
Artificial Intelligence
Artificial Intelligence
Dana Vrajitoru
Dana Vrajitoru
Python
Python
Python Introduction
Python Introduction
An interpreted, compiled, and interactive, object-
An interpreted, compiled, and interactive, object-
oriented, dynamic, imperative, and open source
oriented, dynamic, imperative, and open source
programming language.
programming language.
Created in early 90's by Guido von Rossum at Stichting
Created in early 90's by Guido von Rossum at Stichting
Mathematisch Centrum in the Netherlands.
Mathematisch Centrum in the Netherlands.
The name comes from the Monty Python and not from
The name comes from the Monty Python and not from
the snake.
the snake.
There is a big community of Python programmers, with
There is a big community of Python programmers, with
conferences and magazines:
conferences and magazines:
http://pycon.org/
http://pycon.org/
Web site:
Web site: www.python.org.
.
Features of Python
Features of Python
Interactive: one can launch a Python console and run
Interactive: one can launch a Python console and run
instructions directly it.
instructions directly it.
Portable: available on most existing systems. It only
Portable: available on most existing systems. It only
requires a C compiler to be ported to any new platform.
requires a C compiler to be ported to any new platform.
Structure: functions, classes, modules.
Structure: functions, classes, modules.
It is easy to embed Python with C and C++.
It is easy to embed Python with C and C++.
The user can write their own code in C or C++ and
The user can write their own code in C or C++ and
compile it as Python modules or functions. That makes
compile it as Python modules or functions. That makes
Python extensible.
Python extensible.
Usual applications: scripts including CGI scripts, GUIs,
Usual applications: scripts including CGI scripts, GUIs,
scientific computing.
scientific computing.
Many existing libraries for all sort of purposes.
Many existing libraries for all sort of purposes.
Syntax Rules
Syntax Rules
The syntax is designed to be simplified as compared to
The syntax is designed to be simplified as compared to
other languages like C/C++.
other languages like C/C++.
Every compound instruction ends with ":"
Every compound instruction ends with ":"
There are no blocks of code; blocks are implicitly created
There are no blocks of code; blocks are implicitly created
by indentation.
by indentation.
Expressions: usual arithmetic operators, named logic
Expressions: usual arithmetic operators, named logic
operators: and, or, not.
operators: and, or, not.
Assignments use the = sign but they don't have to end
Assignments use the = sign but they don't have to end
with ";"
with ";"
Comments start with # as in shell scripting.
Comments start with # as in shell scripting.
Variables are declared by assigning them a value and
Variables are declared by assigning them a value and
they are local to the block where they appear first.
they are local to the block where they appear first.
Control Structures
Control Structures
Conditional:
Conditional:
if
if condition:
condition:
instructions
instructions
elif
elif condition: #*
condition: #*
instructions
instructions
else
else: # optional
: # optional
instructions
instructions
Loops:
Loops:
while
while condition:
condition:
instructions
instructions
else
else: # optional
: # optional
instructions
instructions
for
for var
var in
in S:
S:
instructions
instructions
else
else: # optional
: # optional
instructions
instructions
for
for i
i in
in range(n):
range(n):
instructions
instructions
Built-in Data Structures
Built-in Data Structures
Lists
Lists: linked lists implementing the subscript
: linked lists implementing the subscript
operator:
operator:
x = [1,2,3]
x = [1,2,3]
x.append(4)
x.append(4)
print x[2] # result: 3
print x[2] # result: 3
Tupples
Tupples: constant kind of arrays
: constant kind of arrays
x = (1,2,3)
x = (1,2,3)
Dictionaries
Dictionaries: association lists
: association lists
x = {}
x = {}
x["word"] = reference
x["word"] = reference
for k in x.keys():
for k in x.keys():
print x[k]
print x[k]
Functions and Parameters
Functions and Parameters
Function definition:
Function definition:
def
def function_name (par1, par2, ...):
function_name (par1, par2, ...):
body of the function
body of the function
It supports default values for parameters.
It supports default values for parameters.
All parameters are value parameters.
All parameters are value parameters.
Any variable storing a complex data structure
Any variable storing a complex data structure
contains a reference to it. Any changes to the
contains a reference to it. Any changes to the
content of such a data structure in the function
content of such a data structure in the function
will affect the variable passed in the function
will affect the variable passed in the function
call.
call.
Assignments involving a complex data structure
Assignments involving a complex data structure
don't make a copy of it.
don't make a copy of it.
More Built-in Functions
More Built-in Functions
Function
Function type
type: returns the type of an object.
: returns the type of an object.
type(0)
type(0) – returns <type ‘int’>
– returns <type ‘int’>
Checking if something is an integer:
Checking if something is an integer:
if type(x) == type(0): ...
if type(x) == type(0): ...
Reading a value from the terminal: input()
Reading a value from the terminal: input()
x = input()
x = input()
Returning a value from a function:
Returning a value from a function:
return True
return True
Artificial Intelligence – D. Vrajitoru
Artificial Intelligence – D. Vrajitoru
Example of Conditional
Example of Conditional
def check_type(x):
def check_type(x):
if type(x) == type(0):
if type(x) == type(0):
print x, "is an integer"
print x, "is an integer"
elif type(x) == type(1.0):
elif type(x) == type(1.0):
print x, "is a float"
print x, "is a float"
elif type(x) == type(""):
elif type(x) == type(""):
print x, "is a string"
print x, "is a string"
elif type(x) == type([]):
elif type(x) == type([]):
print x, "is an array"
print x, "is an array"
...
...
Artificial Intelligence – D. Vrajitoru
Artificial Intelligence – D. Vrajitoru
Example of while/else
Example of while/else
def Euler(a, b):
def Euler(a, b):
if b==0:
if b==0:
return a
return a
r = a % b
r = a % b
while r:
while r:
a = b
a = b
b = r
b = r
r = a % b
r = a % b
else:
else:
print "a divisible by b"
print "a divisible by b"
return b
return b
return r
return r
Artificial Intelligence – D. Vrajitoru
Artificial Intelligence – D. Vrajitoru
Booleans
Booleans
Truth values: True and False.
Truth values: True and False.
False is equivalent with 0, and empty list
False is equivalent with 0, and empty list
[], an empty dictionary {}.
[], an empty dictionary {}.
Anything else is equivalent to True.
Anything else is equivalent to True.
Example:
Example:
x = 0
x = 0
if not x:
if not x:
print “0 is False”
print “0 is False”
Artificial Intelligence – D. Vrajitoru
Artificial Intelligence – D. Vrajitoru
Default Values for Parameters
Default Values for Parameters
Default values:
Default values:
def function (var1 = value, var2 = value, ...):
def function (var1 = value, var2 = value, ...):
Just like in C++, all the parameters that have
Just like in C++, all the parameters that have
default values must be grouped at the end.
default values must be grouped at the end.
def GCD1(a=10, b=20): ...
def GCD1(a=10, b=20): ...
GCD1() -> 10
GCD1() -> 10
GCD1(125) -> 5
GCD1(125) -> 5
GCD1(12, 39) -> 3
GCD1(12, 39) -> 3
Artificial Intelligence – D. Vrajitoru
Artificial Intelligence – D. Vrajitoru
Variables and Scope
Variables and Scope
Module: one python file.
Module: one python file.
Global scope: exists in the module in which they
Global scope: exists in the module in which they
are declared.
are declared.
Local scope: local to the function inside which it
Local scope: local to the function inside which it
is declared.
is declared.
Global variables in a module can be accessed
Global variables in a module can be accessed
from somewhere else using the notation
from somewhere else using the notation
module.variable.
module.variable.
Example: string.digits contains ‘0123456789’.
Example: string.digits contains ‘0123456789’.
Artificial Intelligence – D. Vrajitoru
Artificial Intelligence – D. Vrajitoru
Example Scope
Example Scope
def test_scope():
def test_scope():
for i in range(4):
for i in range(4):
for j in range (3):
for j in range (3):
x = i*10+j
x = i*10+j
if x>20:
if x>20:
print x,
print x,
print x
print x
test_scope()
test_scope()
21 22 30 31 32 32
21 22 30 31 32 32
Artificial Intelligence – D. Vrajitoru
Artificial Intelligence – D. Vrajitoru
Try - Except
Try - Except
Try: attempts to execute an instruction.
Try: attempts to execute an instruction.
If the operation is successful, it moves on.
If the operation is successful, it moves on.
If not, we have the option of doing something
If not, we have the option of doing something
else with the instruction
else with the instruction
except:
except:
Another option:
Another option:
except error_type:
except error_type:
which does something only for a particular type
which does something only for a particular type
of exception.
of exception.
Artificial Intelligence – D. Vrajitoru
Artificial Intelligence – D. Vrajitoru
def scope1():
def scope1():
y = 15
y = 15
y = 20
y = 20
def scope2():
def scope2():
y = 25
y = 25
def scope3():
def scope3():
try:
try:
print y
print y
except:
except:
print "cannot access global y"
print "cannot access global y"
print days
print days
y = 25
y = 25
print y
print y
days=["monday", "tuesday"]
days=["monday", "tuesday"]
Artificial Intelligence – D. Vrajitoru
Artificial Intelligence – D. Vrajitoru

More Related Content

Similar to C463_02_python.ppt,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, (20)

PPTX
Basic concept of Python.pptx includes design tool, identifier, variables.
supriyasarkar38
 
PDF
Python Software Engineering Python Software Engineering
ssuser1c86e3
 
PDF
Python Module-1.1.pdf
4HG19EC010HARSHITHAH
 
PPT
FALLSEM2022-23_ITA3007_ETH_VL2022230100613_Reference_Material_I_23-09-2022_py...
admin369652
 
PPTX
python introduction initial lecture unit1.pptx
ChandraPrakash715640
 
PPTX
Introduction-to-Python face clone using python.pptx
pandaashirbad9
 
PPTX
Introduction to Python Programming
VijaySharma802
 
PPTX
introduction to Python | Part 1
Ahmedalhassar1
 
PPTX
PYTHON PPT.pptx python is very useful for day to day life
NaitikSingh33
 
PPT
Pythonintroduction
-jyothish kumar sirigidi
 
PPTX
What is python-presentation FOR CLASS 10.pptx
shuhbou39
 
PDF
Python for data science by www.dmdiploma.com
ShwetaAggarwal56
 
PPTX
Python Programming for problem solving.pptx
NishaM41
 
PPT
python language programming presentation
lbisht2
 
PDF
Introduction To Programming with Python
Sushant Mane
 
PPTX
uom-2552-what-is-python-presentation (1).pptx
Prabha Karan
 
PPTX
uom-2552-what-is-python-presentationalllllll.pptx
gagan1700017
 
PPTX
Intro to python programming (basics) in easy language
MuhammadShahbaz36976
 
PPTX
Problem Solving and Python Programming PPT.This will be helpfull for first ye...
MuppudathiR2
 
PPTX
uom-2552-what-is-python-presentation.pptx
ChetanChauhan203001
 
Basic concept of Python.pptx includes design tool, identifier, variables.
supriyasarkar38
 
Python Software Engineering Python Software Engineering
ssuser1c86e3
 
Python Module-1.1.pdf
4HG19EC010HARSHITHAH
 
FALLSEM2022-23_ITA3007_ETH_VL2022230100613_Reference_Material_I_23-09-2022_py...
admin369652
 
python introduction initial lecture unit1.pptx
ChandraPrakash715640
 
Introduction-to-Python face clone using python.pptx
pandaashirbad9
 
Introduction to Python Programming
VijaySharma802
 
introduction to Python | Part 1
Ahmedalhassar1
 
PYTHON PPT.pptx python is very useful for day to day life
NaitikSingh33
 
Pythonintroduction
-jyothish kumar sirigidi
 
What is python-presentation FOR CLASS 10.pptx
shuhbou39
 
Python for data science by www.dmdiploma.com
ShwetaAggarwal56
 
Python Programming for problem solving.pptx
NishaM41
 
python language programming presentation
lbisht2
 
Introduction To Programming with Python
Sushant Mane
 
uom-2552-what-is-python-presentation (1).pptx
Prabha Karan
 
uom-2552-what-is-python-presentationalllllll.pptx
gagan1700017
 
Intro to python programming (basics) in easy language
MuhammadShahbaz36976
 
Problem Solving and Python Programming PPT.This will be helpfull for first ye...
MuppudathiR2
 
uom-2552-what-is-python-presentation.pptx
ChetanChauhan203001
 

More from divijareddy0502 (7)

PPT
GF_Python_Data_Structures.pptjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj
divijareddy0502
 
PPTX
910383538.pptxnnnnnnnnnnnnnnnnnnnnnnnnnnnn
divijareddy0502
 
PPT
python1.pptpppppppppppppppppppppppppppppppp
divijareddy0502
 
PPT
Py-Slides-1.ppt1234444444444444444444444444444444444444444
divijareddy0502
 
PPTX
15 DataStructures - Chang.pptx111111111111
divijareddy0502
 
PPT
UML-class_diagram.ppt diagrams ppt download
divijareddy0502
 
DOCX
III B.TECH CSE_flutter Lab manual (1).docx
divijareddy0502
 
GF_Python_Data_Structures.pptjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj
divijareddy0502
 
910383538.pptxnnnnnnnnnnnnnnnnnnnnnnnnnnnn
divijareddy0502
 
python1.pptpppppppppppppppppppppppppppppppp
divijareddy0502
 
Py-Slides-1.ppt1234444444444444444444444444444444444444444
divijareddy0502
 
15 DataStructures - Chang.pptx111111111111
divijareddy0502
 
UML-class_diagram.ppt diagrams ppt download
divijareddy0502
 
III B.TECH CSE_flutter Lab manual (1).docx
divijareddy0502
 
Ad

Recently uploaded (20)

PPTX
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
PDF
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
PPTX
Evaluation and thermal analysis of shell and tube heat exchanger as per requi...
shahveer210504
 
PPTX
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
PPT
Electrical Safety Presentation for Basics Learning
AliJaved79382
 
PPTX
Introduction to Design of Machine Elements
PradeepKumarS27
 
PPTX
MATLAB : Introduction , Features , Display Windows, Syntax, Operators, Graph...
Amity University, Patna
 
PDF
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
PPTX
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
PDF
Zilliz Cloud Demo for performance and scale
Zilliz
 
PPTX
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
PPTX
VITEEE 2026 Exam Details , Important Dates
SonaliSingh127098
 
PPTX
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
PDF
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
PDF
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
PDF
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
PDF
smart lot access control system with eye
rasabzahra
 
DOCX
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
PDF
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
PDF
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
Evaluation and thermal analysis of shell and tube heat exchanger as per requi...
shahveer210504
 
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
Electrical Safety Presentation for Basics Learning
AliJaved79382
 
Introduction to Design of Machine Elements
PradeepKumarS27
 
MATLAB : Introduction , Features , Display Windows, Syntax, Operators, Graph...
Amity University, Patna
 
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
Zilliz Cloud Demo for performance and scale
Zilliz
 
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
VITEEE 2026 Exam Details , Important Dates
SonaliSingh127098
 
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
smart lot access control system with eye
rasabzahra
 
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
Ad

C463_02_python.ppt,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

  • 1. C463 / B551 C463 / B551 Artificial Intelligence Artificial Intelligence Dana Vrajitoru Dana Vrajitoru Python Python
  • 2. Python Introduction Python Introduction An interpreted, compiled, and interactive, object- An interpreted, compiled, and interactive, object- oriented, dynamic, imperative, and open source oriented, dynamic, imperative, and open source programming language. programming language. Created in early 90's by Guido von Rossum at Stichting Created in early 90's by Guido von Rossum at Stichting Mathematisch Centrum in the Netherlands. Mathematisch Centrum in the Netherlands. The name comes from the Monty Python and not from The name comes from the Monty Python and not from the snake. the snake. There is a big community of Python programmers, with There is a big community of Python programmers, with conferences and magazines: conferences and magazines: http://pycon.org/ http://pycon.org/ Web site: Web site: www.python.org. .
  • 3. Features of Python Features of Python Interactive: one can launch a Python console and run Interactive: one can launch a Python console and run instructions directly it. instructions directly it. Portable: available on most existing systems. It only Portable: available on most existing systems. It only requires a C compiler to be ported to any new platform. requires a C compiler to be ported to any new platform. Structure: functions, classes, modules. Structure: functions, classes, modules. It is easy to embed Python with C and C++. It is easy to embed Python with C and C++. The user can write their own code in C or C++ and The user can write their own code in C or C++ and compile it as Python modules or functions. That makes compile it as Python modules or functions. That makes Python extensible. Python extensible. Usual applications: scripts including CGI scripts, GUIs, Usual applications: scripts including CGI scripts, GUIs, scientific computing. scientific computing. Many existing libraries for all sort of purposes. Many existing libraries for all sort of purposes.
  • 4. Syntax Rules Syntax Rules The syntax is designed to be simplified as compared to The syntax is designed to be simplified as compared to other languages like C/C++. other languages like C/C++. Every compound instruction ends with ":" Every compound instruction ends with ":" There are no blocks of code; blocks are implicitly created There are no blocks of code; blocks are implicitly created by indentation. by indentation. Expressions: usual arithmetic operators, named logic Expressions: usual arithmetic operators, named logic operators: and, or, not. operators: and, or, not. Assignments use the = sign but they don't have to end Assignments use the = sign but they don't have to end with ";" with ";" Comments start with # as in shell scripting. Comments start with # as in shell scripting. Variables are declared by assigning them a value and Variables are declared by assigning them a value and they are local to the block where they appear first. they are local to the block where they appear first.
  • 5. Control Structures Control Structures Conditional: Conditional: if if condition: condition: instructions instructions elif elif condition: #* condition: #* instructions instructions else else: # optional : # optional instructions instructions Loops: Loops: while while condition: condition: instructions instructions else else: # optional : # optional instructions instructions for for var var in in S: S: instructions instructions else else: # optional : # optional instructions instructions for for i i in in range(n): range(n): instructions instructions
  • 6. Built-in Data Structures Built-in Data Structures Lists Lists: linked lists implementing the subscript : linked lists implementing the subscript operator: operator: x = [1,2,3] x = [1,2,3] x.append(4) x.append(4) print x[2] # result: 3 print x[2] # result: 3 Tupples Tupples: constant kind of arrays : constant kind of arrays x = (1,2,3) x = (1,2,3) Dictionaries Dictionaries: association lists : association lists x = {} x = {} x["word"] = reference x["word"] = reference for k in x.keys(): for k in x.keys(): print x[k] print x[k]
  • 7. Functions and Parameters Functions and Parameters Function definition: Function definition: def def function_name (par1, par2, ...): function_name (par1, par2, ...): body of the function body of the function It supports default values for parameters. It supports default values for parameters. All parameters are value parameters. All parameters are value parameters. Any variable storing a complex data structure Any variable storing a complex data structure contains a reference to it. Any changes to the contains a reference to it. Any changes to the content of such a data structure in the function content of such a data structure in the function will affect the variable passed in the function will affect the variable passed in the function call. call. Assignments involving a complex data structure Assignments involving a complex data structure don't make a copy of it. don't make a copy of it.
  • 8. More Built-in Functions More Built-in Functions Function Function type type: returns the type of an object. : returns the type of an object. type(0) type(0) – returns <type ‘int’> – returns <type ‘int’> Checking if something is an integer: Checking if something is an integer: if type(x) == type(0): ... if type(x) == type(0): ... Reading a value from the terminal: input() Reading a value from the terminal: input() x = input() x = input() Returning a value from a function: Returning a value from a function: return True return True Artificial Intelligence – D. Vrajitoru Artificial Intelligence – D. Vrajitoru
  • 9. Example of Conditional Example of Conditional def check_type(x): def check_type(x): if type(x) == type(0): if type(x) == type(0): print x, "is an integer" print x, "is an integer" elif type(x) == type(1.0): elif type(x) == type(1.0): print x, "is a float" print x, "is a float" elif type(x) == type(""): elif type(x) == type(""): print x, "is a string" print x, "is a string" elif type(x) == type([]): elif type(x) == type([]): print x, "is an array" print x, "is an array" ... ... Artificial Intelligence – D. Vrajitoru Artificial Intelligence – D. Vrajitoru
  • 10. Example of while/else Example of while/else def Euler(a, b): def Euler(a, b): if b==0: if b==0: return a return a r = a % b r = a % b while r: while r: a = b a = b b = r b = r r = a % b r = a % b else: else: print "a divisible by b" print "a divisible by b" return b return b return r return r Artificial Intelligence – D. Vrajitoru Artificial Intelligence – D. Vrajitoru
  • 11. Booleans Booleans Truth values: True and False. Truth values: True and False. False is equivalent with 0, and empty list False is equivalent with 0, and empty list [], an empty dictionary {}. [], an empty dictionary {}. Anything else is equivalent to True. Anything else is equivalent to True. Example: Example: x = 0 x = 0 if not x: if not x: print “0 is False” print “0 is False” Artificial Intelligence – D. Vrajitoru Artificial Intelligence – D. Vrajitoru
  • 12. Default Values for Parameters Default Values for Parameters Default values: Default values: def function (var1 = value, var2 = value, ...): def function (var1 = value, var2 = value, ...): Just like in C++, all the parameters that have Just like in C++, all the parameters that have default values must be grouped at the end. default values must be grouped at the end. def GCD1(a=10, b=20): ... def GCD1(a=10, b=20): ... GCD1() -> 10 GCD1() -> 10 GCD1(125) -> 5 GCD1(125) -> 5 GCD1(12, 39) -> 3 GCD1(12, 39) -> 3 Artificial Intelligence – D. Vrajitoru Artificial Intelligence – D. Vrajitoru
  • 13. Variables and Scope Variables and Scope Module: one python file. Module: one python file. Global scope: exists in the module in which they Global scope: exists in the module in which they are declared. are declared. Local scope: local to the function inside which it Local scope: local to the function inside which it is declared. is declared. Global variables in a module can be accessed Global variables in a module can be accessed from somewhere else using the notation from somewhere else using the notation module.variable. module.variable. Example: string.digits contains ‘0123456789’. Example: string.digits contains ‘0123456789’. Artificial Intelligence – D. Vrajitoru Artificial Intelligence – D. Vrajitoru
  • 14. Example Scope Example Scope def test_scope(): def test_scope(): for i in range(4): for i in range(4): for j in range (3): for j in range (3): x = i*10+j x = i*10+j if x>20: if x>20: print x, print x, print x print x test_scope() test_scope() 21 22 30 31 32 32 21 22 30 31 32 32 Artificial Intelligence – D. Vrajitoru Artificial Intelligence – D. Vrajitoru
  • 15. Try - Except Try - Except Try: attempts to execute an instruction. Try: attempts to execute an instruction. If the operation is successful, it moves on. If the operation is successful, it moves on. If not, we have the option of doing something If not, we have the option of doing something else with the instruction else with the instruction except: except: Another option: Another option: except error_type: except error_type: which does something only for a particular type which does something only for a particular type of exception. of exception. Artificial Intelligence – D. Vrajitoru Artificial Intelligence – D. Vrajitoru
  • 16. def scope1(): def scope1(): y = 15 y = 15 y = 20 y = 20 def scope2(): def scope2(): y = 25 y = 25 def scope3(): def scope3(): try: try: print y print y except: except: print "cannot access global y" print "cannot access global y" print days print days y = 25 y = 25 print y print y days=["monday", "tuesday"] days=["monday", "tuesday"] Artificial Intelligence – D. Vrajitoru Artificial Intelligence – D. Vrajitoru