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

004.python Programming Day04

The document provides a collection of Python functions, each with a specific purpose such as greeting, arithmetic operations, and data manipulation. Each function is accompanied by a brief explanation of its functionality. Examples of function calls demonstrate how to use these functions effectively.

Uploaded by

transdaniel123
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

004.python Programming Day04

The document provides a collection of Python functions, each with a specific purpose such as greeting, arithmetic operations, and data manipulation. Each function is accompanied by a brief explanation of its functionality. Examples of function calls demonstrate how to use these functions effectively.

Uploaded by

transdaniel123
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Function

1. Greet Function
def greet(name):
print("Hello", name)
greet("Lahiru")

Explanation: Takes a name as input and prints a greeting.

2. Add Two Numbers


def add(a, b):
return a + b
print(add(5, 3))

Explanation: Uses return to send the sum of two numbers back to where it’s called.

3. Subtract Two Numbers


def subtract(a, b):
return a - b
print(subtract(10, 4))

Explanation: Returns the difference between two numbers.

4. Find Square
def square(num):
return num * num
print(square(6))

Explanation: Multiplies a number by itself.

5. Calculate Area of Rectangle


def area_rectangle(length, width):
return length * width
print(area_rectangle(5, 3))

Explanation: Area = length × width.

6. Find Even or Odd


def check_even(num):
if num % 2 == 0:
print("Even number")
else:
print("Odd number")
check_even(7)

Explanation: Uses modulus operator % to check remainder when divided by 2.


7. Convert Celsius to Fahrenheit
def celsius_to_fahrenheit(c):
return (c * 9/5) + 32
print(celsius_to_fahrenheit(0))

Explanation: Applies the temperature conversion formula.

8. Find Maximum of Two Numbers


def find_max(a, b):
if a > b:
return a
else:
return b
print(find_max(10, 25))

Explanation: Compares two numbers and returns the larger one.

9. Count Letters in a Word


def count_letters(word):
return len(word)
print(count_letters("Python"))

Explanation: Uses built-in len() to count characters.

10. Calculate Average


def average(a, b, c):
return (a + b + c) / 3
print(average(5, 10, 15))

Explanation: Adds three numbers and divides by 3.

11. Display a Line


def line():
print("------------------")
line()

Explanation: No parameters — just prints a separator line.

12. Multiply Numbers


def multiply(a, b):
return a * b
print(multiply(6, 7))

Explanation: Returns the product of two numbers.


13. Check if a Number is Positive or Negative
def check_sign(num):
if num > 0:
print("Positive")
elif num < 0:
print("Negative")
else:
print("Zero")
check_sign(-5)

Explanation: Uses conditional statements to check sign.

14. Find the Largest Number in a List


def find_largest(numbers):
return max(numbers)
print(find_largest([4, 9, 2, 11, 7]))

Explanation: Uses built-in max() to find the biggest number.

15. Sum of a List


def sum_list(numbers):
return sum(numbers)
print(sum_list([2, 4, 6, 8]))

Explanation: Adds all elements in a list using sum().

16. Find Minimum of Three Numbers


def smallest(a, b, c):
return min(a, b, c)
print(smallest(5, 2, 9))

Explanation: Uses min() to find the smallest among three.

17. Simple Calculator


def calculator(a, b, op):
if op == '+':
return a + b
elif op == '-':
return a - b
elif op == '*':
return a * b
elif op == '/':
return a / b
else:
return "Invalid operator"
print(calculator(10, 5, '*'))

Explanation: Performs basic arithmetic depending on the operator symbol.

You might also like