0% found this document useful (0 votes)
11 views2 pages

Python Function Programs Federal Board XII

The document contains a collection of Python programs categorized into two parts: user-defined functions and built-in functions. Part A includes 20 user-defined functions for various operations such as addition, finding maximum/minimum, checking prime numbers, and more. Part B includes 5 programs that utilize built-in functions for string length, maximum/minimum of lists, sum, and sorting.

Uploaded by

bzeshankhan8
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)
11 views2 pages

Python Function Programs Federal Board XII

The document contains a collection of Python programs categorized into two parts: user-defined functions and built-in functions. Part A includes 20 user-defined functions for various operations such as addition, finding maximum/minimum, checking prime numbers, and more. Part B includes 5 programs that utilize built-in functions for string length, maximum/minimum of lists, sum, and sorting.

Uploaded by

bzeshankhan8
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 Programs (Federal Board XII) User-Defined

Functions & Built-in Functions


PART A: 20 PROGRAMS USING USER-DEFINED FUNCTIONS

1. Add two numbers


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

2. Find maximum of two numbers


def maximum(a, b):
if a > b:
return a
else:
return b

3. Check even or odd


def check_even(n):
if n % 2 == 0:
return "Even"
else:
return "Odd"

4. Square of a number
def square(n):
return n * n

5. Factorial of a number
def factorial(n):
f = 1
for i in range(1, n + 1):
f = f * i
return f

6. Check prime number


def is_prime(n):
if n <= 1:
return "Not Prime"
for i in range(2, n):
if n % i == 0:
return "Not Prime"
return "Prime"

7. Sum of two numbers


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

8. Area of rectangle
def area_rectangle(l, w):
return l * w

9. Simple Interest
def simple_interest(p, r, t):
return (p * r * t) / 100

10. Celsius to Fahrenheit


def celsius_to_fahrenheit(c):
return (c * 9 / 5) + 32

11. Length of string


def string_length(s):
return len(s)

12. Count vowels


def count_vowels(s):
count = 0
for ch in s:
if ch in "aeiouAEIOU":
count += 1
return count

13. Reverse string


def reverse_string(s):
return s[::-1]

14. Average of three numbers


def average(a, b, c):
return (a + b + c) / 3

15. Positive, Negative or Zero


def check_number(n):
if n > 0:
return "Positive"
elif n < 0:
return "Negative"
else:
return "Zero"

16. Power of number


def power(a, b):
return a ** b

17. Table of a number


def table(n):
for i in range(1, 11):
print(n, "x", i, "=", n * i)

18. Minimum of two numbers


def minimum(a, b):
if a < b:
return a
else:
return b

19. Sum of natural numbers


def sum_natural(n):
s = 0
for i in range(1, n + 1):
s += i
return s

20. Palindrome string


def palindrome(s):
if s == s[::-1]:
return "Palindrome"
else:
return "Not Palindrome"

PART B: 5 PROGRAMS USING BUILT-IN FUNCTIONS

1. Length of string
len(text)

2. Maximum of list
max(numbers)

3. Minimum of list
min(numbers)

4. Sum of list
sum(numbers)

5. Sort list
sorted(numbers)

You might also like