GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • w3resource
  • Front End
  • HTML
  • CSS
  • JavaScript
  • HTML5
  • Schema.org
  • php.js
  • Twitter Bootstrap
  • Responsive Web Design tutorial
  • Zurb Foundation 3 tutorials
  • Pure CSS
  • HTML5 Canvas
  • JavaScript Course

This view

  • Original page
  • New address

GeezerVison is showing www.w3resource.com — Python Functions Examples

  • Home
  • ▼Introduction to Python
  • Python Home
  • Python 2 vs 3
  • Python Installation
  • Python IDLE
  • ▼Python Basic
  • Python Syntax
  • Python Print Statement
  • Python Variable
  • Python Data Type
  • Python Operators
  • Python If elif else
  • Python For Loop
  • Python While Loop
  • Python break, continue
  • ▼Data Handling and Structures
  • Python String
  • Python Lists
  • Python Dictionary
  • Python Tuples
  • Python Sets
  • Python Bytes, Bytearray
  • Python Advanced Data Structure
  • ▼Date and Time
  • Python Date and Time
  • Python Calendar Module
  • ▼Functions and Modules
  • Python User define function
  • Python Built-in Functions
  • Python Module
  • Python Functions Examples
  • ▼File Handling and I/O
  • Python File Handling
  • ▼Object-Oriented Programming
  • Python Classes
  • ▼Advanced Programming Constructs
  • Python Iterators
  • Python Generators
  • Python Functional Programming
  • Python Multithearding
  • ▼Regular Expressions
  • Python Regular Expressions
  • ▼Error Handling and Debugging
  • Python Exception Handling
  • Python Testing and Debugging
  • ▼Database Interaction
  • Python SQlite Database
  • Python SQLalchemy ORM
  • ▼Advanced Python Topics
  • Advanced Python Topics
  • Python Standard Library
  • ▼Web Programming
  • CGI Programming
  • ▼Python Miscellaneous
  • Python-docx: Create and Manipulate
  • Software Frameworks
  • Floor Division in Python
  • Thonny Python IDE
  • pip install mysql-connector-python
  • Astral Python in 2026
  • PyCharm 2026 The Ultimate Guide
  • Python for Data Analysis in 2026
  • Self-Driving Python Server
  • Python-Native Hypermedia UI
  • Building Dashboards with Pythons
  • Building an Educational HTTP Server
  • Python-First Rapid App Generator
  • Markdown-Native Personal Site Framework
  • pyRAP Framework
  • Hypermedia-Driven Python Framework
  • Shinylives Reactive Framework
  • ASGIMachine: HTTP Decision Graph Framework
  • Embedded Web Framework for MicroPython
  • ..More to come..

Python Functions Examples

Last update on March 23 2024 07:19:36 (UTC/GMT +8 hours)

Basic Function Definition:

Here is a simple function called "greet" that takes one parameter name and returns a greeting message.

Code:

def greet(name):
    """This function greets the person passed in as an argument."""
    return f"Hello, {name}!"
# Example usage:
print(greet("Archana Minea")) 

Output:

Hello, Archana Minea!

Function with Default Argument:

Here, the function greet has a default argument name='Guest', so if no argument is provided, it will greet 'Guest'.

Code:

def greet(name='Guest'):
    """This function greets the person passed in as an argument, defaulting to 'Guest' if no argument is provided."""
    return f"Hello, {name}!"
# Example usage:
print(greet())

Output:

Hello, Guest!

Function with variable number of arguments:

Here in the *args parameter allows the function to accept any number of arguments, and sum(args) computes their sum.

Code:

def nums_sum(*args):
    """This function takes any number of arguments and returns their sum."""
    return sum(args)
# Example usage:
print(nums_sum(1,2))
print(nums_sum(1,2,-3,4,5))

Output:

3
9

Function with keyword arguments:

The **kwargs parameter allows passing additional keyword arguments, which are then printed alongside the name and age.

Code:

def person_info(name, age, **kwargs):
    """This function prints out the person's name, age, 
       and additional information passed as keyword arguments."""
    print(f"Name: {name}, Age: {age}")
    for key, value in kwargs.items():
        print(f"{key}: {value}")
# Example usage:
person_info("Eutychia Xenia", 28, City="New York", Occupation="Engineer")

Output:

