SlideShare a Scribd company logo
1 of 23
Download to read offline
Lambda functions
PYTHON DATA SCIENCE TOOLBOX (PART 1)
Hugo Bowne-Anderson
Instructor
PYTHON DATA SCIENCE TOOLBOX (PART 1)
Lambda functions
raise_to_power = lambda x, y: x ** y
raise_to_power(2, 3)
8
PYTHON DATA SCIENCE TOOLBOX (PART 1)
Anonymous functions
Function map takes two arguments: map(func, seq)
map() applies the function to ALL elements in the sequence
nums = [48, 6, 9, 21, 1]
square_all = map(lambda num: num ** 2, nums)
print(square_all)
<map object at 0x103e065c0>
print(list(square_all))
[2304, 36, 81, 441, 1]
Let's practice!
PYTHON DATA SCIENCE TOOLBOX (PART 1)
Introduction to error
handling
PYTHON DATA SCIENCE TOOLBOX (PART 1)
Hugo Bowne-Anderson
Instructor
PYTHON DATA SCIENCE TOOLBOX (PART 1)
The float() function
PYTHON DATA SCIENCE TOOLBOX (PART 1)
Passing an incorrect argument
float(2)
2.0
float('2.3')
2.3
float('hello')
<hr />---------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-3-d0ce8bccc8b2> in <module>()
<hr />-> 1 float('hi')
ValueError: could not convert string to float: 'hello'
PYTHON DATA SCIENCE TOOLBOX (PART 1)
Passing valid arguments
def sqrt(x):
"""Returns the square root of a number."""
return x ** (0.5)
sqrt(4)
2.0
sqrt(10)
3.1622776601683795
PYTHON DATA SCIENCE TOOLBOX (PART 1)
Passing invalid arguments
sqrt('hello')
------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-4-cfb99c64761f> in <module>()
----> 1 sqrt('hello')
<ipython-input-1-939b1a60b413> in sqrt(x)
1 def sqrt(x):
----> 2 return x**(0.5)
TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'float'
PYTHON DATA SCIENCE TOOLBOX (PART 1)
Errors and exceptions
Exceptions - caught during execution
Catch exceptions with try-except clause
Runs the code following try
If there’s an exception, run the code following except
PYTHON DATA SCIENCE TOOLBOX (PART 1)
Errors and exceptions
def sqrt(x):
"""Returns the square root of a number."""
try:
return x ** 0.5
except:
print('x must be an int or float')
sqrt(4)
2.0
sqrt(10.0)
3.1622776601683795
sqrt('hi')
x must be an int or float
PYTHON DATA SCIENCE TOOLBOX (PART 1)
Errors and exceptions
def sqrt(x):
"""Returns the square root of a number."""
try:
return x ** 0.5
except TypeError:
print('x must be an int or float')
PYTHON DATA SCIENCE TOOLBOX (PART 1)
Errors and exceptions
sqrt(-9)
(1.8369701987210297e-16+3j)
def sqrt(x):
"""Returns the square root of a number."""
if x < 0:
raise ValueError('x must be non-negative')
try:
return x ** 0.5
except TypeError:
print('x must be an int or float')
PYTHON DATA SCIENCE TOOLBOX (PART 1)
Errors and exceptions
sqrt(-2)
-----------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-2-4cf32322fa95> in <module>()
----> 1 sqrt(-2)
<ipython-input-1-a7b8126942e3> in sqrt(x)
1 def sqrt(x):
2 if x < 0:
----> 3 raise ValueError('x must be non-negative')
4 try:
5 return x**(0.5)
ValueError: x must be non-negative
Let's practice!
PYTHON DATA SCIENCE TOOLBOX (PART 1)
Bringing it all
together
PYTHON DATA SCIENCE TOOLBOX (PART 1)
Hugo Bowne-Anderson
Instructor
PYTHON DATA SCIENCE TOOLBOX (PART 1)
Errors and exceptions
def sqrt(x):
try:
return x ** 0.5
except:
print('x must be an int or float')
sqrt(4)
2.0
sqrt('hi')
x must be an int or float
PYTHON DATA SCIENCE TOOLBOX (PART 1)
Errors and exceptions
def sqrt(x):
if x < 0:
raise ValueError('x must be non-negative')
try:
return x ** 0.5
except TypeError:
print('x must be an int or float')
Let's practice!
PYTHON DATA SCIENCE TOOLBOX (PART 1)
Congratulations!
PYTHON DATA SCIENCE TOOLBOX (PART 1)
Hugo Bowne-Anderson
Instructor
PYTHON DATA SCIENCE TOOLBOX (PART 1)
What you’ve learned:
Write functions that accept single and multiple arguments
Write functions that return one or many values
Use default, exible, and keyword arguments
Global and local scope in functions
Write lambda functions
Handle errors
PYTHON DATA SCIENCE TOOLBOX (PART 1)
There’s more to learn!
Create lists with list comprehensions
Iterators - you’ve seen them before!
Case studies to apply these techniques to Data Science
Let's practice!
PYTHON DATA SCIENCE TOOLBOX (PART 1)

