Python Basic – Mathematical operations by python

Home /

Table of Contents

Some basic operations

Mathematical calculations can be easily done on the Python console. So open the Python Interpreter again.

The results can be easily found by writing the following mathematical commands on the console. Like addition and subtraction, multiplication and division can be done easily here. Using brackets, it is possible to determine which part of the piece will be operated first. Dividing by a single / using the result is float type decimal. And if you divide using double //, the result is a decimal of integer type.

   >>> 3+8+10
   21
   >>> 3*8*10
   240
   >>> 3*8*(5+5)
   240
   >>> 3*8/(5+5)
   2.4
   >>>

As in general math, in Python, dividing a number by zero will result in an error,

   >>> 240/0
   Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   ZeroDivisionError: division by zero
   >>>

Float Data Type

Float is used to represent numbers in Python that are not integer types. e.g., 1.0, -5.15, etc. Using a decimal point in any number means that it becomes a float type of data. In Python, dividing the number of any two integer types yields the result of a float type. E.g.

   >>> 1/3
   0.3333333333333333
   >>> 3/8
   0.375
   >>> 10/8
   1.25
   >>> 3+1.25
   4.25
   >>> 3*8.90
   26.700000000000003
   >>> 8/2.50
   3.2
   >>>

Some more numeric operations

In addition to addition, subtraction, and multiplication, Python has support for expansion, which we call the power of one number over another. This operation is done with two ** marks. Only integer division is used to determine the quotient, and the modulo operator is used to determine the quotient. Integer division is done using two forward slashes, //, and the modulo operator is used with the % symbol. Let’s look at the example below:

   >>> 8**3
   512
   >>> 2**3
   8
   >>> 24/5
   4.8
   >>> 24//5
   4
   >>> 38%3
   2
   >>> 50%13
   11
   >>>

Importance of Mathematical operations in python

Mathematical operations are important in Python, as they allow you to perform various calculations and manipulations on numeric data. They are used in a wide range of applications, including data analysis, scientific computing, financial modeling, and many other fields.

Some common mathematical operations in Python include:-

  1. Arithmetic operations: Python supports the basic arithmetic operations of addition, subtraction, multiplication, and division, as well as more advanced operations such as exponents and modulus. Here’s an example of performing arithmetic operations in Python:
>>> 2 + 3
5
>>>
5 - 2
3
>>>
3 * 4
12
>>>
6 / 2
3.0
>>>
5 % 2
1
>>>
2 ** 3
  1. Mathematical functions: Python has a built-in math module that provides a wide range of mathematical functions, such as trigonometric functions, logarithmic functions, and more. Here’s an example of using the math module to calculate the square root of a number:
import math
>>> math.sqrt(16)
4.0
  1. Comparison operators: Python supports the comparison operators of ==, !=, <, >, <=, and >=, which can be used to compare values and make decisions in control flow statements. Here’s an example of using comparison operators in Python:
>>> 2 == 2
True
>>> 3 > 5
False
>>> 4 <= 4
True
  1. Bitwise operations: Python supports bitwise operations such as bitwise AND, OR, XOR, and NOT. These operations can be performed on integers and are used to manipulate the binary representation of the numbers. Here’s an example of performing a bitwise operation in python:
>>> 5 & 3
1

The above example performs a bitwise AND operation between the binary representation of 5 and 3, which results in 00101 & 00011 = 00001, which is equivalent to 1 in decimal.

Similarly, bitwise OR operation can be performed using the | operator, XOR operations can be performed using the ^ operator, and NOT operation can be performed using the ~ operator.

>>> 5 | 3
7
>>> 5 ^ 3
6
>>> ~5
-6

Bitwise operations are useful when working with bit-level manipulation or when working with low-level systems or embedded systems that require direct manipulation of the memory. They are also used in cryptography and data compression.

Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit

Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.

Other Recommended Article