DEV Community

Cover image for Day 8 of My Data Analytics Journey
Ramya .C
Ramya .C

Posted on

Day 8 of My Data Analytics Journey

✨ Mastering Python Functions ✨

Today I explored Python Functions, an essential part of writing reusable, clean, and efficient code. Below are the key topics I learned with simple explanations and example programs.


What is a Function?

A function is a block of code that performs a specific task and runs only when it is called.

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

greet()
Enter fullscreen mode Exit fullscreen mode

Benefits of Using Functions

  • Increases code reusability
  • Makes the code modular and readable
  • Easier to debug and manage

Function Declaration and Syntax

def function_name(parameters):
    # block of code
    return result
Enter fullscreen mode Exit fullscreen mode

Example:

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

print(add(5, 3))  # Output: 8
Enter fullscreen mode Exit fullscreen mode

Function Keywords

  • def: Used to define a function
  • return: Sends back the result
  • *args: For variable number of positional arguments
  • **kwargs: For variable number of keyword arguments

Types of Functions

  1. Built-in Functions
print(len("Python"))  # Output: 6
Enter fullscreen mode Exit fullscreen mode
  1. User-defined Functions
def square(n):
    return n * n

print(square(4))  # Output: 16
Enter fullscreen mode Exit fullscreen mode

Types of Arguments

  1. Positional Arguments
def greet(name):
    print("Hello", name)

greet("Ramya")
Enter fullscreen mode Exit fullscreen mode
  1. Keyword Arguments
def student(name, age):
    print(name, age)

student(age=21, name="Ramya")
Enter fullscreen mode Exit fullscreen mode
  1. Default Arguments
def country(name="India"):
    print("Country:", name)

country()
country("USA")
Enter fullscreen mode Exit fullscreen mode
  1. *Arbitrary Arguments (*args)*
def total(*numbers):
    print(sum(numbers))

total(1, 2, 3, 4)
Enter fullscreen mode Exit fullscreen mode
  1. **Arbitrary Keyword Arguments (kwargs)
def info(**data):
    for key, value in data.items():
        print(key, ":", value)

info(name="Ramya", age=21)
Enter fullscreen mode Exit fullscreen mode

Docstring (Documentation String)

Used to describe what a function does.

def add(a, b):
    """This function adds two numbers."""
    return a + b

print(add.__doc__)
Enter fullscreen mode Exit fullscreen mode

Anonymous Functions (Lambda)

square = lambda x: x * x
print(square(5))  # Output: 25
Enter fullscreen mode Exit fullscreen mode

Function with Default Arguments

def greet(name="Guest"):
    print("Hello", name)

greet()
greet("Ramya")
Enter fullscreen mode Exit fullscreen mode

Today’s Tasks

1. Find Minimum and Maximum in a List

numbers = [10, 4, 25, 1, 99]

def find_min_max(nums):
    return min(nums), max(nums)

minimum, maximum = find_min_max(numbers)
print("Min:", minimum)
print("Max:", maximum)
Enter fullscreen mode Exit fullscreen mode

2. Check if a Number is Prime


python
def is_prime(n):
    if n <= 1:
        return False
    for i in range(2, int(n**0.5)+1):
        if n % i == 0:
            return False
    return True

print(is_prime(7))  # Output: True
print(is_prime(10)) # Output: 
False
Enter fullscreen mode Exit fullscreen mode

Top comments (0)