×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Create a function to return the absolute the given value in Python

Here, we are going to learn to create a function to return the absolute the given value in Python programming language. By IncludeHelp Last updated : January 05, 2024

Problem statement

In the below program – we are creating a function named get_absolute_value(), it accepts a number and returns the absolute value of the given number.

Python program to create a function to return the absolute the given value

# Function to return the
# absolute the given value in Python
def get_absolute_value(n):
    if n >= 0:
        return n
    else:
        return -n

# main code
print(get_absolute_value(101))
print(get_absolute_value(-202))
print(get_absolute_value(10.23))
print(get_absolute_value(-34.56))
print(get_absolute_value(0.34))
print(get_absolute_value(-0.45))

Output

The output of the above program is:

101
202
10.23
34.560.34
0.45

To understand the above program, you should have the basic knowledge of the following Python topics:

Python Basic Programs »

Advertisement
Advertisement

Related Programs

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.