Numbers and arithmetic operations in Python

    python-logo

    Python is a versatile programming language that is widely used for a variety of tasks, from data analysis to web development. One of the fundamental aspects of programming is working with numbers and performing arithmetic operations.

    Number Data Types in Python

    Python has several built-in data types for working with numbers. These include:

    • int: Integer numbers (positive or negative)
    • float: Floating-point numbers (numbers with a decimal point)
    • complex: Complex numbers (numbers with a real and imaginary part)

    Let's take a closer look at each of these data types.

    Integers

    Integers are whole numbers, either positive or negative. They are represented in Python by the int data type. Here are some examples of integer literals:

    x = 10
    y = -5
    z = 0

    Integers can be used in mathematical expressions just like regular numbers. For example:

    result = 2 * x + y

    Floating-Point Numbers

    Floating-point numbers, or floats, are numbers with a decimal point. They are represented in Python by the float data type. Here are some examples of float literals:

    x = 3.14159
    y = -0.1
    z = 2.0

    Floats can be used in mathematical expressions just like integers. For example:

    result = x / y + z

    Complex Numbers

    Complex numbers have both a real and an imaginary part. They are represented in Python by the complex data type. Here are some examples of complex literals:

    x = 2 + 3j
    y = -1j
    z = 4.5 - 2.1j

    Complex numbers can be used in mathematical expressions just like integers and floats. For example:

    result = x + y * z

    Arithmetic Operations in Python

    Python supports a variety of arithmetic operations for working with numbers. These include:

    • Addition: +
    • Subtraction: -
    • Multiplication: *
    • Division: /
    • Integer Division: //
    • Modulus: %
    • Exponentiation: **

    Let's take a closer look at each of these operations.

    Addition

    Addition is a basic arithmetic operation that adds two numbers together. In Python, you can use the + operator to perform addition. Here is an example:

    x = 5
    y = 3
    z = x + y
    print(z) # Output: 8

    Subtraction

    Subtraction is the opposite of addition, and subtracts one number from another. In Python, you can use the - operator to perform subtraction. Here is an example:

    x = 5
    y = 3
    z = x - y
    print(z) # Output: 2

    Multiplication

    Multiplication is a basic arithmetic operation that multiplies two numbers together. In Python, you can use the * operator to perform multiplication. Here is an example:

    x = 5
    y = 3
    z = x * y
    print(z) # Output: 15

    Division

    Division is the opposite of multiplication, and divides one number by another. In Python, there are two types of division:

    • Classic division: Returns a float value that represents the quotient of the division. In Python 3, this is the default type of division.
    • Integer division: Returns an integer value that represents the quotient of the division. In Python 3, you can use the // operator to perform integer division.

    Here are examples of each type of division:

    x = 5
    y = 3
    Classic division
    z = x / y
    print(z) # Output: 1.6666666666666667
    
    Integer division
    w = x // y
    print(w) # Output: 1

    Exponentiation

    Exponentiation is the operation of raising a number to a power. In Python, you can use the ** operator to perform exponentiation. Here is an example:

    x = 2
    y = 3
    z = x ** y
    print(z) # Output: 8

    Conclusion

    Python provides a wide range of data types and variables that allow you to store and manipulate data in your programs. The ability to perform arithmetic operations on these data types is a fundamental feature of Python programming, and is essential for many applications.