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

More Related Content

Similar to Introduction to python & its applications.ppt

Functions_21_22.pdf
Functions_21_22.pdfFunctions_21_22.pdf
Functions_21_22.pdfpaijitk
 
Functions2.pdf
Functions2.pdfFunctions2.pdf
Functions2.pdfDaddy84
 
1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answersAkash Gawali
 
Advanced Web Technology ass.pdf
Advanced Web Technology ass.pdfAdvanced Web Technology ass.pdf
Advanced Web Technology ass.pdfsimenehanmut
 
PythonStudyMaterialSTudyMaterial.pdf
PythonStudyMaterialSTudyMaterial.pdfPythonStudyMaterialSTudyMaterial.pdf
PythonStudyMaterialSTudyMaterial.pdfdata2businessinsight
 
An Overview Of Python With Functional Programming
An Overview Of Python With Functional ProgrammingAn Overview Of Python With Functional Programming
An Overview Of Python With Functional ProgrammingAdam Getchell
 
C++ Interview Question And Answer
C++ Interview Question And AnswerC++ Interview Question And Answer
C++ Interview Question And AnswerJagan Mohan Bishoyi
 
C++ questions And Answer
C++ questions And AnswerC++ questions And Answer
C++ questions And Answerlavparmar007
 
Pydiomatic
PydiomaticPydiomatic
Pydiomaticrik0
 
Functions_19_20.pdf
Functions_19_20.pdfFunctions_19_20.pdf
Functions_19_20.pdfpaijitk
 
Unit 2function in python.pptx
Unit 2function in python.pptxUnit 2function in python.pptx
Unit 2function in python.pptxvishnupriyapm4
 
Functions_in_Python.pptx
Functions_in_Python.pptxFunctions_in_Python.pptx
Functions_in_Python.pptxkrushnaraj1
 
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...Edureka!
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to pythonMaheshPandit16
 

Similar to Introduction to python & its applications.ppt (20)

Functions_21_22.pdf
Functions_21_22.pdfFunctions_21_22.pdf
Functions_21_22.pdf
 
Functions2.pdf
Functions2.pdfFunctions2.pdf
Functions2.pdf
 
1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers
 
Python Functions.pptx
Python Functions.pptxPython Functions.pptx
Python Functions.pptx
 
Python Functions.pptx
Python Functions.pptxPython Functions.pptx
Python Functions.pptx
 
Advanced Web Technology ass.pdf
Advanced Web Technology ass.pdfAdvanced Web Technology ass.pdf
Advanced Web Technology ass.pdf
 
PythonStudyMaterialSTudyMaterial.pdf
PythonStudyMaterialSTudyMaterial.pdfPythonStudyMaterialSTudyMaterial.pdf
PythonStudyMaterialSTudyMaterial.pdf
 
An Overview Of Python With Functional Programming
An Overview Of Python With Functional ProgrammingAn Overview Of Python With Functional Programming
An Overview Of Python With Functional Programming
 
C++ Interview Question And Answer
C++ Interview Question And AnswerC++ Interview Question And Answer
C++ Interview Question And Answer
 
C++ questions And Answer
C++ questions And AnswerC++ questions And Answer
C++ questions And Answer
 
Pydiomatic
PydiomaticPydiomatic
Pydiomatic
 
Python idiomatico
Python idiomaticoPython idiomatico
Python idiomatico
 
Functions_19_20.pdf
Functions_19_20.pdfFunctions_19_20.pdf
Functions_19_20.pdf
 
Unit 2function in python.pptx
Unit 2function in python.pptxUnit 2function in python.pptx
Unit 2function in python.pptx
 
Functions_in_Python.pptx
Functions_in_Python.pptxFunctions_in_Python.pptx
Functions_in_Python.pptx
 
C++ language
C++ languageC++ language
C++ language
 
Advance python
Advance pythonAdvance python
Advance python
 
Chapter - 4.pptx
Chapter - 4.pptxChapter - 4.pptx
Chapter - 4.pptx
 
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 

More from PradeepNB2

CNC MACHINE TOOLS Introduction Module 1.ppt
CNC MACHINE TOOLS Introduction Module 1.pptCNC MACHINE TOOLS Introduction Module 1.ppt
CNC MACHINE TOOLS Introduction Module 1.pptPradeepNB2
 
DFM welding.pptx
DFM welding.pptxDFM welding.pptx
DFM welding.pptxPradeepNB2
 
DFM casting.pptx
DFM casting.pptxDFM casting.pptx
DFM casting.pptxPradeepNB2
 
DFM Machining.pptx
DFM Machining.pptxDFM Machining.pptx
DFM Machining.pptxPradeepNB2
 
DFM Powder Metallurgy.pptx
DFM Powder Metallurgy.pptxDFM Powder Metallurgy.pptx
DFM Powder Metallurgy.pptxPradeepNB2
 
Joining processes.pptx
Joining processes.pptxJoining processes.pptx
Joining processes.pptxPradeepNB2
 
Automation.pptx
Automation.pptxAutomation.pptx
Automation.pptxPradeepNB2
 

More from PradeepNB2 (7)

CNC MACHINE TOOLS Introduction Module 1.ppt
CNC MACHINE TOOLS Introduction Module 1.pptCNC MACHINE TOOLS Introduction Module 1.ppt
CNC MACHINE TOOLS Introduction Module 1.ppt
 
DFM welding.pptx
DFM welding.pptxDFM welding.pptx
DFM welding.pptx
 
DFM casting.pptx
DFM casting.pptxDFM casting.pptx
DFM casting.pptx
 
DFM Machining.pptx
DFM Machining.pptxDFM Machining.pptx
DFM Machining.pptx
 
DFM Powder Metallurgy.pptx
DFM Powder Metallurgy.pptxDFM Powder Metallurgy.pptx
DFM Powder Metallurgy.pptx
 
Joining processes.pptx
Joining processes.pptxJoining processes.pptx
Joining processes.pptx
 
Automation.pptx
Automation.pptxAutomation.pptx
Automation.pptx
 

Recently uploaded

Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756dollysharma2066
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01KreezheaRecto
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLManishPatel169454
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VDineshKumar4165
 

Recently uploaded (20)

Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 

Introduction to python & its applications.ppt

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