[go: up one dir, main page]

0% found this document useful (0 votes)
2 views13 pages

Advanced Conditionl Statement Pyhton

The document discusses advanced conditional statements in Python, focusing on list and dictionary comprehensions, lambda functions, and functional programming tools like filter, reduce, and map. It provides explanations, basic structures, and examples for each concept, emphasizing their advantages in writing cleaner and more efficient code. Additionally, it includes practice questions to reinforce the concepts covered.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views13 pages

Advanced Conditionl Statement Pyhton

The document discusses advanced conditional statements in Python, focusing on list and dictionary comprehensions, lambda functions, and functional programming tools like filter, reduce, and map. It provides explanations, basic structures, and examples for each concept, emphasizing their advantages in writing cleaner and more efficient code. Additionally, it includes practice questions to reinforce the concepts covered.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Advanced Condi onal Statements

1. List comprehension

2. Python Dic onary Comprehension

3. Lambda Operator

4. Filter

5. Reduce

6. Map

7. Recursive Func on

List Comprehension in Python


List comprehension is a simple and elegant way to create lists in Python. It helps write cleaner and
more readable code compared to tradi onal loops.

Basic Structure:

 Expression: Defines what to do with each item.

 Item: The variable represen ng each element in the iterable.

 Iterable: A collec on of values (like a list, range, etc.).

 Condi on (Op onal): Filters items based on a given condi on.


Examples:

1. Creating a List of Squares

 Using a loop:

Output:-

 Use list comprehension


Output of list Comprehension

Why Use List Comprehension?

 Shorter and cleaner code


 Improves readability
 Runs faster than tradi onal loops in most cases

Dic onary Comprehension in Python


Like list comprehension, Python also allows dic onary comprehensions. This
provides a simple way to create dic onaries using expressions.

Basic Structure:

 Key: The key for the dic onary.

 Value: The value corresponding to each key.

 Iterable: A collec on of values (like lists, ranges, or dic onaries).

Examples:

1. Creating a Dictionary from Two Lists

Using a loop:
Output :-

With dictionary comprehension:

Output :-
2. Crea ng a Dic onary with Squared Values

3. Using Condi onal Statements in Dic onary Comprehension

Output :-

Lambda Operator in Python


The lambda operator in Python is a way to create small, anonymous func ons in a
concise manner. These func ons can have mul ple arguments but only contain a single expression.

Basic Structure:
 lambda: Keyword to define the func on.

 Arguments: Input parameters similar to a normal func on.

 Expression: The opera on or computa on that returns a result.

Regular Func on Code

Lambda Func on Code

Why Use Lambda Func ons?


 Concise and efficient for simple opera ons
 O en used in func onal programming
 Works well with built-in func ons like map(), filter(), and sorted()

Lambda Func on with filter func on.

Lambda Func on with sorted func on.

Limitations of Lambda Functions

 Cannot contain multiple statements


 Limited readability for complex logic
 Less flexible compared to regular functions

Filter Function in Python


The filter() function is used to filter elements from an iterable based on a condition.
It returns a new iterator with only the elements that satisfy the condition.

Basic Syntax:
 Func on: A func on that returns True or False for each element.

 Iterable: A sequence (like a list, tuple, or set) to filter.

Example 1: Filtering Even Numbers

Example 2: Filtering Words with More Than 3 Le ers

You can also try with other func ons like map(),sorted()

Reduce Function in Python


The reduce() func on is used to apply a func on cumula vely to the elements of an
iterable, reducing it to a single value
The reduce() func on is part of the functools module and needs to be imported
before use.

Basic Syntax:

 Func on: A func on that takes two arguments and reduces the iterable to a single value.

 Iterable: A sequence (like a list or tuple) to apply the func on on.

Example 1: Summing a List of Numbers

Example 2: Finding the Maximum Value in a List


Map Func on in Python
The map() func on is used to apply a func on to each item in an
iterable (like a list or tuple) and returns a new iterable (map object)

Basic Syntax:

 func on: A func on that will be applied to each element.


 iterable: A sequence (like a list, tuple) whose elements will be processed.

Example 1: Squaring Numbers


Example 2: Conver ng Strings to Uppercase

Recursive Func on in Python

A recursive func on is a func on that calls itself to solve a


problem in smaller subproblems. Recursion is useful for tasks like
factorial computa on, Fibonacci sequence, and tree traversals.
Basic Syntax:

 Base Condi on: A stopping condi on to prevent infinite recursion.


 Recursive Call: The func on calls itself with modified parameters.

Example 1: Factorial Calcula on


Pra ce Ques on
For quick revision Implement this Python program that does the following:

1. Use list comprehension to generate a list of numbers from 1 to 20 that are divisible
by 4.
2. Create a dic onary comprehension where keys are numbers from 1 to 5, and values
are their squares.
3. Write a lambda func on to check if a given number is even.
4. Use map() with a lambda func on to double each number in a given list [2, 4, 6, 8].
5. Use filter() with a lambda func on to extract words star ng with 'b' from the list
["apple", "banana", "berry", "grape"].
6. Use reduce() to find the sum or product of all elements in the list [1, 2, 3, 4, 5].
7. Implement a recursive func on to calculate the factorial of a number.

Bonus:
Modify the recursive func on to compute the Fibonacci sequence for n terms.

You might also like