Python Datatypes – Numbers And Operators

We have discussed datatypes and their classification in the last tutorial. If you have no idea on what are data types and how they function, you can read that article. In this article, you will understand one of the datatypes called numbers. Besides, you will get to know about various python operators.

Every programming language has few common data types pertaining to numbers like integers, Floating Point numbers and complex numbers. As I said earlier, you need not declare them in python.

Int

Generally, int datatype is used for non-decimal natural numbers.Examples : 2,35,6,78..

Float

Float means floating point number/decimal number. Typically, you can find these numbers with (.). Examples: 1.5, 3.6, 2.8.

Complex

Some of the complex numbers are 2+3j, 5+6j

If you want to find the data type of a specific number. you can use type() function on that variable. Please check the below code. 

 

Python Operators

Like other programming languages, python has also few operators which let us perform the logical and arithmetic operations.

Arithmetic Operations:

Python Arithmetic Operators

Operator SymbolOperator NameOperationResult
+Addition4 + 2.56.5
-Substraction 3-21
*Multipilication3 *26
/Divison6/23
//Integer Divison1//20
**power2 ** 24
%modulus8 % 32

In the above table, If you didn’t get //, ** and %. Let’s see them in depth.

// is an integer division operator whereas / is a division operator.

if you perform 2/5 operation, the result is 0.4. But 2//5 gives you the result zero (0). It gives you only the integer part in the number and ignores the decimal part.

** is an exponential operator which acts as a power (a ** b: a raise to the power of b)

Example: 2**2 = 4, 3**3 = 27 etc

% is called as modulus operator which helps us to find the remainder of two numbers. Example 2%2 = 0 and 2 % 6 = 3.

Boolean And Logical Operators

Operator Sign Operator Functioning
~xBitwise not
<<, >>Bitwise shifts
&Bitwise and
^Bitwise xor
|Bitwise or
in, not in, is , is not, <=, >=, ==, !=comparisons
not xBoolean NOT
andBoolean AND
orBoolean OR
lambdalambda expression

Assignment Operators

While writing code, we might have to do various calculations on the same variable mainly increment, decrement etc. For those type of operations, we use assignment operators. Please check the below table.

Operator SymbolOperator NameExample
+=Additional Assignmenti = i + 2
-=Subtraction Assignmenti = i - 2
*=Multiplication Assignmenti = i * 2
/=Division Assignmenti = i / 2
//=Integer Division Assignmenti = i // 2
%=Remainder Assignmenti = i % 2
**=Power Assignmenti = i ** 2

In the next article, We are going to learn about Python Strings

Leave a Reply

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