More Related Content

Similar to chapter3 (2).pdf

Lecture 3.1 to 3.2 bt
Lecture 3.1 to 3.2 btLecture 3.1 to 3.2 bt
Lecture 3.1 to 3.2 btbtmathematics
 
Programs in array using SWIFT
Programs in array using SWIFTPrograms in array using SWIFT
Programs in array using SWIFTvikram mahendra
 
Introduction to Neural Networks and Deep Learning from Scratch
Introduction to Neural Networks and Deep Learning from ScratchIntroduction to Neural Networks and Deep Learning from Scratch
Introduction to Neural Networks and Deep Learning from ScratchAhmed BESBES
 
Functional programming in Python
Functional programming in PythonFunctional programming in Python
Functional programming in PythonColin Su
 
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014PyData
 
Introduction to R programming
Introduction to R programmingIntroduction to R programming
Introduction to R programmingAlberto Labarga
 
Digital signal Processing all matlab code with Lab report
Digital signal Processing all matlab code with Lab report Digital signal Processing all matlab code with Lab report
Digital signal Processing all matlab code with Lab report Alamgir Hossain
 
Gentle Introduction to Functional Programming
Gentle Introduction to Functional ProgrammingGentle Introduction to Functional Programming
Gentle Introduction to Functional ProgrammingSaurabh Singh
 
Kristhyan kurtlazartezubia evidencia1-metodosnumericos
Kristhyan kurtlazartezubia evidencia1-metodosnumericosKristhyan kurtlazartezubia evidencia1-metodosnumericos
Kristhyan kurtlazartezubia evidencia1-metodosnumericosKristhyanAndreeKurtL
 
Python programming workshop session 3
Python programming workshop session 3Python programming workshop session 3
Python programming workshop session 3Abdul Haseeb
 
Redux saga: managing your side effects. Also: generators in es6
Redux saga: managing your side effects. Also: generators in es6Redux saga: managing your side effects. Also: generators in es6
Redux saga: managing your side effects. Also: generators in es6Ignacio Martín
 
17, r) -,r I -l19.t... 121.2t-314 23. ^t -rr - .docx
17, r) -,r I  -l19.t... 121.2t-314 23. ^t -rr - .docx17, r) -,r I  -l19.t... 121.2t-314 23. ^t -rr - .docx
17, r) -,r I -l19.t... 121.2t-314 23. ^t -rr - .docxhyacinthshackley2629
 
Python matplotlib cheat_sheet
Python matplotlib cheat_sheetPython matplotlib cheat_sheet
Python matplotlib cheat_sheetNishant Upadhyay
 
User defined functions
User defined functionsUser defined functions
User defined functionsshubham_jangid
 

Similar to chapter3 (2).pdf (20)

Lecture 3.1 to 3.2 bt
Lecture 3.1 to 3.2 btLecture 3.1 to 3.2 bt
Lecture 3.1 to 3.2 bt
 
signal and system
signal and system signal and system
signal and system
 
