samuel bajracharya
15 pages
samuel bajracharya
15 pages
227
/ 15
Exception Handling, Comprehensions and Decorators
in Python
Contents
1 Introduction 4
2 What is Exception? 4
3 Simple Example 4
4 Basic try-except 5
5 How try Works 5
6 Exception Object 5
7 Specific Exception Handling 5
8 Why Specific Exceptions are Better 6
9 Infinite Input Validation Loop 6
10 What is else in Exception Handling? 6
11 Program Flow 6
12 finally Block 7
13 Example 7
14 File Exception 7
15 Real-world Importance 7
16 Comprehensions 8
17 List Comprehension 8
18 Structure of List Comprehension 8
19 Filtering with Comprehension 8
1
20 Three Important Parts 8
21 Odd Numbers 9
22 Dictionary Comprehension 9
23 What Happened Here? 9
24 Conditional Expression in Comprehension 9
25 Decorator Introduction 9
26 Basic Decorator Example 10
27 What Happens Here? 10
28 @ Symbol 10
29 Decorator Flow 10
30 Execution Time Decorator 10
31 Using time Module 11
32 Calculating Duration 11
33 *args and **kwargs in Decorators 11
34 Example 11
35 Real-world Use of Decorators 11
36 Important Final Thought 12
37 Exercises 13
37.1 Exercise 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
37.2 Exercise 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
37.3 Exercise 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
37.4 Exercise 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
37.5 Exercise 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
37.6 Exercise 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
37.7 Exercise 7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
37.8 Exercise 8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
37.9 Exercise 9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
37.10Exercise 10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
38 Challenge Questions 15
38.1 Challenge 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
38.2 Challenge 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
38.3 Challenge 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
38.4 Challenge 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2
38.5 Challenge 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
38.6 Challenge 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
38.7 Challenge 7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
38.8 Challenge 8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
38.9 Challenge 9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
38.10Challenge 10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3
/ 15
End of Document
227