Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs

Python ldexp

by suresh

The Python ldexp function is one of the Python Math function used to return x * (2**i). Python ldexp function also called the inverse of Python frexp function. In this section, we discuss how to use ldexp function in Python Programming language with example.

Syntax of a Python ldexp Function

The syntax of the ldexp Function in Python Programming Language is

math.ldexp(x, i);
  • X: Please specify the X value here.
  • i: Please specify the i value here.

For example, if x = 2 and i = 3 then, Math.ldexp(2, 3) = 16. It is because, the formula behind this function is x * (2**i)

Output = 2 * (2**3)

Output = 2 * (8)

so, the output is 16

NOTE: If the X value or i value argument is not a number, ldexp function return TypeError.

Python ldexp Function Example

The Python ldexp function returns x * (2**i). In this example, we are going to find the ldexp value of different data types and display the output.

# Python ldexp Function

import math

Tup = (1.50, 2.26, -3.05, -4.95 , 5.85) # Tuple Declaration
Lis = [-1.98, 2.65, -9.29, -4.15 , 9.97] # List Declaration

print('LDEXP() Function on Positive Number = %.2f' %math.ldexp(2, 3))
print('LDEXP() Function on Negative Number = %.2f' %math.ldexp(-3, 2))

print('LDEXP() Function on Positive Decimal = %.2f' %math.ldexp(4.5, 2))
print('LDEXP() Function on Negative Decimal = %.2f' %math.ldexp(-6.5, 3))

print('LDEXP() Function on Tuple Item = %.2f' %math.ldexp(Tup[2], 3))
print('LDEXP() Function on Tuple Item = %.2f' %math.ldexp(Tup[4], 3))
print('LDEXP() Function on List Item = %.2f' %math.ldexp(Lis[2], 4))
print('LDEXP() Function on List Item = %.2f' %math.ldexp(Lis[4], 4))

print('LDEXP() Function on Multiple Number = %.2f' %math.ldexp(1 + 2 - 9, 2))

print('LDEXP() Function on String Value = ', math.ldexp('2.95', 2))

OUTPUT

Python LDEXP Function

ANALYSIS

  1. Within the first two statements, We passed both the Positive integer and negative integer as the ldexp Function arguments. From the above screenshot, see that the ldexp Function is returning output.
  2. Within the next two statements, we passed both the Positive and negative decimal values as the ldexp Function arguments. You can see that the ldexp Function is returning output.
  3. Next four statements, We used the Python Tuple and Python List items as first arguments and Positive and negative decimal values as the second argument for ldexp Math function. If you observe the above, the ldexp function is working perfectly on them.
  4. Next, we assigned multiple values as the Python first arguments. And the ldexp Function worked without any issue.
  5. Last, We tried ldexp Function on the String value, and it returns TypeError.

Placed Under: Python

  • Download and Install Python
  • Python Arithmetic Operators
  • Python Assignment Operators
  • Python Bitwise Operators
  • Python Comparison Operators
  • Python Logical Operators
  • Python If Statement
  • Python If Else
  • Python Elif Statement
  • Python Nested If
  • Python For Loop
  • Python While Loop
  • Python Break
  • Python Continue
  • Python Dictionary
  • Python datetime
  • Python String
  • Python Set
  • Python Tuple
  • Python List
  • Python List Comprehensions
  • Python Lambda Function
  • Python Functions
  • Python Types of Functions
  • Python Iterator
  • Python File
  • Python Directory
  • Python Class
  • Python classmethod
  • Python Inheritance
  • Python Method Overriding
  • Python Static Method
  • Connect Python and SQL Server
  • Python SQL Create DB
  • Python SQL Select Top
  • Python SQL Where Clause
  • Python SQL Order By
  • Python SQL Select Statement
  • Python len Function
  • Python max Function
  • Python map Function
  • Python print Function
  • Python sort Function
  • Python range Function
  • Python zip Function
  • Python Math Functions
  • Python String Functions
  • Python List Functions
  • Python NumPy Array
  • NumPy Aggregate Functions
  • NumPy Arithmetic Operations
  • Python Numpy Bitwise operators
  • Numpy Comparison Operators
  • Numpy Exponential Functions
  • Python Numpy logical operators
  • Python Numpy String Functions
  • NumPy Trigonometric Functions
  • Python random Array
  • Python Numpy concatenate
  • Python Numpy Array shape
  • Python Pandas DataFrame
  • Pandas DataFrame plot
  • Python Series
  • Python matplotlib Histogram
  • Python matplotlib Scatter Plot
  • Python matplotlib Pie Chart
  • Python matplotlib Bar Chart
  • Python List Length
  • Python sort List Function
  • Python String Concatenation
  • Python String Length
  • Python Substring
  • Python Programming Examples
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy