SlideShare a Scribd company logo
Industrial training
Presentation
Submitted by
Kapil
on
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 C463_02_python.ppt

James Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on PythonJames Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on Python
CP-Union
 
Functions_in_Python.pptx
Functions_in_Python.pptxFunctions_in_Python.pptx
Functions_in_Python.pptx
krushnaraj1
 
CLASS-11 & 12 ICT PPT Functions in Python.pptx
CLASS-11 & 12 ICT PPT Functions in Python.pptxCLASS-11 & 12 ICT PPT Functions in Python.pptx
CLASS-11 & 12 ICT PPT Functions in Python.pptx
seccoordpal
 
Python Functions.pptx
Python Functions.pptxPython Functions.pptx
Python Functions.pptx
AnuragBharti27
 
Python Functions.pptx
Python Functions.pptxPython Functions.pptx
Python Functions.pptx
AnuragBharti27
 
Unit 2function in python.pptx
Unit 2function in python.pptxUnit 2function in python.pptx
Unit 2function in python.pptx
vishnupriyapm4
 
Functions_19_20.pdf
Functions_19_20.pdfFunctions_19_20.pdf
Functions_19_20.pdf
paijitk
 
1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers
Akash Gawali
 
Chapter - 4.pptx
Chapter - 4.pptxChapter - 4.pptx
Chapter - 4.pptx
MikialeTesfamariam
 
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!
 
cp Module4(1)
cp Module4(1)cp Module4(1)
cp Module4(1)
Amarjith C K
 
cbse class 12 Python Functions2 for class 12 .pptx
cbse class 12 Python Functions2 for class 12 .pptxcbse class 12 Python Functions2 for class 12 .pptx
cbse class 12 Python Functions2 for class 12 .pptx
tcsonline1222
 
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 Answer
Jagan Mohan Bishoyi
 
C++ questions And Answer
C++ questions And AnswerC++ questions And Answer
C++ questions And Answer
lavparmar007
 
Advanced Web Technology ass.pdf
Advanced Web Technology ass.pdfAdvanced Web Technology ass.pdf
Advanced Web Technology ass.pdf
simenehanmut
 
PythonStudyMaterialSTudyMaterial.pdf
PythonStudyMaterialSTudyMaterial.pdfPythonStudyMaterialSTudyMaterial.pdf
PythonStudyMaterialSTudyMaterial.pdf
data2businessinsight
 
Pydiomatic
PydiomaticPydiomatic
Pydiomatic
rik0
 
Python idiomatico
Python idiomaticoPython idiomatico
Python idiomatico
PyCon Italia
 
Functions and Modules.pptx
Functions and Modules.pptxFunctions and Modules.pptx
Functions and Modules.pptx
Ashwini Raut
 

Similar to C463_02_python.ppt (20)

James Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on PythonJames Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on Python
 
Functions_in_Python.pptx
Functions_in_Python.pptxFunctions_in_Python.pptx
Functions_in_Python.pptx
 
CLASS-11 & 12 ICT PPT Functions in Python.pptx
CLASS-11 & 12 ICT PPT Functions in Python.pptxCLASS-11 & 12 ICT PPT Functions in Python.pptx
CLASS-11 & 12 ICT PPT Functions in Python.pptx
 
Python Functions.pptx
Python Functions.pptxPython Functions.pptx
Python Functions.pptx
 
Python Functions.pptx
Python Functions.pptxPython Functions.pptx
Python Functions.pptx
 
Unit 2function in python.pptx
Unit 2function in python.pptxUnit 2function in python.pptx
Unit 2function in python.pptx
 
Functions_19_20.pdf
Functions_19_20.pdfFunctions_19_20.pdf
Functions_19_20.pdf
 
1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers
 
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...
 
cp Module4(1)
cp Module4(1)cp Module4(1)
cp Module4(1)
 
cbse class 12 Python Functions2 for class 12 .pptx
cbse class 12 Python Functions2 for class 12 .pptxcbse class 12 Python Functions2 for class 12 .pptx
cbse class 12 Python Functions2 for class 12 .pptx
 
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
 
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
 
Pydiomatic
PydiomaticPydiomatic
Pydiomatic
 
Python idiomatico
Python idiomaticoPython idiomatico
Python idiomatico
 
Functions and Modules.pptx
Functions and Modules.pptxFunctions and Modules.pptx
Functions and Modules.pptx
 

Recently uploaded

Explore-Insanony: Watch Instagram Stories Secretly
Explore-Insanony: Watch Instagram Stories SecretlyExplore-Insanony: Watch Instagram Stories Secretly
Explore-Insanony: Watch Instagram Stories Secretly
Trending Blogers
 
