Fundamentals of python in a nutshell by Pancherpula Kalyan varma & Linga Sai chandana & Menghwani Jatin & Kyatam Harshavardhini
Author:Pancherpula, Kalyan varma & Linga, Sai chandana & Menghwani, Jatin & Kyatam, Harshavardhini
Language: eng
Format: epub
Publisher: UNKNOWN
Published: 2023-10-29T00:00:00+00:00
Clearly, as we can see, we did not define any value for âaâ, hence it is an error. So instead of terminating the program, the except block is executed printing âException Occurredâ.
If we do not write the âexceptâ block, an error occurs in the program, as shown below:
Example:
Output:
(iii) Finally Clause
Even if an error occurs in the âtryâ and âexceptâ block, the âfinallyâ block is never affected as it is independent of these. As an example, see the subsequent code:
Example:
Output:
(iv) Standard Exceptions
(v) Custom Exceptions:
The âtryâ block should include the âraiseâ keyword in order to execute any custom exception.
We can raise any exception of our own choice by creating a âclassâ using CustomException. You might get confused, does python supports to use of âclassâ? Even I was. Letâs find out how.
Example:
Output:
2.3 FUNCTIONAL PROGRAMMING
As supported by other programming languages, Python also includes many predefined functions which can be used by us by simply calling them. This can reduce our task of coding again and again and help to code in an efficient way.
Functional programming is a programming model that uses functions to define computations. The unchangeable state of functional programming is the major key. The computations are done through statements in imperative programming.
We have three main functions:
1. map()
2. filter()
3. reduce()
1. map() function
The map() function is a higher order function, which means that this function accepts another function and sequence of âiterablesâ as parameters. After each iterable is applied in the function in the sequence, the output is generated. The syntax is shown below:
Syntax:
The function is used to define an expression which is then applied to the âiterablesâ. We can use both user-defined functions and lambda functions to do the mapping.
Example:
Output:
2. The filter() function
As the name itself suggests, the filter() function does the filtering and generates the list of values that return true when the function is called as an output. The syntax is shown below:
Syntax:
This function is also similar to map(), as it can take both user-defined functions and lambda functions as parameters.
Example:
Output:
3. The reduce() function
The reduce() function reduces the âiterablesâ to a single value as shown below:
Syntax:
We cannot simply use the reduce() function directly, we need to import it from the âfunctoolsâ module. See the below example:
Example:
Output:
4. Lambda Functions
The lambda function is a popular yet another anonymous function. It generally has many arguments but just one expression. See the subsequent syntax for a better understanding:
Syntax:
Example:
Output:
Example:
Output: Use of Lambda Function
The lambda function unveils into charge when it loops directly, as an anonymous function inside another function.
Here we define a function that considers one single argument, i.e., ânâ, multiplies it with an anonymous number as shown below and is produced as output:
Can you guess what is happening in the below example?
Example:
Output:
(i.e.,) it is simply acting as a recursive function, which adds 10 each time for 12 times!
Isnât it exciting on using the lambda function?
2.4 List Comprehensions
Writing larger syntaxes to make even a small list can be inefficient and time-consuming. Hence to beat this issue python uses list comprehension. It includes brackets holding the expression that needs to be executed for every element included in the loop.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Fundamentals of python in a nutshell by Pancherpula Kalyan varma & Linga Sai chandana & Menghwani Jatin & Kyatam Harshavardhini(50)
Microsoft Dynamics CRM 4 for Dummies by Joel Scott & David Lee & Scott Weiss(49)
Computer Networks - A Top-Down Approach by Firouz Mosharraf Behrouz A Forouzan(28)
Introducing Blockchain with SAP Leonardo by Unknown(28)
Practical IT Service Management by BS Thejendra;(24)
Integrated Measurement - KPIs and Metrics for ITSM : A Narrative Account by Daniel McLean(21)
Magnetic exchange behavior of a long-range CuII system through the diamagnetic CuI2 unit bridge: A DFT study by You-Min Sun & Li-Li Wang & Yong-Shan Ma & Cheng-Bu Liu & Zhao-Jie Cui(18)
Connecting Discrete Mathematics and Computer Science (Instructor Solution Manual, Solutions) by David Liben-Nowell(17)
Mathematical Pictures at a Data Science Exhibition by Simon Foucart(14)
SM1002207 Chapter-4 Coding and Decoding by Unknown(13)
Interaction of Imidazole Containing Hydroxamic Acids with Fe(III): Hydroxamate Versus Imidazole Coordination of the Ligands by Etelka Farkas et al(7)
