Fundamentals of python in a nutshell by Pancherpula Kalyan varma & Linga Sai chandana & Menghwani Jatin & Kyatam Harshavardhini

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



Copyright Disclaimer:
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.