Python Operators

Python Operators

Operators in Python are special symbols or keywords that perform operations on variables and values. They are used to perform arithmetic, comparison, logical, and bitwise operations, among others.

Types of Operators:

1. Arithmetic Operators
Arithmetic operators are used to perform mathematical operations.

Operator Description Example Explanation Result
+ Addition 5 + 3 Addition of 5 and 3 8
Subtraction 7 – 2 Subtraction of 7 and 3 5
* Multiplication 4 * 6 Multiplication of 4 and 6 26
/ Division 10 / 2 Division of 10 and 2 5
// Floor Division (integer division) 5 // 3 Divide and returns only integer part of quotient 1
% Modulus (remainder) 10 % 3 Divides and returns remainder 1
** Exponentiation 5 ** 3 5 to the power of 3 i.e. 53 125

2. Comparison Operators
Comparison operators are used to compare two values.

Operator Description Example Result
== Equal to 5 == 3 FALSE
!= Not equal to 5 != 3 TRUE
> Greater than 5 > 3 TRUE
< Less than 5 < 3 FALSE
>= Greater than or equal to 5 >= 3 TRUE
<= Less than or equal to 5 <= 3 FALSE

3. Logical Operators
Logical operators are used to combine conditional statements.

Operator Description Example Explanation Result
and Logical AND (5 > 3) and (3 > 1) when both conditions are met TRUE
or Logical OR (5 > 3) or (3 < 1) when either of conditions are met TRUE
not Logical NOT not (5 > 3) Inverts the output FALSE

4. Assignment Operators
Assignment operators are used to assign values to variables.

Operator Description Example Explanation Result
`= Assign x = 5 variable x is assigned value 5 x is 5
`+= Add and assign x += 1 x is assigned x + 1 i.e. x=x+1 x is 8
-= Subtract and assign x -= 2 x is assigned x – 2 i.e. x=x-2 x is 3
*= Multiply and assign x *= 4 x is assigned x * 4 i.e. x=x*4 x is 20
/= Divide and assign x /= 2 x is assigned x / 2 i.e. x=x/2 x is 10
%= Modulus and assign x %= 3 x is assigned x % 3 i.e. x=x%3 x is 1
**= Exponentiate and assign x **= 2 x is assigned x ** 3 i.e. x=x**3 x is 25
//= Floor divide and assign x //= 3 x is assigned x // 3 i.e. x=x//3 x is 8

5. Membership Operators
Membership operators are used to test if a value is in a sequence.

Operator Description Example Result
in Checks if value is present in the sequence 5 in [1, 2, 3, 5] TRUE
not in Checks if value is not present in the sequence 5 not in [1, 2, 3] TRUE

6. Identity Operators
Identity operators are used to compare the memory location of two objects.

Operator Description Example Result
is Checks if two variables refer to the same object x = 7 y = x x is y TRUE
is not Checks if two variables do not refer to the same object x = 7 y = 7 x is not y TRUE

7. Ternary Operator
Note: Ternary operator is different from if else conditional statement even if they both contain ‘if-else keyword’
Syntax: true_value if condition else false_value
Example:
x = 15
y = 25
# python ternary operator
min = “x is minimum” if x < y else “y is minimum”

Example of Arithmetic Operator:

