Operators are used to operating on values operations are generally a symbol that is used to manipulate any mathematical or logical expressions. There are six types of Operators in python. In this article, we will discuss every operator in detail with examples and explanations.
Note:- we will use a terminal to perform operations or writing code. To perform all these operators make sure you should have been installed the latest version of python. If you have already then you are good to go.
In Python Operators are different types:
- Airthmetic Operator
- Assignment Operator
- Comparision Operator/ Relational operator
- Logical / Boolean Operator
- Identity Operator
- Bitwise Operator
It is also called a binary operator. These Operators are mainly used to manipulate any mathematical expression.
For Example:- + , – , * , ? , / , % , // [Perform floor duration] , ** [ perform exponant]
Arithmetic operators are simple operators two main operators are there divide and floor. the difference between both of them is. Divide operator returns float value and floor operator returns an integer value.
It is also called a binary operator. These Operators are mainly used to assign a value to a variable these operators always work from right to left.
For Example:- = , + = , – = , * = , / = , % = , // = , * *=.
Assignment operators are used to assigning a value to a variable and then perform an operation with the same value. like we can see in the above code we assign a value (x = 5) to the variable x and then we used an assignment operator to perform an addition operation. (x+=5). as we have assigned before x=5. Now we will put the value of x in (x+=5) = (5+=5) =10.
It is also called a binary operator. These operators are mainly used to check a condition if the condition is true then return true if the condition is false then return false.
For example:- == , > , >= , < , <= , !=
in the above we assign two values to two different variable named x=5, y=10. And then we chacked that x=y if x is equal to y then it will return True otherwise false. In our case it will return false because x is not equal to y.
It is used for checking more than one conditions
For Example:- and (binary operator), or (binary operator), Not(unary operator).
Logical operators are binary operators. those operators return true or false. like we have done in the above code. we stored two different constant values in two different variables and then we used logical operators with comparison operators and conditions. if both conditions are true then and operator returns True otherwise it will return False. if both conditions are false then OR Operator will return False otherwise it will return True
Multiple values work on. Don’t work on a single value.
For Example:- in, not in.
Only two membership operators are there in Python named ‘in’ and ‘not in’. Suppose there is a bucket of apple and one mango is also there in the same bucket and we want to find that is there any mango inside the bucket we can use ‘in’ membership operator. like ‘mango in bucket’ it will return True because there is a mango inside the bucket. Same logic we used in the above code. we store a list of items in a variable named list1=[5,10,15,20,25,30], Now we want to find a particular item from this list of items. like we want to find 15. As we know 15 is present in the list at index 2 of the list1. It will return True.
Identity Operators are used to finding the identity of elements. Identity operators compare the memory locations of two objects. There are two Identity operators ‘is’ and ‘is not‘. As we know every element has a unique id in python. so the identity operator compares that unique id and gives us a response in the form of True and False.
For Example:- is, is not.
first, we store the same constant number in two different variables named x, y. and then we find the unique id of both elements which is the same for both it means python never contains extra space for the same value. it will return the same memory location for both elements. but in the next example of string contain different memory locations and the identity operator return False.
Bitwise operators are used to comparing (binary) numbers: Operator, Name, Description. &, AND, Sets each bit to 1 if both bits are 1.
For Example:- & (bitwise and operator)
|(bitwise or operator)
^(bitwise xor Operator)
~(bitwise not operator)
Looping Statement in Python:
looping Statements are used to execute the same block of code of a specified number of times.
Advantage of loops:
- Reduce The complexity of the program.
- Exicution fast.
- Code Optimization (less number of lines of code).
Generally in python loops are of only two types:
- While loop.
- For Loop.
Syntax of while loop:
#Initialization while(condition): body of loop: increament/ decreament;
i=1 n=int(input("enter any number")) while(i<=10): print(n*i) i+=1;
initialize a variable i=1. And then we used the input function to get a number from the user. If the value of i is less than or equal to the value entered by the user, it will multiply (n* i) and increase the value by one every time until while loop iterates the loop or condition will not be false.
A for loop is used for iterating over a sequence or a group of elements (that is either a list, a tuple, a dictionary, a set, or a string).
Syntax of for loop:
for val in sequence: Body of for;
for x in [1,2,3,4,5,6,7,8]: print (x) #Ouput: 1 2 3 4 5 6 7 8 for x in range(100): print(x) #Output:0 1 2 3 ... ... ... 99
in the above code, we used a for loop to Print every element of a list. In the first example, we provide a list of elements. and in the second example, we provide a range of numbers. This loop will print 1-99 except 100.
Python Pass Statement.
In Python programming, a pass Statement is a null statement the difference in python is that, while the interpreter ignores a comment entirely, Pass is not ignored, But nothing happens when it is executed. It results in no operation(NOP). We generally use it as a placeholder. That is not implemented yet, but we want to implement it in the future they can not have an empty body the interpreter would complain to construct a body that does nothing.
#Empty loop: for val in sequence: pass #Empty function def function (args): pass #empty class: class example: pass
We hope you have understood the concept of all the operators well. thanks for visite us and visit our previous and next articles to learn more.