Seminar PSU 10.10.2014 mme
Seminar PSU 10.10.2014 mmeSeminar PSU 10.10.2014 mme
Seminar PSU 10.10.2014 mme
 
Programs in array using SWIFT
Programs in array using SWIFTPrograms in array using SWIFT
Programs in array using SWIFT
 
Introduction to Neural Networks and Deep Learning from Scratch
Introduction to Neural Networks and Deep Learning from ScratchIntroduction to Neural Networks and Deep Learning from Scratch
Introduction to Neural Networks and Deep Learning from Scratch
 
Functional programming in Python
Functional programming in PythonFunctional programming in Python
Functional programming in Python
 
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
 
Perm winter school 2014.01.31
Perm winter school 2014.01.31Perm winter school 2014.01.31
Perm winter school 2014.01.31
 
Introduction to R programming
Introduction to R programmingIntroduction to R programming
Introduction to R programming
 
Digital signal Processing all matlab code with Lab report
Digital signal Processing all matlab code with Lab report Digital signal Processing all matlab code with Lab report
Digital signal Processing all matlab code with Lab report
 
1010n3a
1010n3a1010n3a
1010n3a
 
Gentle Introduction to Functional Programming
Gentle Introduction to Functional ProgrammingGentle Introduction to Functional Programming
Gentle Introduction to Functional Programming
 
Kristhyan kurtlazartezubia evidencia1-metodosnumericos
Kristhyan kurtlazartezubia evidencia1-metodosnumericosKristhyan kurtlazartezubia evidencia1-metodosnumericos
Kristhyan kurtlazartezubia evidencia1-metodosnumericos
 
Python programming workshop session 3
Python programming workshop session 3Python programming workshop session 3
Python programming workshop session 3
 
Java VS Python
Java VS PythonJava VS Python
Java VS Python
 
Redux saga: managing your side effects. Also: generators in es6
Redux saga: managing your side effects. Also: generators in es6Redux saga: managing your side effects. Also: generators in es6
Redux saga: managing your side effects. Also: generators in es6
 
17, r) -,r I -l19.t... 121.2t-314 23. ^t -rr - .docx
17, r) -,r I  -l19.t... 121.2t-314 23. ^t -rr - .docx17, r) -,r I  -l19.t... 121.2t-314 23. ^t -rr - .docx
17, r) -,r I -l19.t... 121.2t-314 23. ^t -rr - .docx
 
Python matplotlib cheat_sheet
Python matplotlib cheat_sheetPython matplotlib cheat_sheet
Python matplotlib cheat_sheet
 
Introduction to MATLAB
Introduction to MATLABIntroduction to MATLAB
Introduction to MATLAB
 
User defined functions
User defined functionsUser defined functions
User defined functions
 

Recently uploaded

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
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
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
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
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
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...RajaP95
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 

Recently uploaded (20)

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...
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
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...
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
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
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 