[HUN][hackersuli] Red Teaming alapok 2024
[HUN][hackersuli] Red Teaming alapok 2024[HUN][hackersuli] Red Teaming alapok 2024
[HUN][hackersuli] Red Teaming alapok 2024
hackersuli
 
Search Result Showing My Post is Now Buried
Search Result Showing My Post is Now BuriedSearch Result Showing My Post is Now Buried
Search Result Showing My Post is Now Buried
Trish Parr
 
Understanding User Behavior with Google Analytics.pdf
Understanding User Behavior with Google Analytics.pdfUnderstanding User Behavior with Google Analytics.pdf
Understanding User Behavior with Google Analytics.pdf
SEO Article Boost
 
假文凭国外(Adelaide毕业证)澳大利亚国立大学毕业证成绩单办理
假文凭国外(Adelaide毕业证)澳大利亚国立大学毕业证成绩单办理假文凭国外(Adelaide毕业证)澳大利亚国立大学毕业证成绩单办理
假文凭国外(Adelaide毕业证)澳大利亚国立大学毕业证成绩单办理
cuobya
 
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
3ipehhoa
 
办理毕业证(UPenn毕业证)宾夕法尼亚大学毕业证成绩单快速办理
办理毕业证(UPenn毕业证)宾夕法尼亚大学毕业证成绩单快速办理办理毕业证(UPenn毕业证)宾夕法尼亚大学毕业证成绩单快速办理
办理毕业证(UPenn毕业证)宾夕法尼亚大学毕业证成绩单快速办理
uehowe
 
Meet up Milano 14 _ Axpo Italia_ Migration from Mule3 (On-prem) to.pdf
Meet up Milano 14 _ Axpo Italia_ Migration from Mule3 (On-prem) to.pdfMeet up Milano 14 _ Axpo Italia_ Migration from Mule3 (On-prem) to.pdf
Meet up Milano 14 _ Axpo Italia_ Migration from Mule3 (On-prem) to.pdf
Florence Consulting
 
制作毕业证书(ANU毕业证)莫纳什大学毕业证成绩单官方原版办理
制作毕业证书(ANU毕业证)莫纳什大学毕业证成绩单官方原版办理制作毕业证书(ANU毕业证)莫纳什大学毕业证成绩单官方原版办理
制作毕业证书(ANU毕业证)莫纳什大学毕业证成绩单官方原版办理
cuobya
 
Internet of Things in Manufacturing: Revolutionizing Efficiency & Quality | C...
Internet of Things in Manufacturing: Revolutionizing Efficiency & Quality | C...Internet of Things in Manufacturing: Revolutionizing Efficiency & Quality | C...
Internet of Things in Manufacturing: Revolutionizing Efficiency & Quality | C...
CIOWomenMagazine
 
7 Best Cloud Hosting Services to Try Out in 2024
7 Best Cloud Hosting Services to Try Out in 20247 Best Cloud Hosting Services to Try Out in 2024
7 Best Cloud Hosting Services to Try Out in 2024
Danica Gill
 
存档可查的(USC毕业证)南加利福尼亚大学毕业证成绩单制做办理
存档可查的(USC毕业证)南加利福尼亚大学毕业证成绩单制做办理存档可查的(USC毕业证)南加利福尼亚大学毕业证成绩单制做办理
存档可查的(USC毕业证)南加利福尼亚大学毕业证成绩单制做办理
fovkoyb
 
可查真实(Monash毕业证)西澳大学毕业证成绩单退学买
可查真实(Monash毕业证)西澳大学毕业证成绩单退学买可查真实(Monash毕业证)西澳大学毕业证成绩单退学买
可查真实(Monash毕业证)西澳大学毕业证成绩单退学买
cuobya
 
制作原版1:1(Monash毕业证)莫纳什大学毕业证成绩单办理假
制作原版1:1(Monash毕业证)莫纳什大学毕业证成绩单办理假制作原版1:1(Monash毕业证)莫纳什大学毕业证成绩单办理假
制作原版1:1(Monash毕业证)莫纳什大学毕业证成绩单办理假
ukwwuq
 
Italy Agriculture Equipment Market Outlook to 2027
Italy Agriculture Equipment Market Outlook to 2027Italy Agriculture Equipment Market Outlook to 2027
Italy Agriculture Equipment Market Outlook to 2027
harveenkaur52
 
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptxBridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
Brad Spiegel Macon GA
 
Ready to Unlock the Power of Blockchain!
Ready to Unlock the Power of Blockchain!Ready to Unlock the Power of Blockchain!
Ready to Unlock the Power of Blockchain!
Toptal Tech
 
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
APNIC
 
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
ufdana
 
Bài tập unit 1 English in the world.docx
Bài tập unit 1 English in the world.docxBài tập unit 1 English in the world.docx
Bài tập unit 1 English in the world.docx
nhiyenphan2005
 

Recently uploaded (20)

