Python Numbers

Python Numbers

Python is one of the most popular programming languages today. It is loved by developers for its simplicity, readability and versatility. One of the main reasons why Python language is so versatile is the availability of various data types. One such data type is “Numbers”.

Types of Python Numbers

Python language supports three types of numbers:

1. Integers

Integers are whole numbers. They can be either positive or negative. In Python language, integers are denoted by the “int” keyword. For example:

    
        number = 10 # positive integer
        negative_number = -5 # negative integer
    

2. Float

Floats are numbers with a decimal point. In Python language, floats are denoted by the “float” keyword. For example:

    
        pi = 3.14 # float
    

3. Complex numbers

Complex numbers are numbers that have both a real and imaginary part. In Python language, complex numbers are denoted by the “complex” keyword. For example:

    
        a = 5 + 3j # 5 is the real part and 3j is the imaginary part
    

Basic Arithmetic Operations

Python language supports various arithmetic operations on numbers. These include:

  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)
  • Modulus (%)
  • Exponentiation (**)
  • Floor division (//)

Addition

Addition operator ( + ) is used to add two or more numbers. In Python language, addition can be performed on integers, floats, and complex numbers. For example:

    
        a = 10
        b = 20
        c = a + b
        print(c) # Output: 30
    

Subtraction

Subtraction operator ( – ) is used to subtract one number from another. In Python language, subtraction can be performed on integers, floats, and complex numbers. For example:

    
        a = 20
        b = 10
        c = a - b
        print(c) # Output: 10
    

Multiplication

Multiplication operator ( * ) is used to multiply two or more numbers. In Python language, multiplication can be performed on integers, floats, and complex numbers. For example:

    
        a = 5
        b = 6
        c = a * b
        print(c) # Output: 30
    

Division

Division operator ( / ) is used to divide one number by another. In Python language, division can be performed on integers, floats, and complex numbers. For example:

    
        a = 20
        b = 5
        c = a / b
        print(c) # Output: 4.0
    

Modulus

Modulus operator ( % ) is used to find the remainder when one number is divided by another. This operation can only be performed on integers. For example:

    
        a = 20
        b = 7
        c = a % b
        print(c) # Output: 6
    

Exponentiation

Exponentiation operator ( ** ) is used to raise a number to a power. In Python language, exponentiation can be performed on integers, floats, and complex numbers. For example:

    
        a = 2
        b = 3
        c = a ** b
        print(c) # Output: 8
    

Floor Division

Floor division operator ( // ) is used to perform integer division. This operation returns the quotient after division, discarding any fractional part. In Python language, floor division can be performed on integers, floats, and complex numbers. For example:

    
        a = 20
        b = 6
        c = a // b
        print(c) # Output: 3
    

Conversion between Data Types

Python allows for conversion between different data types. Below are some examples:

1. int()

The int() function converts a string or float into an integer. For example:

    
        a = "10"
        b = int(a)
        print(b) # Output: 10
    

2. float()

The float() function converts a string or integer into a float. For example:

    
        a = 10
        b = float(a)
        print(b) # Output: 10.0
    

3. complex()

The complex() function creates a complex number from two arguments, the real and imaginary parts. For example:

    
        a = complex(3, 4)
        print(a) # Output: (3+4j)
    

4. str()

The str() function converts a number into a string. For example:

    
        a = 10
        b = str(a)
        print(b) # Output: "10"
    

Numerical Functions

Python language provides many built-in numerical functions that can be used to perform various operations on numbers. Some of the commonly used numerical functions are:

1. abs()

The abs() function returns the absolute value of a number. For example:

    
        a = -10
        b = abs(a)
        print(b) # Output: 10
    

2. pow()

The pow() function returns the result of raising a number to a given power. For example:

    
        a = 2
        b = pow(a, 3)
        print(b) # Output: 8
    

3. round()

The round() function rounds a floating-point number to a given number of decimal places. For example:

    
        a = 3.14159
        b = round(a, 2)
        print(b) # Output: 3.14
    

4. max()

The max() function returns the largest number in a given sequence. For example:

    
        a = [1, 3, 5, 7, 9]
        b = max(a)
        print(b) # Output: 9
    

5. min()

The min() function returns the smallest number in a given sequence. For example:

    
        a = [1, 3, 5, 7, 9]
        b = min(a)
        print(b) # Output: 1
    

Conclusion

In this article, we have learned about the different types of numbers in Python language, their basic arithmetic operations, conversion between data types, and some of the commonly used numerical functions. This knowledge will help you to perform various mathematical computations in Python language.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top