Python Exercises-3
1. Area of a circle is calculated as follows: area = π x r x r and perimeter = 2 x π x r. Write a
function that calculates area_of_circle and perimeter_of_circle.
2. Write a function called add_all_nums which takes arbitrary number of arguments and sums
all the arguments. Check if all the list items are number types. If not, provide reasonable
feedback.
3. Temperature in °C can be converted to °F using this formula: °F = (°C x 9/5) + 32. Write a
function which converts °C to °F, convert_celsius_2_fahrenheit.
4. Write a function called check_season, it takes a month parameter and returns the season:
Autumn, Winter, Spring, or Summer.
5. Write a function called calculate_slope which returns the slope of a linear equation.
6. Quadratic equation is calculated as follows: ax² + bx + c = 0. Write a function which
calculates the solution set of a quadratic equation, solve_quadratic_eqn.
7. Declare a function named print_list. It takes a list as a parameter and prints out each
element of the list.
8. Declare a function named reverse_list. It takes an array as a parameter and returns the
reverse of the array (use loops).
print (reverse_list([1, 2, 3, 4, 5]))
#[5, 4, 3, 2, 1]
print(reverse_list(["A", "B", "C"]))
#["C", "B", "A"]
9. Declare a function named capitalize_list_items. It takes a list as a parameter and returns a
capitalized list of items.
10. Declare a function named add_item. It takes a list and an item as parameters. It returns a list
with the item added at the end.
food_staff = ['Potato', 'Tomato', 'Mango', 'Milk']
print(add_item(food_staff, 'Fungi')) # ['Potato', 'Tomato', 'Mango', 'Milk', 'Fungi']
numbers = [2, 3, 7, 9]
print(add_item(numbers, 5)) # [2, 3, 7, 9, 5]
11. Declare a function named remove_item. It takes a list and an item as parameters. It returns
a list with the item removed from it.
food_staff = ['Potato', 'Tomato', 'Mango', 'Milk']
print(remove_item(food_staff, 'Mango')) # ['Potato', 'Tomato', 'Milk']
numbers = [2, 3, 7, 9]
print(remove_item(numbers, 3)) # [2, 7, 9]
12. Declare a function named sum_of_odds. It takes a number parameter and adds all the odd
numbers in that range.
13. Declare a function named sum_of_even. It takes a number parameter and adds all the even
numbers in that range.
14. Call your function is_empty, it takes a parameter and checks if it is empty or not.
15. Write a different function which take lists. They should calculate_mean, calculate_median,
calculate_mode, calculate_range, calculate_variance, and calculate_std (standard
deviation).
16. Write a Python program to count the number of lines in a text file.
17. Write a Python program to read a file line by line and store it into a list.
18. Write a Python program to remove newline characters from a file.
19. Write a Python program to copy the contents of a file to another file.
20. Write a Python program to combine each line from first file with the corresponding line in
second file