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
    • Go Programs
    • Python Programs
    • Java Programs

Python log2

by suresh

The Python log2 math function is used to calculate the logarithmic value of a given number of base 2. In this section, we discuss how to use math log2 function in Python Programming language with example.

The syntax of the log2 Function in Python is

  • If the number argument is a positive number, thelog2 function returns the output.
  • If the number argument is a Negative number or Zero, log2 function returns ValueError.
  • And if it is not a number, log2 function return TypeError.
math.log2(number);

Python log2 Function Example

The Python log2 Function calculates the logarithmic value of a given number of base 2. In this example, We are going to check the base 2 logarithmic value with different data types and display the output.

TIP: The math log2 function is more accurate than math.log(x, 2). Please refer to the Python log article to understand the log function.

# Python LOG2 Function

import math

Tup = (10, 20, 30, -40 , 50) # Tuple Declaration
Lis = [-1, 2, 3.5, -43 , 50] # List Declaration

print('Logarithm value of Positive Number = %.2f' %math.log2(5))
print('Logarithm value of Positive Decimal = %.2f' %math.log2(2.5))

print('Logarithm value of Tuple Item = %.2f' %math.log2(Tup[2]))
print('Logarithm value of List Item = %.2f' %math.log2(Lis[2]))

print('Logarithm value of Multiple Number = %.2f' %math.log2(2 + 7 - 5))
print('Logarithm value of String Value = ', math.log2('Python'))
Python LOG2 Function
  1. Within the first two statements, We used the log2 Function directly on Positive integer and Decimal values.
  2. Next two statements, We used the log2 Function on Tuple and List items. If you observe the above Python screenshot, this Math function is calculating the logarithm value of base 2.
  3. Next statement, We tried Python log2 Function with multiple values
  4. Next, We tried log2 Function on the String value, and it returns TypeError: a float is required
  5. Here, We tried log2 Function on Zero value. As we said before, this is returning ValueError: math domain error
  6. Last, We tried log2 Function on Negative value. As we said before, it returns ValueError: math domain error

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 Handling
  • 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

Copyright © 2021 · All Rights Reserved by Suresh

About Us | Contact Us | Privacy Policy