Explore-Insanony: Watch Instagram Stories Secretly
Explore-Insanony: Watch Instagram Stories SecretlyExplore-Insanony: Watch Instagram Stories Secretly
Explore-Insanony: Watch Instagram Stories Secretly
 
[HUN][hackersuli] Red Teaming alapok 2024
[HUN][hackersuli] Red Teaming alapok 2024[HUN][hackersuli] Red Teaming alapok 2024
[HUN][hackersuli] Red Teaming alapok 2024
 
Search Result Showing My Post is Now Buried
Search Result Showing My Post is Now BuriedSearch Result Showing My Post is Now Buried
Search Result Showing My Post is Now Buried
 
Understanding User Behavior with Google Analytics.pdf
Understanding User Behavior with Google Analytics.pdfUnderstanding User Behavior with Google Analytics.pdf
Understanding User Behavior with Google Analytics.pdf
 
假文凭国外(Adelaide毕业证)澳大利亚国立大学毕业证成绩单办理
假文凭国外(Adelaide毕业证)澳大利亚国立大学毕业证成绩单办理假文凭国外(Adelaide毕业证)澳大利亚国立大学毕业证成绩单办理
假文凭国外(Adelaide毕业证)澳大利亚国立大学毕业证成绩单办理
 
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
 
办理毕业证(UPenn毕业证)宾夕法尼亚大学毕业证成绩单快速办理
办理毕业证(UPenn毕业证)宾夕法尼亚大学毕业证成绩单快速办理办理毕业证(UPenn毕业证)宾夕法尼亚大学毕业证成绩单快速办理
办理毕业证(UPenn毕业证)宾夕法尼亚大学毕业证成绩单快速办理
 
Meet up Milano 14 _ Axpo Italia_ Migration from Mule3 (On-prem) to.pdf
Meet up Milano 14 _ Axpo Italia_ Migration from Mule3 (On-prem) to.pdfMeet up Milano 14 _ Axpo Italia_ Migration from Mule3 (On-prem) to.pdf
Meet up Milano 14 _ Axpo Italia_ Migration from Mule3 (On-prem) to.pdf
 
制作毕业证书(ANU毕业证)莫纳什大学毕业证成绩单官方原版办理
制作毕业证书(ANU毕业证)莫纳什大学毕业证成绩单官方原版办理制作毕业证书(ANU毕业证)莫纳什大学毕业证成绩单官方原版办理
制作毕业证书(ANU毕业证)莫纳什大学毕业证成绩单官方原版办理
 
Internet of Things in Manufacturing: Revolutionizing Efficiency & Quality | C...
Internet of Things in Manufacturing: Revolutionizing Efficiency & Quality | C...Internet of Things in Manufacturing: Revolutionizing Efficiency & Quality | C...
Internet of Things in Manufacturing: Revolutionizing Efficiency & Quality | C...
 
7 Best Cloud Hosting Services to Try Out in 2024
7 Best Cloud Hosting Services to Try Out in 20247 Best Cloud Hosting Services to Try Out in 2024
7 Best Cloud Hosting Services to Try Out in 2024
 
存档可查的(USC毕业证)南加利福尼亚大学毕业证成绩单制做办理
存档可查的(USC毕业证)南加利福尼亚大学毕业证成绩单制做办理存档可查的(USC毕业证)南加利福尼亚大学毕业证成绩单制做办理
存档可查的(USC毕业证)南加利福尼亚大学毕业证成绩单制做办理
 
可查真实(Monash毕业证)西澳大学毕业证成绩单退学买
可查真实(Monash毕业证)西澳大学毕业证成绩单退学买可查真实(Monash毕业证)西澳大学毕业证成绩单退学买
可查真实(Monash毕业证)西澳大学毕业证成绩单退学买
 
制作原版1:1(Monash毕业证)莫纳什大学毕业证成绩单办理假
制作原版1:1(Monash毕业证)莫纳什大学毕业证成绩单办理假制作原版1:1(Monash毕业证)莫纳什大学毕业证成绩单办理假
制作原版1:1(Monash毕业证)莫纳什大学毕业证成绩单办理假
 
Italy Agriculture Equipment Market Outlook to 2027
Italy Agriculture Equipment Market Outlook to 2027Italy Agriculture Equipment Market Outlook to 2027
Italy Agriculture Equipment Market Outlook to 2027
 
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptxBridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
 
Ready to Unlock the Power of Blockchain!
Ready to Unlock the Power of Blockchain!Ready to Unlock the Power of Blockchain!
Ready to Unlock the Power of Blockchain!
 
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
 
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
 
Bài tập unit 1 English in the world.docx
Bài tập unit 1 English in the world.docxBài tập unit 1 English in the world.docx
Bài tập unit 1 English in the world.docx
 

C463_02_python.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