chapter3 (2).pdf

  • 1. Lambda functions PYTHON DATA SCIENCE TOOLBOX (PART 1) Hugo Bowne-Anderson Instructor
  • 2. PYTHON DATA SCIENCE TOOLBOX (PART 1) Lambda functions raise_to_power = lambda x, y: x ** y raise_to_power(2, 3) 8
  • 3. PYTHON DATA SCIENCE TOOLBOX (PART 1) Anonymous functions Function map takes two arguments: map(func, seq) map() applies the function to ALL elements in the sequence nums = [48, 6, 9, 21, 1] square_all = map(lambda num: num ** 2, nums) print(square_all) <map object at 0x103e065c0> print(list(square_all)) [2304, 36, 81, 441, 1]
  • 4. Let's practice! PYTHON DATA SCIENCE TOOLBOX (PART 1)
  • 5. Introduction to error handling PYTHON DATA SCIENCE TOOLBOX (PART 1) Hugo Bowne-Anderson Instructor
  • 6. PYTHON DATA SCIENCE TOOLBOX (PART 1) The float() function
  • 7. PYTHON DATA SCIENCE TOOLBOX (PART 1) Passing an incorrect argument float(2) 2.0 float('2.3') 2.3 float('hello') <hr />--------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-3-d0ce8bccc8b2> in <module>() <hr />-> 1 float('hi') ValueError: could not convert string to float: 'hello'
  • 8. PYTHON DATA SCIENCE TOOLBOX (PART 1) Passing valid arguments def sqrt(x): """Returns the square root of a number.""" return x ** (0.5) sqrt(4) 2.0 sqrt(10) 3.1622776601683795
  • 9. PYTHON DATA SCIENCE TOOLBOX (PART 1) Passing invalid arguments sqrt('hello') ------------------------------------------------------------------ TypeError Traceback (most recent call last) <ipython-input-4-cfb99c64761f> in <module>() ----> 1 sqrt('hello') <ipython-input-1-939b1a60b413> in sqrt(x) 1 def sqrt(x): ----> 2 return x**(0.5) TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'float'
  • 10. PYTHON DATA SCIENCE TOOLBOX (PART 1) Errors and exceptions Exceptions - caught during execution Catch exceptions with try-except clause Runs the code following try If there’s an exception, run the code following except
  • 11. PYTHON DATA SCIENCE TOOLBOX (PART 1) Errors and exceptions def sqrt(x): """Returns the square root of a number.""" try: return x ** 0.5 except: print('x must be an int or float') sqrt(4) 2.0 sqrt(10.0) 3.1622776601683795 sqrt('hi') x must be an int or float
  • 12. PYTHON DATA SCIENCE TOOLBOX (PART 1) Errors and exceptions def sqrt(x): """Returns the square root of a number.""" try: return x ** 0.5 except TypeError: print('x must be an int or float')
  • 13. PYTHON DATA SCIENCE TOOLBOX (PART 1) Errors and exceptions sqrt(-9) (1.8369701987210297e-16+3j) def sqrt(x): """Returns the square root of a number.""" if x < 0: raise ValueError('x must be non-negative') try: return x ** 0.5 except TypeError: print('x must be an int or float')
  • 14. PYTHON DATA SCIENCE TOOLBOX (PART 1) Errors and exceptions sqrt(-2) ----------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-2-4cf32322fa95> in <module>() ----> 1 sqrt(-2) <ipython-input-1-a7b8126942e3> in sqrt(x) 1 def sqrt(x): 2 if x < 0: ----> 3 raise ValueError('x must be non-negative') 4 try: 5 return x**(0.5) ValueError: x must be non-negative
  • 15. Let's practice! PYTHON DATA SCIENCE TOOLBOX (PART 1)
  • 16. Bringing it all together PYTHON DATA SCIENCE TOOLBOX (PART 1) Hugo Bowne-Anderson Instructor
  • 17. PYTHON DATA SCIENCE TOOLBOX (PART 1) Errors and exceptions def sqrt(x): try: return x ** 0.5 except: print('x must be an int or float') sqrt(4) 2.0 sqrt('hi') x must be an int or float
  • 18. PYTHON DATA SCIENCE TOOLBOX (PART 1) Errors and exceptions def sqrt(x): if x < 0: raise ValueError('x must be non-negative') try: return x ** 0.5 except TypeError: print('x must be an int or float')
  • 19. Let's practice! PYTHON DATA SCIENCE TOOLBOX (PART 1)
  • 20. Congratulations! PYTHON DATA SCIENCE TOOLBOX (PART 1) Hugo Bowne-Anderson Instructor
  • 21. PYTHON DATA SCIENCE TOOLBOX (PART 1) What you’ve learned: Write functions that accept single and multiple arguments Write functions that return one or many values Use default, exible, and keyword arguments Global and local scope in functions Write lambda functions Handle errors
  • 22. PYTHON DATA SCIENCE TOOLBOX (PART 1) There’s more to learn! Create lists with list comprehensions Iterators - you’ve seen them before! Case studies to apply these techniques to Data Science
  • 23. Let's practice! PYTHON DATA SCIENCE TOOLBOX (PART 1)