PYTHON OPERATORS

Python Operators

PYTHON OPERATORS IMAGE

Operators are special symbols that perform operations on variables and values.

Example:

print(5 + 6)   # 11

Here, + is an operator that adds two numbers: 5 and 6.

Types of Python Operators

There are 7 types of Python operators...

1. Arithmetic Operators

2. Assignment Operators

3. Comparison Operators

4. Logical Operators

5. Bitwise Operators

6. Identity Operators

7. Membership Operators

1. Python Arithmetic Operators

Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, etc.

Example:

sub = 10 - 5 # 5

Here, - is an arithmetic operator that subtracts two values or variables.

2. Python Assignment Operators

Assignment operators are used to assign values to variables. For example,

# assign 5 to x 

var x = 5

Here, = is an assignment operator that assigns 5 to x.

3. Python Comparison Operators

Comparison operators compare two values/variables and return a boolean result: True or False.

Example:

a = 5

b =2

print (a > b)    # True

4. Python Logical Operators

Logical operators are used to check whether an expression is True or False. They are used in decision-making. For example,

a = 5

b = 6

print((a > 2) and (b >= 6))    # True

5. Python Bitwise Operators

Bitwise operators act on operands as if they were strings of binary digits. They operate bit by bit, hence the name.

Example:

2 is 10 in binary, and 7 is 111.

6. Identity Operators

Identity operators are used for locating the memory unit of the objects, especially when both objects have the same name and can be differentiated only using their memory location.

Types of Identity Operators

There are two types of identity operators...

1. Is Operator

2. Is Not Operator

Is Operator

Is operator is used for comparing if the objects are in the same location while returning ‘true’ or ‘false’ values as a result.

Example:

m = 70

n = 70

if ( m is n ):

   print("Result: m and n have same identity")

else:

   print("Result: m and n do not have same identity")

Output: m and n have same identity

2. Is Not Operator

Is Not operator works in the opposite way of is operator? This returns true if the memory location of two objects is not the same. This returns False if the memory location of two objects is the same.

Example:

m = 70

n = 70

if ( m is not n ):

   print("Result: m and n have same identity")

else:

   print("Result: m and n do not have same identity")

Output: m and n do not have same identity

7. Membership Operators

We use membership operators to check whether a value or variable exists in a sequence (string, list, tuples, sets, dictionary) or not. Python has two membership operators: in and not in. Both return a Boolean result. The result of in operator is opposite to that of not in operator.

Example:

var = "TotalSach"

a = "s"

b = "tal"

c = "ac"

d = "Al"

print (a, "in", var, ":", a in var)

print (b, "not in", var, ":", b not in var)

print (c, "in", var, ":", c in var)

print (d, "not in", var, ":", d not in var)

Output:

s in TotalSach : True

tal not in TotalSach : False

ac in TotalSach : True

Al not in TotalSach : True

ALSO READ:

6. CONDITIONAL STATEMENTS

7. LOOPING

8. CONTROL STATEMENTS, STRING MANIPULATION, LISTS, TUPLE, SETS

9. DICTIONARIES

10. ARRAYS

11. ITERATORS, MODULES, DATES, MATH

12. MODULES, INPUT AND OUTPUT

13. FUNCTION & ARGUMENTS

14. MODULES

15. EXCEPTION HANDLING

16. BUILT IN FUNCTION IN PYTHON

17. FILE HANDLING IN PYTHON

18. PYTHON ARCHITECTURE

19. DOCUMENTATION IN PYTHON


Total सच

एक टिप्पणी भेजें

और नया पुराने