Main Function Parameters and Reversal - Python Programming Exercise

In this exercise, you will develop a Python program named "reverse" that takes multiple words from the command line and prints them in reverse order. This exercise is perfect for practicing command-line input handling and string manipulation in Python. By implementing this program, you will gain hands-on experience in handling command-line input and performing string manipulation in Python. This exercise not only reinforces your understanding of command-line input handling but also helps you develop efficient coding practices for managing user interactions.

 Category

Mastering Functions

 Exercise

Main Function Parameters And Reversal

 Objective

Develop a Python program named "reverse" that takes multiple words from the command line and prints them in reverse order. For example:

reverse one two three three two one

 Example Python Exercise

 Copy Python Code
import sys

# Define the reverse function
def reverse():
    # Get the arguments from the command line (skipping the script name)
    words = sys.argv[1:]
    
    # Reverse the list of words
    reversed_words = words[::-1]
    
    # Print the reversed words
    print(" ".join(reversed_words))

# Main function to run the program
if __name__ == "__main__":
    reverse()

 Output

python reverse.py one two three
three two one

 Share this Python Exercise

 More Python Programming Exercises of Mastering Functions

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Function to Obtain Integer Value

    In this exercise, you will develop a Python program with a function named "get_int" that displays the text received as a parameter, prompts the user for an integer, a...

  •  Function to Manage Task Database

    In this exercise, you will develop a Python program to enhance the "tasks database" by dividing it into multiple functions for better organization and readability. Th...

  •  Function to Find Maximum Value in an Array

    In this exercise, you will develop a Python program containing a function that accepts a list of floating-point numbers as input and returns the maximum value within ...

  •  Iterative Implementation of the Factorial Function

    In this exercise, you will develop a Python program that calculates the factorial of a given number using an iterative (non-recursive) method. This exercise is...

  •  Function to Print Title

    In this exercise, you will develop a Python program with a function called 'WriteTitle' that displays a text in uppercase, centered on the screen with extra spaces an...

  •  Main Function Output Value

    In this exercise, you will develop a Python program that prompts the user to provide a title via the command line (using the previously defined WriteTitle function). ...