Page -2
DATA TYPES and Introduction to Operators
Data types tell about the type of data hold by a variable and the type of operations that can be performed on that particular variable.
Taken From NCERT |
Examples-
- int data type stores integer numbers like 4, 5 , -3, 0 etc.
- float data type stores the floating point numbers like -3.4, 6.7, etc.
- list variable holds a number of data items enclosed in [] like city_list=["delhi", "nagpur","Hydrabad"]
Operators
- Arithmetic Operators
- Relational Operators
- Assignment Operators
- Logical Operators
- Identity Operators
- Membership Operators
(1) Arithmetic Operators
Arithmetic Operators | |||
Operator | Operation | Description | Example |
+ | Addition | It adds two numbers or concatenates the strings | 1+1=2 “hello”+”class”=”helloclass” |
- | Subtraction | It subtracts the two numbers | 2-1=1 |
* | Multiplication | It Multiplies the two numbers or Repeats a String | 2*3=6 “Hello”*2= HelloHello |
/ | Division | It divides the two numbers | 6/2=3.0 |
% | Modulus | Used to get the remainder | 14%3=2(remainder) |
// | Floor Division | Gives the quotient by removing the decimal part | 14//3=3.0 |
** | Exponent | Used to calculate “Power of operation” or exponent | 3**2=9 |
(2) Relational Operators
Relational Operators | |||
Operator | Operation | Description | Example |
== | Equals to | Compares two values if they are equal | 1==1 returns True 2==1 returns False |
!= | Not Equals to | Compares two values if they are not equal | 1!=1 returns False 2!=1 returns True |
> | Greater Than | Compare if the value on the left side is greater than | 2>1 returns True 1>2 returns False |
< | Less Than | Compare if the value on the left side is less than | 2<1 Returns False 1<2 Returns True |
>= | Greater Than or Equal to | Compare if the value on the left side is greater than or Equal to | 2>=1 returns True 1>=1 returns True |
<= | Less Than or Equal to | Compare if the value on the left side is less than or Equal to | 2<=1 Returns False 1<=1 Returns True |
(3) Assignment Operators
Assignment Operators | ||
Operator | Description | Example |
= | Assigns Value | x=2 x variable is assigned value 2 |
+= | Adds and Assigns x+=y Same as x=x+y | x=2 y=3 x+=y makes the x=5 |
-= | Subtracts and Assign x -= y is same as x = x - y | x=5 y=3 x-=y makes the x=2 |
*= | Multiply and Assign x *= y is same as x = x * y | x=5 y=3 x*=y makes the x=15 |
/= | Divides and Assign x /= y is same as x = x / y | x=15 y=3 x/=y makes the x=5.0 |
%= | Modulus and Assign x %= y is same as x = x % y | x=5 y=3 x%=y makes the x=2 |
//= | Floor Division and Assign x //= y is same as x = x // y | x=5 y=3 x//=y makes the x=1.0 |
**= | Exponent and assign x **= y is same as x = x ** y | x=3 y=2 x**=y makes the x=9 |
NOTE- Any Non-Zero value is considered as True in python.
(4) Logical Operators
Logical Operators | ||
Operator | Description | Example |
and | Logical AND | >>>1 and 0 Results in False |
or | Logical OR | >>>1 or 0 Results in True |
not | Logical NOT | >>> not 1 Results in False |
Precedence of Operators
Operators are evaluated according to their precedence like we did in our maths class where we solve a given mathematical expression according to the BODMAS Rule.
Example-
What will be the answer of 2*3+4?
Here, we need to understand whether we should do the 2*3 first or 3+4 first.
Now, because * operator has higher precedence than that of +, we will do 2*3 first, and than 6 will be added to 4. Accordingly, the answer will be 10.
Precedence determines which operator should be evaluated first.
Order of Precedence of different Operators in decreasing order-
1 ** Exponentiation (raise to the power)
2 ~ ,+, - Complement, unary plus, and unary minus
3 * ,/, %, // Multiply, divide, modulo and floor division
4 +, - Addition and subtraction
5 <= , < , > , >=, == , != Relational and Comparison operators
6 =, %=, /=, //=, -=, +=, *=, **= Assignment operators
7 is, is not Identity operators
8 in, not in Membership operators
9 not
10 and Logical operators
11 or
Note-
1)if Parenthesis () is used in an expression, it will be evaluated first.
2) if operators with the same precedence are used in an expression, it will be evaluated from left or right.
No comments:
Post a Comment