Python Functions Exercises
1. Maximum of Three Numbers
Write a Python function to find the maximum of three numbers.
2. Sum All Numbers in a List
Write a Python function to sum all the numbers in a list.
- Sample List: (8, 2, 3, 0, 7)
- Expected Output: 20
3. Multiply All Numbers in a List
Write a Python function to multiply all the numbers in a list.
- Sample List: (8, 2, 3, -1, 7)
- Expected Output: -336
4. Reverse a String
Write a Python program to reverse a string.
- Sample String: “1234abcd”
- Expected Output: “dcba4321”
5. Factorial of a Number
Write a Python function to calculate the factorial of a number (a non-negative integer). The function accepts the number as an argument.
6. Check if a Number Falls Within a Given Range
Write a Python function to check whether a number falls within a given range.
7. Count Uppercase and Lowercase Letters in a String
Write a Python function that accepts a string and counts the number of upper and lower case letters.
- Sample String: ‘The quick Brow Fox’
- Expected Output:
- No. of Upper case characters : 3
- No. of Lower case Characters : 12
8. Return a New List with Distinct Elements from a List
Write a Python function that takes a list and returns a new list with distinct elements from the first list.
- Sample List: [1, 2, 3, 3, 3, 3, 4, 5]
- Unique List: [1, 2, 3, 4, 5]
9. Check Whether a Number is Prime
Write a Python function that takes a number as a parameter and checks whether the number is prime or not.
- Note: A prime number (or a prime) is a natural number greater than 1 and that has no positive divisors other than 1 and itself.
10. Print Even Numbers from a Given List
Write a Python program to print the even numbers from a given list.
- Sample List: [1, 2, 3, 4, 5, 6, 7, 8, 9]
- Expected Result: [2, 4, 6, 8]
11. Etgar Check if a Number is Perfect
Write a Python function to check whether a number is “Perfect” or not.
- Definition: A perfect number is a positive integer that is equal to the sum of its proper positive divisors (excluding the number itself).
- Example: 6 (1 + 2 + 3 = 6) and 28 (1 + 2 + 4 + 7 + 14 = 28).
12. Check if a String is a Palindrome
Write a Python function that checks whether a passed string is a palindrome or not.
- Note: A palindrome reads the same backward as forward (e.g., “madam”).
13. Check if a String is a Pangram
Write a Python function to check whether a string is a pangram or not.
- Note: A pangram contains every letter of the alphabet at least once (e.g., “The quick brown fox jumps over the lazy dog”).