PYTHON
CLASS: XI
COMPUTER SCIENCE(083)
OPERATORS IN PYTHON
PART-1
OPERATORS:
The operator can be defined as a symbol which is responsible for a particular
operation between two operands. Operators are used to perform operations
on variables and values.
The operator can be defined as a symbol which is responsible for a particular
operation between two operands. Operators are used to perform operations
on variables and values.
Arithmetic operators
Comparison operators
Assignment Operators
Logical Operators
Membership Operators
Identity Operators
Types of Operators
Arithmetic Operators
Arithmetic operators are used with numeric values to perform common
mathematical operations:
Operator Name Example
+ Addition x + y
- Subtraction x - y
* Multiplication x * y
/ Division x / y
// Floor division[Integer Division] x // y
** Exponentiation x ** y
% Modulus x % y
Arithmetic Operators
+ Addition x + y
Example 1:
A=10
B=20
C=A+B
print(“A+B=“,C)
--Output---
A+B=30
Example 2:
A=10
B=20
--Output---
A+B=30
print(“A+B=“,A+B)
Example 3:
A=int(input(“Enter value A:”))
print(“A+B=“,C)
B=int(input(“Enter value B:”))
C=A+B
--Output---
Enter value A: 5
Enter value B: 4
A+B=9
Arithmetic Operators
Example 1:
A=20
B=10
C=A-B
print(“A-B=“,C)
--Output---
A-B=10
Example 2:
A=30
B=20
--Output---
A-B=10
print(“A-B=“,A-B)
Example 3:
A=int(input(“Enter value A:”))
print(“A-B=“,C)
B=int(input(“Enter value B:”))
C=A-B
--Output---
Enter value A: 5
Enter value B: 4
A-B=1
- Subtraction x - y
Arithmetic Operators
Example 1:
A=2
B=10
C=A*B
print(“A*B=“,C)
--Output---
A*B=20
Example 2:
A=3
B=2
--Output---
A*B=6
print(“A*B=“,A*B)
Example 3:
A=int(input(“Enter value A:”))
print(“A*B=“,C)
B=int(input(“Enter value B:”))
C=A*B
--Output---
Enter value A: 5
Enter value B: 4
A*B=20
* Multiplication x * y
Arithmetic Operators
Example 1:
A=27
B=5
C=A/B
print(“A/B=“,C)
--Output---
A/B=5.4
Example 2:
A=27
B=5
--Output---
A//B=5
/ [Division] x / y // Floor division[Integer Division] x // y
C=A//B
print(“A//B=“,C)
Arithmetic Operators
Example 1:
A=27
B=5
C=A%B
print(“A%B=“,C)
--Output---
A%B=2
Example 2:
A=int(input(“Enter the value of A:”))
--Output---
Enter the value of A: 4
Enter the value of B: 2
A%B=0
% [Modulus] x % y : It return remainder value
C=A%B
print(“A%B=“,C)
B=int(input(“Enter the value of B:”))
Arithmetic Operators
Example 1:
A=2
B=3
C=A**B
print(“A**B=“,C)
--Output---
A**B=8
Example 2:
A=int(input(Enter the value of A”))
B=int(input(“Enter the power”))
--Output---
Enter the value of A: 7
Enter the power: 2
A**B=49
C=A**B
print(“A**B=“,C)
** Exponentiation x ** y
Meaning of A**B
AB
Precedence of Arithmetic Operators
Highest
Lowest
() parentheses
** (Exponentiation)
/(division) // (integer division) * ( multiply)
% (modulas)
+ (addition) - ( subtraction)
Expressions:
An expression is a combination of variable and operators. It generates a
single value, which by itself is an expression.
Example:
5 + 2 * 4 and the result is 13
Q. Convert the Mathematical Expressions to equivalent python expressions
Algebraic Expression Python Programming Expression
5A 5 * A
4ab 4 * a * b
A2 + B2 A**2 + B **2
a=(x+y)/(x-y)
20 + 30 * 40
Evaluate the expressions:
1
2
20 + 120
140
20 - 30 + 40
1
2
-10 + 40
30
The two operators (–) and (+)
have equal precedence. Thus,
the first operator, i.e.,
subtraction is applied before
the second operator, i.e.,
addition (left to right).
Evaluate the expressions:
12 + 3 * 4 – 6/2
1
3
4
12 + 12 – 6/2
12 + 12 – 3.0
2
24– 3.0
21.0
(12 + 3) * 4 – 6//2
1
2
4
3 15 * 4 – 6//2
60 – 6//2
60 – 3
57
+ ( plus) and (*) sign for string
+ ( plus) : It is use to concatenation of string and in string case + (plus)
sign is known as concatenation operator.
Example:
If there are two variable A=‘WELCOME’ and B=‘PYTHON’ and we want
to join these two strings.
A=‘WELCOME’
B=‘PYTHON’
print(A+B)
---Output----
WELCOMEPYTHON
It display join or concatenation of
two string but without any gap
+ ( plus) and (*) sign for string
* (multiply) : It is use to repeat the string as many times we need by
multiplying the string with number given.
Example:
If variable A=‘WELCOME’ and we want to display WELCOME 3 times
A=‘WELCOME’
print(A*3)
---Output----
WELCOMEWELCOMEWELCOME

OPERATOR IN PYTHON-PART1

  • 1.
  • 2.
    OPERATORS: The operator canbe defined as a symbol which is responsible for a particular operation between two operands. Operators are used to perform operations on variables and values. The operator can be defined as a symbol which is responsible for a particular operation between two operands. Operators are used to perform operations on variables and values. Arithmetic operators Comparison operators Assignment Operators Logical Operators Membership Operators Identity Operators Types of Operators
  • 3.
    Arithmetic Operators Arithmetic operatorsare used with numeric values to perform common mathematical operations: Operator Name Example + Addition x + y - Subtraction x - y * Multiplication x * y / Division x / y // Floor division[Integer Division] x // y ** Exponentiation x ** y % Modulus x % y
  • 4.
    Arithmetic Operators + Additionx + y Example 1: A=10 B=20 C=A+B print(“A+B=“,C) --Output--- A+B=30 Example 2: A=10 B=20 --Output--- A+B=30 print(“A+B=“,A+B) Example 3: A=int(input(“Enter value A:”)) print(“A+B=“,C) B=int(input(“Enter value B:”)) C=A+B --Output--- Enter value A: 5 Enter value B: 4 A+B=9
  • 5.
    Arithmetic Operators Example 1: A=20 B=10 C=A-B print(“A-B=“,C) --Output--- A-B=10 Example2: A=30 B=20 --Output--- A-B=10 print(“A-B=“,A-B) Example 3: A=int(input(“Enter value A:”)) print(“A-B=“,C) B=int(input(“Enter value B:”)) C=A-B --Output--- Enter value A: 5 Enter value B: 4 A-B=1 - Subtraction x - y
  • 6.
    Arithmetic Operators Example 1: A=2 B=10 C=A*B print(“A*B=“,C) --Output--- A*B=20 Example2: A=3 B=2 --Output--- A*B=6 print(“A*B=“,A*B) Example 3: A=int(input(“Enter value A:”)) print(“A*B=“,C) B=int(input(“Enter value B:”)) C=A*B --Output--- Enter value A: 5 Enter value B: 4 A*B=20 * Multiplication x * y
  • 7.
    Arithmetic Operators Example 1: A=27 B=5 C=A/B print(“A/B=“,C) --Output--- A/B=5.4 Example2: A=27 B=5 --Output--- A//B=5 / [Division] x / y // Floor division[Integer Division] x // y C=A//B print(“A//B=“,C)
  • 8.
    Arithmetic Operators Example 1: A=27 B=5 C=A%B print(“A%B=“,C) --Output--- A%B=2 Example2: A=int(input(“Enter the value of A:”)) --Output--- Enter the value of A: 4 Enter the value of B: 2 A%B=0 % [Modulus] x % y : It return remainder value C=A%B print(“A%B=“,C) B=int(input(“Enter the value of B:”))
  • 9.
    Arithmetic Operators Example 1: A=2 B=3 C=A**B print(“A**B=“,C) --Output--- A**B=8 Example2: A=int(input(Enter the value of A”)) B=int(input(“Enter the power”)) --Output--- Enter the value of A: 7 Enter the power: 2 A**B=49 C=A**B print(“A**B=“,C) ** Exponentiation x ** y Meaning of A**B AB
  • 10.
    Precedence of ArithmeticOperators Highest Lowest () parentheses ** (Exponentiation) /(division) // (integer division) * ( multiply) % (modulas) + (addition) - ( subtraction)
  • 11.
    Expressions: An expression isa combination of variable and operators. It generates a single value, which by itself is an expression. Example: 5 + 2 * 4 and the result is 13 Q. Convert the Mathematical Expressions to equivalent python expressions Algebraic Expression Python Programming Expression 5A 5 * A 4ab 4 * a * b A2 + B2 A**2 + B **2 a=(x+y)/(x-y)
  • 12.
    20 + 30* 40 Evaluate the expressions: 1 2 20 + 120 140 20 - 30 + 40 1 2 -10 + 40 30 The two operators (–) and (+) have equal precedence. Thus, the first operator, i.e., subtraction is applied before the second operator, i.e., addition (left to right).
  • 13.
    Evaluate the expressions: 12+ 3 * 4 – 6/2 1 3 4 12 + 12 – 6/2 12 + 12 – 3.0 2 24– 3.0 21.0 (12 + 3) * 4 – 6//2 1 2 4 3 15 * 4 – 6//2 60 – 6//2 60 – 3 57
  • 14.
    + ( plus)and (*) sign for string + ( plus) : It is use to concatenation of string and in string case + (plus) sign is known as concatenation operator. Example: If there are two variable A=‘WELCOME’ and B=‘PYTHON’ and we want to join these two strings. A=‘WELCOME’ B=‘PYTHON’ print(A+B) ---Output---- WELCOMEPYTHON It display join or concatenation of two string but without any gap
  • 15.
    + ( plus)and (*) sign for string * (multiply) : It is use to repeat the string as many times we need by multiplying the string with number given. Example: If variable A=‘WELCOME’ and we want to display WELCOME 3 times A=‘WELCOME’ print(A*3) ---Output---- WELCOMEWELCOMEWELCOME