Thursday, September 19, 2024

Input and Output statements in Python

In Python, input and output are handled using built-in functions input() and print().
पायथन में, इनपुट और आउटपुट को अंतर्निहित फ़ंक्शन इनपुट() और प्रिंट() का उपयोग करके नियंत्रित किया जाता है।

1.) input():-
To take input from the user, we can use the `input()` function. This function reads a line usually from a standard input device (keyboard) and returns it as a string to a variable. By default, `input()` returns data as a string, so if we need an integer or a float, we must convert it by type conversion.
1.) इनपुट():-
उपयोगकर्ता से इनपुट लेने के लिए, हम `इनपुट()` फ़ंक्शन का उपयोग कर सकते हैं। यह फ़ंक्शन आमतौर पर एक मानक इनपुट डिवाइस (कीबोर्ड) से एक लाइन पढ़ता है और इसे एक स्ट्रिंग के रूप में एक वेरिएबल में लौटाता है। डिफ़ॉल्ट रूप से, `इनपुट()` डेटा को एक स्ट्रिंग के रूप में लौटाता है, इसलिए यदि हमें इंटीजर या फ्लोट की आवश्यकता है, तो हमें इसे टाईप कन्वर्जन द्वारा परिवर्तित करना होगा।

2.) print():-
To display output to the user, we can use the `print()` function.
2.) प्रिंट():- 
उपयोगकर्ता को आउटपुट प्रदर्शित करने के लिए, हम `प्रिंट()` फ़ंक्शन का उपयोग कर सकते हैं। 

Example 1:
#By default string input:-
name = input("Enter your name: ") #name=Rahul
print("Hello, " + name) #Hello, Rahul

Example 2:
#integer input:-
num = int(input("Enter a number: ")) #num= 100
print("You entered:", num)
#You entered: 100

Example 3:
#floating point input:-
pi = float(input("Enter a value of PI: ")) #pi=3.14
print("You entered:", pi)
#You entered: 3.14
 

Tuesday, February 27, 2024

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
numbers = [7, 2, 9, 1, 5]
result = reduce(lambda x, y: x if x > y else y, numbers)
print(result)

Output
9

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.

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
filtered_numbers = list(filter(lambda x: x % 2 == 0, numbers))

print(filtered_numbers)
We will get the same output as earlier.
[2, 4, 6, 8, 10]

Similarly, we can use the filter function with dictionaries or other iterable objects. Just provide a condition-checking function that examines each item and decide whether to keep it or discard it.
By using the filter function, we can easily sort through your data and get a new set of items that meet our specific criteria. It saves you time and makes our code more readable and efficient.


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 map() function, we can perform a wide range of operations on each element of an iterable. It can be used with built-in functions like len(), str(), int(), as well as user-defined functions.

Benefits of using the map() function

We can eliminate the need for explicit loops, resulting in shorter and more readable code.
The map() function performs operations on each element of an iterable in parallel, making it more efficient than using a traditional for loop.
Code reusability: The map() function allows us to pass any function as an argument, making our code more flexible and reusable.


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

Monday, February 26, 2024

Flow of execution in Python

In Python, the flow of execution refers to the order in which statements and expressions are executed in a program. Understanding the flow of execution is crucial for writing effective and readable Python code. Here's a general overview:

1. Sequential Execution:- 
Python programs are executed sequentially, meaning that each statement is executed one after the other in the order they appear in the code.

2. Conditional Execution (if-else):-
Conditional statements, such as `if`, `elif`, and `else`, allow for the execution of different blocks of code based on certain conditions.

   if condition:
       # Code block executed 
       # if the condition is True
   elif another_condition:
       # Code block executed 
       # if the previous condition is False and 
       # this one is True
   else:
       # Code block executed 
       # if none of the above conditions are True

3. Looping (for and while):- Loops, like `for` and `while`, enable repeated execution of a block of code.

   for item in iterable:
       # Code block executed 
       # for each item in the iterable

   while condition:
       # Code block executed repeatedly
       # as long as the condition is True

4. Function Calls:- Functions can be defined and called to encapsulate and reuse blocks of code.
   def my_function():
       # Code block inside the function

   my_function()  
      # Calling the function
   

5. Exception Handling (try-except):- Exception handling allows you to handle errors gracefully.
   try:
       # Code block where an exception might occur.
   except SomeException:
       # Code block executed 
       # if the specified exception occurs
   else:
       # Code block executed 
       # if no exception occurs in the try block
   finally:
       # Code block always executed
       # regardless of whether an exception occurred.

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:
  
   = # Assignment
   += # Add and assign
   -= # Subtract and assign
   *= # Multiply and assign
   /= # Divide and assign
   
5. Identity Operators:
   
   is # Returns True if both variables are the same object
   is not # Returns True if both variables are not the same object
   

6. Membership Operators:
   
   in # Returns True if a value is present in a sequence
   not in # Returns True if a value is not present in a sequence
   
These operators allow you to perform a wide range of operations in Python. 
ये ऑपरेटर आपको पायथन में कई प्रकार के ऑपरेशन करने की अनुमति देते हैं।

In Python, operators have different precedence levels, which determine the order in which operations are performed in an expression. Understanding operator precedence helps in avoiding ambiguity and clarifying the order of operations in complex expressions. Here's a general overview of operator precedence, from highest to lowest:
पायथन में, ऑपरेटरों के पास अलग-अलग प्राथमिकता स्तर होते हैं, जो किसी अभिव्यक्ति में संचालन के क्रम को निर्धारित करते हैं। ऑपरेटर प्राथमिकता को समझने से अस्पष्टता से बचने और जटिल अभिव्यक्तियों में संचालन के क्रम को स्पष्ट करने में मदद मिलती है। यहां उच्चतम से निम्नतम तक ऑपरेटर प्राथमिकता का सामान्य अवलोकन दिया गया है:

1. Parentheses ( )

2. Exponentiation **

3. Unary Operators: unary plus +x , unary minus -x , bitwise NOT ~x

4. Multiplication *, Division /, Modulus %

5. Addition + , Subtraction -

6. Bitwise Left Shift Operator<< , Bitwise Right Shift Operator >> 

7. Bitwise AND &

8. Bitwise XOR ^

9. Bitwise OR |

10. Comparison Operators
    == , !=, <, >, <=, >=

11. Logical NOT not

12. Logical AND and

13. Logical OR or

14. Conditional Expression (Ternary Operator)
    x if condition else y

15. Assignment Operators
    =, +=, -=, *=, /=, etc

Input and Output statements in Python

In Python, input and output are handled using built-in functions input() and print(). पायथन में, इनपुट और आउटपुट को अंतर्निहित फ़ंक्शन इनपुट...