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.
Generally, int datatype is used for non-decimal natural numbers.Examples : 2,35,6,78..
Float means floating point number/decimal number. Typically, you can find these numbers with (.). Examples: 1.5, 3.6, 2.8.
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.
>>> intExample = 78
>>> floatExample = 7.8
>>> complexExample = 7+8j
Like other programming languages, python has also few operators which let us perform the logical and arithmetic operations.
Python Arithmetic Operators
|Operator Symbol||Operator Name||Operation||Result|
|+||Addition||4 + 2.5||6.5|
|**||power||2 ** 2||4|
|%||modulus||8 % 3||2|
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|
|<<, >>||Bitwise shifts|
|in, not in, is , is not, <=, >=, ==, !=||comparisons|
|not x||Boolean NOT|
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 Symbol||Operator Name||Example|
|+=||Additional Assignment||i = i + 2|
|-=||Subtraction Assignment||i = i - 2|
|*=||Multiplication Assignment||i = i * 2|
|/=||Division Assignment||i = i / 2|
|//=||Integer Division Assignment||i = i // 2|
|%=||Remainder Assignment||i = i % 2|
|**=||Power Assignment||i = i ** 2|
In the next article, We are going to learn about Python Strings