Challenge one
[su_spoiler title=”Show suggestion” style=”fancy” icon=”chevron-circle”]
# Prompting the user to input a list of words user_input = input("Enter a list of words, separated by spaces: ") # Splitting the input string into a list of words words = user_input.split() # Initializing an empty list to store words starting with 'a' words_starting_with_a = [] # Iterating through each word in the list for word in words: # Checking if the word starts with the letter 'a' (case-insensitive) if word.lower().startswith('a'): # Appending the word to the list if it starts with 'a' words_starting_with_a.append(word) # Printing the words that start with 'a' print("Words starting with 'a':", words_starting_with_a)
[/su_spoiler]
Explanation of the code:
User Input: The program prompts the user to enter a list of words, separated by spaces.
Splitting Words: The input string is split into a list of words using the split() method.
Filtering Words: The program iterates through each word in the list and checks if it starts with the letter ‘a’ (case-insensitive). Words that meet this criterion are added to the words_starting_with_a list.
Printing Results: Finally, the program prints the words that start with ‘a’.
Challenge two
[su_spoiler title=”Show suggestion” style=”fancy” icon=”chevron-circle”]
def find_min_max(numbers): """ Function to find the minimum and maximum numbers in a list. Parameters: - numbers (list): List of numbers. Returns: - min_number (float): Minimum number in the list. - max_number (float): Maximum number in the list. """ # Checking if the list is not empty if not numbers: return None, None # Initializing variables to store minimum and maximum values min_number = float('inf') # Set to positive infinity initially max_number = float('-inf') # Set to negative infinity initially # Iterating through each number in the list for num in numbers: # Updating minimum and maximum values as needed if num < min_number: min_number = num if num > max_number: max_number = num # Returning the minimum and maximum values return min_number, max_number # Prompting the user to input a list of numbers user_input = input("Enter a list of numbers, separated by spaces: ") # Splitting the input string into a list of numbers numbers = [float(num) for num in user_input.split()] # Calling the function to find the minimum and maximum numbers min_num, max_num = find_min_max(numbers) # Printing the results if min_num is not None and max_num is not None: print("Minimum number:", min_num) print("Maximum number:", max_num) else: print("Please enter a valid list of numbers.")
[/su_spoiler]
Explanation of the code:
Function Definition: The program defines a function find_min_max that takes a list of numbers as input and returns the minimum and maximum numbers in the list.
User Input: The program prompts the user to enter a list of numbers, separated by spaces.
Splitting Numbers: The input string is split into a list of numbers, and each number is converted to a float.
Function Call: The function is called with the list of numbers to find the minimum and maximum values.
Printing Results: The program prints the minimum and maximum numbers, or a message if the input is invalid.
Challenge three
[su_spoiler title=”Show suggestion” style=”fancy” icon=”chevron-circle”]
# Prompting the user to input a list of numbers user_input = input("Enter a list of numbers, separated by spaces: ") # Splitting the input string into a list of numbers and converting them to float numbers = [float(num) for num in user_input.split()] # Sorting the list in ascending order sorted_numbers = sorted(numbers) # Printing the sorted list print("Sorted list in ascending order:", sorted_numbers)
[/su_spoiler]
Explanation of the code:
User Input: The program prompts the user to enter a list of numbers, separated by spaces.
Splitting Numbers: The input string is split into a list of numbers, and each number is converted to a float.
Sorting: The program uses the sorted() function to sort the list of numbers in ascending order.
Printing Results: Finally, the program prints the sorted list in ascending order.
Discord
Conclusion.
In the first challenge, we honed our string manipulation skills by filtering words starting with ‘a.’ This exercise underscored the importance of handling user inputs dynamically and showcased the power of list comprehension.
The second challenge pushed us to design a function for finding the minimum and maximum values in a list of numbers. Beyond traditional iteration, we embraced the concept of function-driven modular coding, enhancing both readability and reusability.
The final challenge invited us to orchestrate the sorting of a list of numbers in ascending order, emphasizing the simplicity and elegance of Python’s built-in functions.
Through these challenges, we not only cultivated problem-solving acumen but also honed our ability to create efficient and elegant Python code. Remember, the journey to Python proficiency is an ongoing exploration, and each challenge conquered adds a new layer to your coding expertise.
As you continue to tackle Python challenges, keep experimenting, refining, and embracing the joy of coding. Your mastery of Python is not just a skill; it’s a journey of continuous learning and growth. Happy coding!