Skip to main content

Posts

Reduce function of python

reduce function The reduce function in Python allows us to apply a specific operation to a sequence of elements and reduce them into a single value. It takes two parameters: a function and an iterable object, such as a list or tuple. Syntax reduce(function, iterable) Let's say you have a list of numbers and you want to find their sum. You can use the reduce function to achieve this in a concise manner. from functools import reduce numbers = [1, 2, 3, 4, 5] result = reduce(lambda x, y: x + y, numbers) print(result) Output 15 In this example, the reduce function takes a lambda function as the first argument, which adds two numbers together. It applies this lambda function cumulatively to the list of numbers, combining them into a single result. In the end, we get the sum of all the numbers. Let's consider another scenario. Suppose we have a list of numbers and we want to find the maximum value. The reduce function can help us achieve this with ease. from functools import reduce n...

Filter function of python

filter function:-  Imagine we have a bunch of things, like a list of numbers, a collection of fruits, or any other group of items. Now, let's say we want to pick out only certain items from that group based on a specific condition. This is where the filter function comes in handy. Let's say we have a list of numbers, and we want to filter out only the even numbers. We define a condition-checking function that checks if a number is even. Then, we pass this function and the list of numbers to the filter function. It goes through each number, applies the condition-checking function, and keeps only the numbers that are even. Finally, it gives us a new list containing only the even numbers. def is_even(num):     return num % 2 == 0 numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] filtered_numbers = list(filter(is_even, numbers)) print(filtered_numbers) Output [2, 4, 6, 8, 10] We can also utilize lambda expressions to define filtering conditions directly within the filter function. nu...

Map function of Python

map() function The map() function takes two arguments: a function and an iterable (such as a list, tuple, or string). It applies the function to each element of the iterable and returns a new iterable with the transformed values. Syntax map(function, iterable) Let's say we have a list of numbers, and we want to square each number in the list. Instead of writing a loop to iterate through the list and square each number, we can simply use the map() function to achieve the same result in a more concise way. numbers = [1, 2, 3, 4, 5] squared_numbers = list(map(lambda x: x**2, numbers)) print(squared_numbers) Output [1, 4, 9, 16, 25] Here, we passed a lambda function (a small anonymous function) as the first argument to map(). This lambda function takes a single argument x and returns the square of that number. The map() function applies this lambda function to each element in the numbers list and returns a new iterable, which we convert into a list using the list() function. Using the ...

Lambda Function of Python पायथन में लैम्ब्डा फ़ंक्शंस

Lambda functions, also known as anonymous functions that are one-line functions without a name. They are defined using the lambda keyword and are primarily used when a small function is required for a short period. Lambda functions can take any number of arguments but can only have a single expression. लैम्ब्डा फ़ंक्शंस, जिन्हें अनाम फ़ंक्शंस के रूप में भी जाना जाता है, जो बिना नाम के एक-पंक्ति फ़ंक्शंस हैं। उन्हें लैम्ब्डा कीवर्ड का उपयोग करके परिभाषित किया गया है और मुख्य रूप से तब उपयोग किया जाता है जब छोटी अवधि के लिए एक छोटे फ़ंक्शन की आवश्यकता होती है। लैम्ब्डा फ़ंक्शंस किसी भी संख्या में तर्क ले सकते हैं लेकिन केवल एक ही अभिव्यक्ति हो सकती है। Syntax:- lambda arguments: expression Example:- double = lambda x: x * 2 print(double(5))  #Output=10 sum = lambda a, b: a + b print(sum(3, 4))  #Output=7

Flow of execution of a python program पाइथन प्रोग्राम के निष्पादन का प्रवाह

Flow of execution represents a general outline of Python program execution. The specific flow can vary based on the program's structure, control flow statements (if, else, for, while), and function calls. निष्पादन का प्रवाह पायथन प्रोग्राम निष्पादन की एक सामान्य रूपरेखा का प्रतिनिधित्व करता है। विशिष्ट प्रवाह प्रोग्राम की संरचना, नियंत्रण प्रवाह विवरण (यदि, अन्यथा, के लिए, जबकि), और फ़ंक्शन कॉल के आधार पर भिन्न हो सकता है। 1.) Start: The program begins execution. 1.) प्रारंभ: प्रोग्राम का निष्पादन प्रारंभ होता है। 2.) Import Modules: If necessary, the program imports external modules or libraries. 2.) मॉड्यूल आयात करें: यदि आवश्यक हो, तो प्रोग्राम बाहरी मॉड्यूल या लाइब्रेरी आयात करता है। 3.) Define Functions: Any functions used in the program are defined. 3.) फंक्शन्स को परिभाषित करें: प्रोग्राम में उपयोग किए गए किसी भी फंक्शन को परिभाषित किया जाता है। 4.) Main Program: The main body of the program starts. 4.) मुख्य प्रोग्राम: प्रोग्राम का मुख्य भाग प्रारंभ होता है। 5.) Initializat...

Operator's in Python पायथन में ऑपरेटर्स

Python supports various types of operators for performing operations on variables and values. Here are some common types of operators in Python: पायथन वेरिएबल्स और मानों पर संचालन करने के लिए विभिन्न प्रकार के ऑपरेटरों का समर्थन करता है। यहां पायथन में कुछ सामान्य प्रकार के ऑपरेटर हैं: 1. Arithmetic Operators:    + # Addition     - # Subtraction    * # Multiplication    / # Division    % # Modulus (remainder)    ** # Exponentiation    // # Floor Division 2. Comparison Operators:        == # Equal to    != # Not equal to    < # Less than    > # Greater than    <= # Less than or equal to    >= # Greater than or equal to     3. Logical Operators:       and # Logical AND    or # Logical OR    not # Logical NOT    4. Assignment Operators:    ...

Statements in Python पायथन में कथन

In Python, statements are individual instructions that the interpreter can execute. Here are some common types of statements: पायथन में, कथन व्यक्तिगत निर्देश होते हैं जिन्हें दुभाषिया निष्पादित कर सकता है। यहां कुछ सामान्य प्रकार के कथन दिए गए हैं: 1. Assignment Statement:     x = 10 2. Conditional Statement (if-else):    if x > 5:        print("x is greater than 5")    else:        print("x is not greater than 5") 3. Looping Statements (for and while):    for i in range(5):        print(i)    while x > 0:        print(x)        x -= 1 4. Function Definition:    def greet(name):        print("Hello, " + name + "!") 5. Import Statement:     import math     These are just a few examples, and there are many other types of statements in Python. Each statement serves a specific purpose an...