Python Functions
Python Functions
Python
Functions
UNIT-4: Programming
What is a Function?
A function is a reusable block of code that performs a specific task. Functions
help organize code, make it more readable, and allow for code reusability.
2
UNIT-4: Programming
3
UNIT-4: Programming
A Simple Function
Explain a basic function that takes no arguments and returns no value.
4
UNIT-4: Programming
5
UNIT-4: Programming
6
UNIT-4: Programming
Default Parameters
Sometimes, we can set default values for parameters.
7
UNIT-4: Programming
Keyword Arguments
Demonstrate the use of keyword arguments for better readability
8
UNIT-4: Programming
Variable-Length Arguments
A function can accept a variable number of arguments using *args and **kwargs
9
UNIT-4: Programming
Nested Functions
Functions can be defined within other functions.
10
UNIT-4: Programming
Lambda Functions
Introduce anonymous (lambda) functions for quick one-liners.
11
UNIT-4: Programming
12
UNIT-4: Programming
Tasks page 1
1. Greet a User: Write a function greet_user(name) that takes a name as input and prints a greeting
message.
2. Square of a Number: Write a function square(num) that takes a number and returns its square.
3. Even or Odd: Write a function is_even(num) that takes a number and returns True if it's even and
False otherwise.
4. Find the Maximum: Write a function find_max(a, b, c) that takes three numbers and returns the
largest one.
5. Simple Calculator: Write a function calculate(a, b, operator) that takes two numbers and an
operator (+, -, *, /) and returns the result.
6. Convert Temperature: Write a function celsius_to_fahrenheit(celsius) to convert Celsius to
Fahrenheit.
7. Check Palindrome: Write a function is_palindrome(word) that checks if a given word is a
palindrome (e.g., "madam").
8. Factorial: Write a function factorial(n) that calculates the factorial of a number.
9. Count Vowels: Write a function count_vowels(string) to count the number of vowels in a given
string.
10. Reverse a String: Write a function reverse_string(string) to reverse a given string.
11. Sum of List: Write a function sum_of_list(numbers) that takes a list of numbers and returns the
sum. 13
UNIT-4: Programming
Tasks page 2
1. Find a Word: Write a function find_word(sentence, word) that checks if a word exists in a
sentence.
2. Default Greeting: Write a function welcome(name="Guest") that prints a default greeting if no
name is provided.
3. Check Prime: Write a function is_prime(num) that checks if a number is prime.
4. Simple Interest: Write a function calculate_interest(principal, rate, time) to calculate simple
interest.
5. Table of a Number: Write a function print_table(num) that prints the multiplication table of a
number.
6. Count Words: Write a function count_words(sentence) to count the number of words in a
sentence.
7. Find Length: Write a function find_length(data) to find the length of a string, list, or tuple.
8. Create Acronym: Write a function create_acronym(phrase) that converts a phrase into its acronym
(e.g., "World Health Organization" → "WHO").
9. Password Validator: Write a function validate_password(password) that checks if a password
meets the following criteria:
At least 8 characters
Contains at least one digit
Contains at least one special character (!@#$%^&*). 14
UNIT-4: Programming
Algorithmic Tasks
1. Fibonacci Sequence: Write a function fibonacci(n) to return the first n numbers of the Fibonacci
sequence.
2. Find GCD: Write a function gcd(a, b) that finds the greatest common divisor (GCD) of two numbers.
3. Binary Search: Write a function binary_search(arr, target) that implements the binary search
algorithm to find the index of a target in a sorted list.
4. Remove Duplicates: Write a function remove_duplicates(numbers) that removes duplicate elements
from a list.
5. Sort a List: Write a function bubble_sort(numbers) that sorts a list of numbers using the bubble sort
algorithm.
6. Number to Words: Write a function num_to_words(num) to convert a number to its word
representation (e.g., 123 → "one hundred twenty-three").
15