UNIT II - PYTHON BASIC
UNIT II - Python Basic
2.1) Python’s Operators:
Arithmetic Operators,
Comparison Operators,
Assignment Operators,
Logical Operators,
Bitwise Operators,
Membership Operators,
Identity Operators,
Ternary Operator,
Operator precedence.
2.2) Python’s Built-in Data types
String,
List,
Tuple,
Set, Dictionary (characteristics and methods)
Prepared By: [Link] Sayyad
UNIT II - PYTHON BASIC
UNIT II - Python Basic
2.1 Python’s Operators
Operators :
In Python, operators are special symbols used to perform operations
on variables and values.
They are categorized based on the type of operation they perform.
Here’s a list of the main types of operators in Python, along with
explanations and
examples. Operators such as Arithmetic, Comparison (Relational),
Logical, Bitwise, Assignment, Identity, Membership.
[Link] Operators:
Arithmetic operators are used to perform mathematical operations.
Prepared By: [Link] Sayyad
UNIT II - PYTHON BASIC
# Examples of Arithmetic Operator
1) Addition or + : Adds two variables.
Example x = 5
y=5
print(x+y)
output: 10
2) Subtraction Or - : Subtracts the second variable from the first.
Example
x=5
y=2
print (x-y)
output: 3
3) Multiplication Or *: Multiplies two variables.
Example
a=5
b=3
c=a*b
print(c)
output: 15
4) Division Or / : Divides the first variable by the second (result is
float).
Example
a=5
b=5
Prepared By: [Link] Sayyad
UNIT II - PYTHON BASIC
c=a/b
print (c)
output: 1.0
5) Floor Division Or // : Divides the first variable by the second
(result is integer, rounded down).
Example
a=5
b=4
print(a//b)
output : 1
6) Modulus Or % : Returns the remainder when the first operand
is divided by the second. Example
a=5
b=7
print(a%b)
output: 5
Comparison Operators:
Comparison (Relational) Operators (==, !=, > , >= , < ,<= ).
1) > (Greater than):
Checks if the value of the left operand is greater than the value of
the right operand.
Example
a=5
Prepared By: [Link] Sayyad
UNIT II - PYTHON BASIC
b=3
print(a>b)
Output: True
2) < (Less than):
Checks if the value of the left operand is less than the value of the
right operand.
Example
x=5
y=3
print(x<y)
Output: False
3) == (Equal): Checks if the values of two operands are equal.
Example
x=5
y=3
print(x==y)
Output: False
4) != (Not Equal): Checks if the values of two operands are not equal.
Example
x=5
y=3
print(x!=y)
Output: True
Prepared By: [Link] Sayyad
UNIT II - PYTHON BASIC
5) >= (Greater than or Equal to): Checks if the value of the left
operand is greater than or equal to the value of the right operand.
Example
a=5
b=3
print(a>=b)
Output: True
6) <= (Less than or Equal to): Checks if the value of the left operand
is less than or equal to the value of the right operand.
Example
x=5
y=4
print(x<=b)
Output: False
Logical Operators (and, or, not)
Logical operators are used to combine conditional statements.
1) and: Returns True if both statements are true.
Example
a=10
b=12
c=0
if a and b and c:
print("all the numbers have boolean value as true")
else:
Prepared By: [Link] Sayyad
UNIT II - PYTHON BASIC
print("atleast one number has boolean value as false.")
2) or: Returns True if at least one of the statements is true.
Example
a=10
b=12
c=0
if a or b or c:
print("all the numbers have boolean value as true")
else:
print("atleast one number has boolean value as false.")
3) not: Reverses the result, returns False if the result is true.
Example
a=10
b=5
print(not (a>b))
print(not (a==b))
Bitwise operator:
( & , | , ^ , ~ , << , >> )
In python, Bitwise operators perform operations on binary
representations of integers.
1) & (AND): python bitwise and operator returns 1 if both the bits
are 1,otherwise 0.
Example
Prepared By: [Link] Sayyad
UNIT II - PYTHON BASIC
a=5 #Binary: 0101
b=3 #Binary: 0011
print(a&b)
Output: 1 (Decimal)
(binary: 0101 & 0011 = 0001)
2) | (OR): Python bitwise or operator returns 1 if any of the bits is 1.
if both the bits are 0,then it returns 0.
Example
a=5 # Binary: 0101
b=3 # Binary: 0011
print(a|b)
Output: 7 (Decimal)
(binary: 0101 | 0011 = 0111)
3) ^ (XOR): Python bitwise XOR operator returns 1 if one of the bits is
0 and the other bit is 1. if both the bits are 0 or 1,then it returns 0.
Example
a=5 # Binary: 0101
b=3 # Binary: 0011
print(a^b)
Output: 6 (Decimal)
(binary: 0101 ^ 0011 = 0110)
4) ~ (NOT): Inverts all the bits.
Example
b=5 # Binary: 0101
Prepared By: [Link] Sayyad
UNIT II - PYTHON BASIC
print(~b)
output: -6
(binary: ~0101 = 1010)
5) << (Left Shift): Shifts bits to the left, adding zeroes on the right.
Example
a=5 # Binary: 0101
print(a<<1)
Output: 10
(binary: 0101 << 1 = 1010)
6) >> (Right Shift): Shifts bits to the right, discarding bits on the right.
Example
a=5 # Binary : 0101
print(a>>1)
Output: 2 (Decimal)
(binary: 0101 >> 1 = 0010)
Assignment Operators :
(=,+=,-=,*=,/=,%=,//=,**=,&=,|=,<<=, >>= )
Assignment operators are used to assign values to variables.
1) = (Assign): Assigns a value to a variable.
Example x = 5
2) += (Add and Assign): Adds and assigns the result to the left
operand.
Example x += 5 # x = x + 5
Prepared By: [Link] Sayyad
UNIT II - PYTHON BASIC
x=5
print(x)
x=x+5
print(x)
Output: 5
10
3) -= (Subtract and Assign): Subtracts and assigns the result to the
left operand.
Example x -= 5 # x = x – 5
x=5
print(x)
x=x-5
print(x)
Outut: 5
0
4) *= (Multiply and Assign): Multiplies and assigns the result to the
left operand.
Example x *= 5 # x = x * 5
x=5
print(x)
x=x*5
print(x)
output: 5
25
Prepared By: [Link] Sayyad
UNIT II - PYTHON BASIC
5) /= (Divide and Assign): Divides and assigns the result to the left
operand.
Example x /= 5 # x = x / 5
x=5
print(x)
x=x/5
print(x)
Output:
5
1.0
6) %= (Modulus and Assign): Takes modulus and assigns the result to
the left operand.
Example x %= 5 # x = x % 5
x=5
print(x)
x=x%5
print(x)
Output:
5
0
7) //= (Floor Divide and Assign): Floor divides and assigns the result
to the left operand.
Example x //= 5 # x = x // 5
x=5
print(x)
Prepared By: [Link] Sayyad
UNIT II - PYTHON BASIC
x=x//5
print(x)
Output:
5
1
8) **= (Exponent and Assign): Exponentiates and assigns the result
to the left operand.
Example x **= 5 # x = x ** 5
x=5
print(x)
x=x**5
print(x)
Output:
5
3125
9) &= (Bitwise AND and Assign): Performs bitwise AND and assigns
the result.
Example x &= 5 #x=x&5
x=5
print(x)
x=x&5
print(x)
Output:
5
Prepared By: [Link] Sayyad
UNIT II - PYTHON BASIC
5
10) |= (Bitwise OR and Assign): Performs bitwise OR and assigns the
result.
Example x |= 3 #x=x|3
x=5
print(x)
x=x|5
print(x)
Output:
5
5
11) <<= (Left Shift and Assign): Performs left shift and assigns the
result.
Example x <<= 5 # x = x << 5
x=5
print(x)
x=x<<5
print(x)
Output:
5
160
12) >>= (Right Shift and Assign): Performs right shift and assigns the
result.
Example x >>= 5 # x = x >> 5
x=5
Prepared By: [Link] Sayyad
UNIT II - PYTHON BASIC
print(x)
x=x>>5
print(x)
Output:
5
0
Prepared By: [Link] Sayyad