Download Python Functions Handwritten Notes Pdf In Hindi || Diplomawale

PYTHON HANDWRITTEN NOTES(Chapter 5: Python Function):



reload| diplomawale.in
Python Functions

Note: In Python, a function is defined using the del keyword.

Python functions are necessary for intermediate-level programming and are easy to define.

In Python, a function is a reusable block of code that performs a specific task or set of tasks. Functions are an essential concept in programming because they allow you to organize and modularize your code, making it more readable, maintainable, and efficient.

Function Definition: To create a function, you use the def keyword, followed by the function name, a pair of parentheses (), and a colon ':' to define the function. 

 EXAMPLE: 

         def greet(name):

            print(f" Hello, {name}!") 

USER-DEFINED FUNCTION:

A user-defined function in Python is a function that you create yourself to perform a specific task. Unlike built-in functions that are provided by the Python language (like print(), '  len() ', etc.), user-defined functions are designed by you to meet the requirements of your program.

EXAMPLE:

                                
def greet(name):
                                """ This function greets the person passed in as a parameter."""    
             print(f"Hello, {name}!")
                                # Calling the function
                                greet("GAURAV")


CALLING A FUNCTION:

Function with No Arguments:

If the function doesn't require any arguments, you simply write the function name followed by parentheses.


EXAMPLE:

                                    

def say_hello():

                                            print("Hello, world!")


                                    # Calling the function

                                    say_hello()


Function with Arguments:


If the function requires one or more arguments, you provide the necessary values inside the parentheses when calling the function.

diplomawale.inTO DOWNLOAD 


EXAMPLE:

                               

def greet(name):

                                   print(f"Hello, {name}!")


                              # Calling the function with an argument

                              greet("GAURAV")


Function with Return Value:


If the function returns a value using the return statement, you can capture and use that returned value.


EXAMPLE:

                          def add(a, b):

                                 result = a + b

                                 return result


                          # Calling the function and using the returned value

                          sum_result = add(3, 5)

                          print(sum_result) # Output: 8


Variable Number of Arguments:


Functions can accept a variable number of arguments using *args for positional arguments or **kwargs for keyword arguments.


EXAMPLE:

                         

def print_arguments(*args):

                               for arg in args:

                                     print(arg)


                        # Calling the function with variable arguments

                        print_arguments(1, 2, 3) # Output: 1 2 3


                        def print_keyword_arguments(**kwargs):

                                 for key, value in kwargs.items():

                                        print(f"{key}: {value}")


                       # Calling the function with keyword arguments

                      print_keyword_arguments(a=1, b=2, c=3)

                      # Output:

                      # a: 1

                      # b: 2

                      # c: 3

diplomawale.inTO DOWNLOAD 

ANONYMOUS FUNCTION:

In Python, an anonymous function is a function that is defined without a proper name. These types of functions are also referred to as "lambda functions." Anonymous functions are typically used for short, simple operations where a full function definition is unnecessary. They are created using the 'lambda' keyword.

SYNTAX:

                     lambda arguments: expression

EXAMPLE:

                          
  # Regular function to square a number
                            def square(x):
                                return x ** 2

                            # Equivalent lambda function to square a number
                            square_lambda = lambda x: x ** 2

                            print(square(4))          # Output: 16
                            print(square_lambda(4))   # Output: 16

                            # Lambda function to add two numbers
                            add = lambda a, b: a + b

                            print(add(3, 5))  # Output: 8

                            # Lambda functions as a key for sorting a list of tuples
                            points = [(3, 5), (1, 9), (8, 2)]
                            sorted_points = sorted(points, key=lambda point: point[1])

                            print(sorted_points)  # Output: [(8, 2), (3, 5), (1, 9)]

PYTHON BUILT-IN FUNCTION:

Numeric Functions:

  • abs(x): Returns the absolute value of x.
  • round(x): Rounds x to the nearest integer.

String Functions:

  • len(s): Returns the length of a string.
  • str(x): Converts x to a string representation.
  • upper(): Converts a string to uppercase.

Type Conversion Functions:

  • int(x): Converts x to an integer.
  • float(x): Converts x to a floating-point number.
  • str(x): Converts x to a string.

Container Functions:

  • list(iterable): Converts an iterable to a list.
  • tuple(iterable): Converts an iterable to a tuple.
  • set(iterable): Converts an iterable to a set.



Input/Output Functions:

  • print(*objects, sep=' ', end='\n'): Prints objects to the standard output.
  • input(prompt): Reads a line of text from the user.

Math Functions:

  • max(iterable): Returns the maximum value from an iterable.
  • min(iterable): Returns the minimum value from an iterable.
  • sum(iterable): Returns the sum of values in an iterable.

Iteration and Iterables:

  • range(start, stop[, step]): Generates a sequence of numbers.
  • enumerate(iterable): Generates index-value pairs from an iterable.

Boolean Functions:

  • bool(x): Converts x to a Boolean value.
  • all(iterable): Returns True if all elements of the iterable are True.
  • any(iterable): Returns True if any element of the iterable is True.

Sorting and Iteration:

  • sorted(iterable): Returns a sorted list from the elements of an iterable.
  • reversed(sequence): Returns a reverse iterator.

Post a Comment

0 Comments