[go: up one dir, main page]

0% found this document useful (0 votes)
5 views3 pages

Functions in Python

The document explains the concept of functions in programming, including their definition, creation using the 'def' keyword, and how to call them. It outlines different types of functions such as built-in, user-defined, lambda, and recursive functions, along with various types of parameters like positional, default, keyword, and variable-length parameters. Examples are provided for each type to illustrate their usage and functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Functions in Python

The document explains the concept of functions in programming, including their definition, creation using the 'def' keyword, and how to call them. It outlines different types of functions such as built-in, user-defined, lambda, and recursive functions, along with various types of parameters like positional, default, keyword, and variable-length parameters. Examples are provided for each type to illustrate their usage and functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

Define function

It is a reusable block of code and it is performs specific task.

2.creating function

We create function by using def keyword .

def function_name():

#block of code

3.call the function

def greetings():

print("hello")

greetings()

4.Types of functions and definations

1)Built in function:it is an already created function

2) User defined function: it is created by users or programmers

Example: def

3)Lambda function

4)Recursive function

5.Types of parameters write definations and examples

1)positional parameters.

2)default parameter.

3)keyword parameter.

4)variable length positional parameter.

5)variable length keyword or arguments.


1)positional parameters: In positional parameter order must should be follow

Example:

def greetings(name, age):

print(name, age)

greetings("sanghana", 24)

output: sanghana 24

2)Default parameter: if default value is assigned no need to follow order

Example:

def greetings(name, age=18):

print(f"{name}, {age}")

greetings("Sanghana")

3) keyword parameter: Directly pass the values in function call with specific parameter name

Example:

def greetings(name, age):

print(f"{name}, {age}")

greetings(age=21, name="Sanghana")

Output: Sanghana, 21---order doesn’t matter

4)variable length positional parameters: it accepts any number of values or arguments (use
single * to store values) it stores in tuple

Example:

def numbers(*args):

print(args)
numbers(1,2,3,4,5)

output:(1,2,3,4,5)

5) variable length keyword or arguments : it is a keyword argument accepts any number of


keywords, using double( **), it stores in dictionary.

Example:

def multiple_values(**kwargs):

print(kwargs)

multiple_values(name="sanghana",age="24",course="python")

output: {'name': 'sanghana', 'age': '24', 'course': 'python'}

You might also like