Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Python Functions Lambda Skillzam (1)

The document explains user-defined functions in Python, detailing various types including functions without parameters, functions with parameters, and functions with return values. It also covers default parameters, variable-length arguments, and keyword arguments. Additionally, it introduces lambda functions as concise, unnamed functions for simple operations.

Uploaded by

Sami Sanadi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Python Functions Lambda Skillzam (1)

The document explains user-defined functions in Python, detailing various types including functions without parameters, functions with parameters, and functions with return values. It also covers default parameters, variable-length arguments, and keyword arguments. Additionally, it introduces lambda functions as concise, unnamed functions for simple operations.

Uploaded by

Sami Sanadi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Python Functions and Lambda Functions

User-Defined Functions in Python

A user-defined function is a function that you create to perform a specific task.


Instead of writing the same code again and again, you can put it inside a function and use it
whenever needed!

Defining a Function:
def greet():
print("Hello, welcome to Python!")

greet() # Output: Hello, welcome to Python!

1. Function without Parameters & No Return Value


def say_hello():
print("Hello, world!")

say_hello() # Output: Hello, world!

2. Function with Parameters & No Return Value


def greet(name):
print(f"Hello, {name}!")

greet("Alice") # Output: Hello, Alice!

3. Function with Parameters & Return Value


def add(a, b):
return a + b

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

4. Function Without Parameters & With Return Value


def get_pi():

Skillzam
Python Functions and Lambda Functions

return 3.1416

pi_value = get_pi()
print(pi_value) # Output: 3.1416

5. Function with Default Parameters


def greet(name="Guest"):
print(f"Hello, {name}!")

greet() # Output: Hello, Guest!


greet("John") # Output: Hello, John!

6. Function with Variable-Length Arguments (*args)


def add_numbers(*numbers):
return sum(numbers)

print(add_numbers(1, 2, 3, 4, 5)) # Output: 15

7. Function with Multiple Keyword Arguments (**kwargs)


def print_info(**info):
for key, value in info.items():
print(f"{key}: {value}")

print_info(name="Alice", age=25, city="New York")

Skillzam
Python Functions and Lambda Functions

Lambda Functions in Python

Lambda functions are tiny one-line functions in Python that don't need a name.
They are useful for short, simple operations.

Example 1: Adding Two Numbers


add = lambda x, y: x + y
print(add(3, 5)) # Output: 8

Example 2: Checking If a Number is Even


is_even = lambda n: n % 2 == 0
print(is_even(4)) # Output: True
print(is_even(7)) # Output: False

Skillzam

You might also like