Python Practice Set (Part 7): Mastering Functions
- Anubhav Somani
- Sep 2
- 2 min read

Python Functions: Writing Clean, Reusable Code
Welcome to Part 7 of our Python practice series! If variables are the nouns of Python, and control flow is the grammar, then functions are the verbs—they perform the actions. Mastering functions is the single most important step to writing clean, organized, and efficient code. This practice set will guide you through defining functions, passing arguments, returning values, and understanding variable scope.
1. The Basic Greeting (Simple Function Definition)
Write a function called display_welcome_message that takes no arguments. When called, the function should simply print the string "Welcome to our Python practice series!". Call the function after you define it to see the output.
2. Rectangle Area Calculator (Parameters and Return Values)
Write a function named calculate_rectangle_area that takes two arguments: width and height. Inside the function, calculate the area of the rectangle (area = width * height) and return the result. Call the function with sample values (e.g., width 10, height 5) and print the returned value.
3. Personalized Greeting (Default Arguments)
Create a function greet_user that takes two arguments: name and greeting="Hello". The greeting argument should have a default value of "Hello". The function should return a formatted string like "[Greeting], [Name]!".
Test the function by calling it with just a name (e.g., greet_user("Alice")).
Test it again by providing both a name and a custom greeting (e.g., greet_user("Bob", "Good morning")).
4. Word Counter (Combining Functions and Data Types)
Write a function count_words_in_string that takes a single string (representing a sentence) as an argument. The function should:
Split the sentence into a list of words.
Return the total number of words in the list.
Call the function with a sample sentence and print the result.
5. Temperature Converter (Logic and Multiple Return Paths)
Create a function convert_temp that takes a temperature (float) and a unit (string: either "C" for Celsius or "F" for Fahrenheit) as input.
If the unit is "C", convert the temperature to Fahrenheit (F = C * 9/5 + 32) and return it.
If the unit is "F", convert the temperature to Celsius (C = (F - 32) * 5/9) and return it.
If the unit is anything else, return the string "Invalid unit".
Test your function with all three scenarios.
Comments