x = 10
y = 2
print(f”Addition:{x+y}”) #Output: Addition:12
print(f”Subtraction:{x-y}”) #Output: Subtraction:8
print(f”Multiplication:{x*y}”) #Output: Multiplication:20
print(f”Division:{x/y}”) #Output: Division:5.0
print(f”Remainder:{x%y}”) #Output: Remainder:0
print(f”Floor division:{x//y}”) #Output: Floor Division:5
print(f”Exponential:{x**y}”) #Output: Exponential: 100

Example of Comparison Operator:

x = 10
y = 2
print(f”{x==y}”) #Output:False
print(f”{x!=y}”) #Output:True
print(f”{x>y}”) #Output:True
print(f”{x<y}”) #Output:False
print(f”{x>=y}”) #Output:True
print(f”{x<=y}”) #Output:False

Example of Logical Operator:

#Logical Operator
x = True
y = False
a = 10
b = 20
print(f”{x and y}”) #Output:False
print(f”{x or y}”) #Output:True
print(f”{not y}”) #Output:True

print(f”{a<b and a==b}”) #Output:False
print(f”{a<b or a==b}”) #Output:True

Example of Assignment Operator

x = 10 #Assign
print(x) #Output: 10
x+=1 #Add and Assign
print(x) #Output: 11
x-=1 #Subtract and Assign
print(x) #Output: 10
x*=2 #Multiply and Assign
print(x) #Output: 20
x/=2 #Divide and Assign
print(x) #Output: 10
x**=2 #Exponentiate and Assign
print(x) #Output: 100
x//=2 #Floor divide and Assign
print(x) #Output: 10
x%=2 #Modulus and Assign
print(x) #Output: 0

Examples of Identity Operator:

a=10
b=10
c=20
print(a is b) # Output: True
print(a is c) # Output: False
print(a is not b) # Output: False
print(a is not c) # Output: True

Examples of Ternary Operator:
Example 1:

marksofenglish = 10
marksofscience = 20
result = marksofenglish if marksofenglish > marksofscience else marksofscience
print(result) #Output: 20

Explanation:
● marksofenglish = 10: Initializes an integer variable with a value of 10.
● marksofscience = 20: Initializes another integer variable with a value of 20.
● result = marksofenglish if marksofenglish > marksofscience else marksofscience:
This line uses a conditional (ternary) operator to determine the larger of the two values. If marksofenglish is greater than marksofscience, result is set to marksofenglish, otherwise, it is set to marksofscience.
● print(result): Outputs the value of result to the console. In this case, it prints 20.

Example 2:

age = 20
status = “Adult” if age >= 18 else “Minor”
print(status) #Output: Adult

Explanation:
● age = 20: Declares an integer variable age with a value of 20.
● status = “Adult” if age >= 18 else “Minor”: Uses a condition to check if the person is an adult or minor. If the age is greater than or equal to 18, the status is set to “Adult”, otherwise, it is “Minor”.
● print(status): Outputs the status to the console. In this case, it will print “Adult”.

Example 3:

privatecustomer = False
value = 1 if privatecustomer else 0
print(value) #Output: 0

Explanation:
● privatecustomer = False: Initializes a Boolean variable privatecustomer with the value False.
● value = 1 if privatecustomer else 0: Checks if privatecustomer is True. If it is True, value is set to 1; otherwise, it is set to 0.
● print(value): Outputs the value of value to the console. In this case, it will print 0.

Example 4:

Set default value if the value is None or False using ‘or’

#Scenario A
input_value = None
result = input_value or “Hello World”
print(result)
#Output: Hello World

#Scenario B
input_value = 7
result = input_value or “Hello World”
print(result)
#Output: 7

Explanation:
Scenario A:
● input_value = None: Declares a string variable input_value and sets it to None.
● result = input_value or “Hello World”: Uses the or operator to check if input_value is None or falsy. If it is, the result is assigned “Hello World”. Otherwise, result gets the value of input_value.
● print(result): Outputs the value of result. Since input_value is None, it will print “Hello World”.

Scenario B:
● input_value = 7: Declares a string variable input_value and sets it to 7.
● result = input_value or “Hello World”: Uses the or operator to check if input_value is None or falsy. If it is, result is assigned “Hello World”. Otherwise, result gets the value of input_value. In this case it is not none so result = input_value = 7
● print(result): Outputs the value of result. Since input_value is 7, it will print “7”.

Task:
1. Arithmetic Operators
Write a Python program to perform the following operations and print the results:
Addition: 8 + 4
Subtraction: 15 – 7
Multiplication: 6 * 9
Division: 20 / 4
Modulus: 19 % 4

Create a variable num with a value of 10. Use the increment operator to increase its value by 1, then print the result. Next, use the decrement operator to decrease its value by 1, and print the result.

2. Comparison Operators
Write a Python program to compare two integers, a = 15 and b = 20. Use all the comparison operators (==, !=, >, <, >=, <=) and print the results of each comparison.

3. Logical Operators
Create two boolean variables, isStudent and isMember. Set isStudent to true and isMember to false. Write a Python program to evaluate and print the following logical expressions:

isStudent and isMember
isStudent or isMember
not isStudent

4. Assignment Operators
Start with an integer variable x initialized to 10. Use the assignment operators to perform the following operations and print the value of x after each step:
Add 5 to x
Subtract 3 from x
Multiply x by 2
Divide x by 4
Calculate the modulus of x by 3

5. Conditional Operator
Write a Python program using the conditional operator to:
1. Compare two integers a = 8 and b = 12, and print which one is greater.
2. Check if a given number num = 7 is even or odd and print the result.
3. Determine if a person with age = 16 is an adult or a minor and print the result.
4. Assign a value to an integer variable value based on a boolean variable isValid
5. Create a string variable name that is null. Use the null operator to assign a default value “Unknown” if name is null, and print the result

Course Video

YouTube Reference :