Working with Negative Numbers in Python
Last Updated :
01 Mar, 2024
Negative numbers play a crucial role in various mathematical and programming scenarios. In Python, handling negative numbers is a fundamental skill for developers. In this article, we will explore some commonly used methods for working with negative numbers in Python, along with examples to illustrate each approach.
Working with Negative Numbers in Python
Below are some examples by which can understand the working of Negative Numbers in Python:
- Arithmetic Operation
- Absolute Value
- Conditional Statements
- Built-in Functions
- Using Math Module
Arithmetic Operation with Negative Numbers in Python
Python supports basic arithmetic operations for negative numbers, just like positive numbers. Addition, subtraction, multiplication, and division can all be performed on negative numbers using the standard arithmetic operators.
Python3
# Example of basic arithmetic operations
a = -5
b = 3
sum_result = a + b
difference_result = a - b
product_result = a * b
quotient_result = a / b
print("Sum:", sum_result)
print("Difference:", difference_result)
print("Product:", product_result)
print("Quotient:", quotient_result)
OutputSum: -2
Difference: -8
Product: -15
Quotient: -1.6666666666666667
Absolute Value with Negative Numbers in Python
The abs()
function in Python returns the absolute value of a number, which is its distance from zero. This method is useful when you need to work with the magnitude of a negative number.
Python3
# Example of using abs() function
negative_number = -8
absolute_value = abs(negative_number)
print("Absolute Value:", absolute_value)
Conditional Statements with Negative Numbers in Python
Using conditional statements allows you to check if a number is negative and take specific actions accordingly. This method is particularly useful when you want to execute different code based on whether a number is positive or negative.
Python3
# Example of using conditional statements
number = -12
if number < 0:
print("The number is negative.")
else:
print("The number is non-negative.")
OutputThe number is negative.
Built-in Functions and Negative Numbers in Python
Python provides built-in functions for various mathematical operations, such as min()
, max()
, and sum()
. These functions can be used to find the minimum, maximum, and sum of a list of numbers, including negative ones.
Python3
# Example of using built-in functions
numbers = [5, -8, 12, -4, 7]
min_value = min(numbers)
max_value = max(numbers)
sum_value = sum(numbers)
print("Minimum Value:", min_value)
print("Maximum Value:", max_value)
print("Sum:", sum_value)
OutputMinimum Value: -8
Maximum Value: 12
Sum: 12
Using Math Module with Negative Numbers in Python
The math
module in Python provides additional mathematical functions, including exponentiation, logarithms, and trigonometric functions. This module is particularly useful for more advanced mathematical operations involving negative numbers.
Python3
# Example of using the math module
import math
negative_number = -16
square_root = math.sqrt(abs(negative_number))
exponential_result = math.exp(negative_number)
print("Square Root:", square_root)
print("Exponential Result:", exponential_result)
OutputSquare Root: 4.0
Exponential Result: 1.1253517471925912e-07
Similar Reads
Print all Negative Numbers in a Range - Python We are given a range we need to print all negative number within specific range. For example, we are given start and end point start, end = -5, 0 we need to print all numbers so that output should be -5 -4 -3 -2 -1.Using a loopWe can use a loop to iterate through a given range and check if each numb
3 min read
Python program to print negative numbers in a list In Python, itâs often necessary to find and print negative numbers from a list. In this article we will explore various approches to print negative numbers in a list. The most basic method for printing negative numbers is to use a for loop to iterate through the list and check each element.Pythona =
2 min read
Subtract Two Numbers in Python Subtracting two numbers in Python is a basic operation where we take two numeric values and subtract one from the other. For example, given the numbers a = 10 and b = 5, the result of a - b would be 5. Let's explore different methods to do this efficiently.Using Minus Operator (-)This is the most st
2 min read
Python program to count positive and negative numbers in a list In this article, we will explore various methods to count positive and negative numbers in a list. The simplest way to do this is by using a loop. This method counts the positive and negative numbers in a list by iterating through each element using for loop.Pythona = [10, -20, 30, -40, 50, -60, 0]
2 min read
Python Program to Check Whether a Number is Positive or Negative or zero Given a number. The task is to check whether the number is positive or negative or zero. Examples: Input: 5 Output: Positive Input: -5 Output: Negative Approach: We will use the if-elif statements in Python. We will check whether the number is greater than zero or smaller than zero or equal to zero.
2 min read