Function to Check Numeric Value - Python Programming Exercise

In this exercise, you will develop a Python program that defines a function to check if a string represents an integer value. This exercise is perfect for practicing function definition, string manipulation, and conditional statements in Python. By implementing this function, you will gain hands-on experience in handling function definitions, string manipulation, and conditional statements in Python. This exercise not only reinforces your understanding of functions but also helps you develop efficient coding practices for managing user interactions.

 Category

Mastering Functions

 Exercise

Function To Check Numeric Value

 Objective

Develop a Python program that defines a function to check if a string represents an integer value. This function can be used to determine whether a string like '1234' is a valid numeric value. If it is, the program will print a message saying 'It is a numerical value'.

 Example Python Exercise

 Copy Python Code
# Function to check if a string represents an integer value
def is_integer(value):
    # Try to convert the string to an integer
    try:
        int(value)  # Attempt to convert the string to an integer
        return True  # Return True if successful
    except ValueError:
        return False  # Return False if a ValueError occurs (i.e., not a valid integer)

# Example usage of the is_integer function
def main():
    # Take a string input from the user
    value = input("Enter a value: ")

    # Check if the input value is a valid integer
    if is_integer(value):
        print(f"It is a numerical value.")  # Print if it is a valid integer
    else:
        print(f"It is not a numerical value.")  # Print if it is not a valid integer

# Run the main function if the script is executed directly
if __name__ == "__main__":
    main()

 Output

Case 1:
Enter a value: 1234
It is a numerical value.

Case 2:
Enter a value: abc
It is not a numerical value.

 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.

  •  Main Function Parameters for Calculator

    In this exercise, you will develop a Python program that performs basic arithmetic operations such as addition, subtraction, multiplication, or division based on comm...

  •  Main Function Parameters and Return Value for Calculator

    In this exercise, you will develop a Python program that performs arithmetic operations such as addition, subtraction, multiplication, or division by analyzing comman...

  •  Function to Find Array's Min and Max Values

    In this exercise, you will develop a Python program that defines a function to find the smallest and largest numbers in a list. The function should accept the list as...

  •  Recursive Function to Invert a Sequence

    In this exercise, you will develop a Python program that utilizes recursion to reverse a sequence of characters. This exercise is perfect for practicing functi...

  •  Function to Display a Rectangle

    In this exercise, you will develop a Python program that defines a function to print a filled rectangle on the screen, where the dimensions (width and height) are pro...

  •  Iterative Palindrome Function

    In this exercise, you will develop a Python program with an iterative function to check if a string is a palindrome (symmetric). This exercise is perfect for p...