Python tutorial 3rd lesson, Numerics - BoardCode
 Numerics
Hi everyone.
This is the third lesson of Python tutorial. In this lesson we would talk about data types in Python exactly Numerics.


There are 6 main categories of data in Python. Numerics, sequences, mappings instances, exceptions and classes.
As we said above, here we'll talk about Numerics.
Numerics class itself contains three other distinct subclasses integers, floating point numbers and complex numbers.
 An integer is a number doesn't have a fractional part like -5,-3,-1,0,1,2,5...etc.

A floating point number is a number has a fractional part like -3.1,-2.5,0.01,4.5 ...etc.

A complex number is a number has a real and an imaginary part like 1+I,2+5i,5-6i...etc in programming we use "j" instead of "i" .

Here are operations could be applied on all numerics.
" + " addition
" - " substation
" * " multiplication
" / " division and
" ** " raising to power.
Notes:
**Division always returns a floating point number as result.
**Double star is raising to power sign in Python. Some languages use "^" instead.

Here are operations could be applied only on integers or floating point numbers
" x//y " div ( multiple of Euclidean's division)
" x%y " modulo (remainder of Euclidean's division).

Here are operations could be applied only on integers. They are also called bitwise.

Here, you can apply the operations on both binary and decimal system.
"  x << y " shift x to left by y places. In other words, x multiplied by 2**y
Example
3<<1 ,3 multiplied by 2**1 so the result will be 6

" x >> y " shift x to right by y places, in other words, x divided by 2**y
6 >>1 will be 3.

" a&b " bitwise AND.  Concept is converting the two integers a and b to binary and compare one bit from a with another one from b, if the two bits were "1" the result will be "1" ,otherwise it will be "0" .

To understand bitwise "&" well ,we have to make an example
7&12 ?  Before ,let's convert 7 and 12 to binary 7 is 0111 and 12 is 1100 for 4-bites (0111 & 1100=0100=4), then 7&12=4.

" a|b " bitwise OR , the same ,except that if only one of the two bits were "1" the result will be "1", otherwise it will be" 0".
Example
7|12=?
( 0111 | 1100= 1111=15) ,then 7|12=15.

" a^b " bitwise XOR the same, but result will be "1" if one of the two bits were "1" and the other were "0" . In other wards, if the two bits weren't same ,the result will be "1" ,otherwise it will be "0".
Example
7^12 ?
( 0111 ^ 1100= 1011=11),then 7^12=11.

Comparison operations:
operationm        meaning
   ==                        equal to
   != or <>              not equal to
   <                          less than
   >                          greater than
   <=                        less than or equal
   >=                        greater than or equal

Be careful
In programming we use "=" to assign data into a variable, However. we use "==" to compare between two variables.

Assignment operations:
Let's discuss about this instrument
a=a+3 this tells Python that the variable a will take a new value , that value will be its last value + 3 .We can writ that instrument in short firm like this  a+=3
a=a+3  is the same with a+=3
You can make this short form with
"   +   ,  -  ,  *  ,   /   ,   **   ,   //   ,   ℅.  ,   &   ,   |   "  and  "   ^   " operation

Thanks for reading.

No comments:

Post a Comment

Pages