Name: Eutychia Xenia, Age: 28
City: New York
Occupation: Engineer

Lambda Function:

This function defines a lambda function "add" which takes two arguments and returns their sum. Lambda functions are anonymous functions.

Code:

add = lambda x, y: x + y
# Example usage:
print(add(3,4))

Output:

7

Function as a First-Class Citizen:

Functions can be passed as arguments to other functions, allowing for dynamic behavior.

Code:

def apply_operation(x, y, operation):
    """This function applies the given operation to the given numbers."""
    return operation(x, y)
def add(a, b):
    return a + b
def subtract(a, b):
    return a - b
# Example usage:
print(apply_operation(5, 3, add))
print(apply_operation(5, 3, subtract))

Output:

8
2

Recursion:

Recursion is a technique where a function calls itself. Here, "factorial" calculates the factorial of a number recursively.

Code:

def factorial(n):
    """This function calculates the factorial of a number using recursion."""
    if n == 0:
        return 1
    else:
        return n * factorial(n-1)

# Example usage:
print(factorial(4))

Output:

24

Generator Function:

Generator functions use "yield" instead of "return" to return data incrementally, enabling efficient memory usage for large datasets.

Code:

def fibonacci(n):
    """This function generates Fibonacci sequence up to n."""
    a, b = 0, 1
    while a < n:
        yield a
        a, b = b, a + b

# Example usage:
fib_sequence = fibonacci(10)
print("Fibonacci sequence up to 10:")
for num in fib_sequence:
    print(num, end=" ")

Output:

Fibonacci sequence up to 10:
0 1 1 2 3 5 8

Function Annotations:

Function annotations provide metadata about the types used by the function parameters and return value.

Code:

def add(x: int, y: int) -> int:
    """This function adds two integers."""
    return x + y
# Example usage:
print(add(4,5))

Output:

9

Note:

Function annotations in Python are primarily for documentation purposes and do not enforce the types at runtime. If you want the function to strictly enforce integer inputs and output, you can add runtime type checking using isinstance() and raise an error if the input types are not integers:

Code:

def add(x: int, y: int) -> int:
    """This function adds two integers."""
    if not isinstance(x, int) or not isinstance(y, int):
        raise TypeError("Both inputs must be integers.")
    return x + y

# Example usage:
print(add(4, 5))  # Output: 9
print(add(4.2, 5.7))  # This will raise a TypeError

Output:

9
TypeError: Both inputs must be integers.

Decorator:

Decorators allow modifying the behavior of functions or methods. Here, debug is a decorator that prints the name of the called function.

Code:

def debug(func):
    def wrapper(*args, **kwargs):
        print(f"Calling function: {func.__name__}")
        return func(*args, **kwargs)
    return wrapper

@debug
def add(a, b):
    return a + b
# Example usage:
result = add(4, 5)
print("Result:", result)

Output:

Calling function: add
Result: 9

Function with Docstring:

Docstrings are used to provide documentation about the purpose and usage of a function. They are enclosed in triple quotes and are accessible via the __doc__ attribute of the function.

Code:

def square(x):
    """This function returns the square of a number."""
    return x ** 2

Nested Function:

Nested functions are functions defined within another function. They have access to variables from the enclosing scope.

Code:

def outer_function(x):
    """This is the outer function."""
    def inner_function(y):
        """This is the inner function."""
        return y * 2
    return inner_function(x)

# Example usage:
result = outer_function(5)
print(result)

Output:

10

Function with Unpacking:

Unpacking allows passing elements of a collection as individual arguments to a function, making the code more concise.

Code:

def print_person_info(name, age):
    """This function prints person's name and age."""
    print(f"Name: {name}, Age: {age}")
# Example usage:
person_info = ("Diindiisi", 28)
print_person_info(*person_info)

Output:

Name: Diindiisi, Age: 28

Function with Keyword-Only Arguments (PEP 3102):

Keyword-only arguments can only be passed using keyword syntax, improving the clarity of function calls.

Code:

def greet(*, name="Guest"):
    """This function greets the person with the provided name."""
    return f"Hello, {name}!"

# Example usage:
greeting = greet(name="Emanuel")
print(greeting)

Output:

Hello, Emanuel!


Follow us on Facebook and Twitter for latest update.


  • Daily Coding Challenges & Projects
  • Weekly Trends and Language Statistics



You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.