5.Which are the operators available in Python? Explain each with suitable python script.
ANS Operators are used to perform operations on variables and values.
There are 7 type of operators in python :
1) Arithmetic operators
CODE :
n1 = int (input("Enter tne first number: "))
n2 = int (input("Enter tne second number: "))
ans = n1 + n2
print("sum =",ans)
ans = n1 - n2
print("sub =",ans)
ans = n1 * n2
print("mul =",ans)
ans = n1 / n2
print("div =",ans)
ans = n1 % n2
print("mod =",ans)
ans = n1 ** n2
print("expo =",ans)
ans = n1 // n2
print("floor div =",ans)
OUTPUT :
2) Assignment operators
CODE :
x=5#=
print(x)
x = 5 # +=
x += 3
print(x)
x = 5 # -=
x -= 3
print(x)
x = 5 # *=
x *= 3
print(x)
x = 5 # /=
x /= 3
print(x)
x = 5 # %=
x%=3
print(x)
x = 5 # //=
x//=3
print(x)
x = 5 # **=
x **= 3
print(x)
x = 5 # &=
x &= 3
print(x)
OUTPUT :
3) Comparison operators
CODE :
# Comparision operators
x=5
y=3
print(x == y)
# returns False because 5 is not equal to 3
x=5
y=3
print(x != y)
# returns True because 5 is not equal to 3
x=5
y=3
print(x > y)
# returns True because 5 is greater than 3
x=5
y=3
print(x > y)
# returns True because 5 is greater than 3
x=5
y=3
print(x >= y)
# returns True because five is greater, or equal, to 3
x=5
y=3
print(x <= y)
# returns False because 5 is neither less than or equal to 3
OUTPUT:
4) Logical operators
CODE :
x=5
print(x > 3 and x < 10)
# returns True because 5 is greater than 3 AND 5 is less than 10
x=5
print(x > 3 or x < 4)
# returns True because one of the conditions are true (5 is greater
than 3, but 5 is not less than 4)
x=5
print(not(x > 3 and x < 10))
# returns False because not is used to reverse the result.
OUTPUT:
5) Identity operators and Membership operators
CODE : 1) is/not is
x = ["Sanjana"]
y = x # y refers to the same list as x
print(x is y) # True, because x and y point to the same
list object
print(x is not y) # False, because x and y are the same
object
z = ["Sanjana"]
print(x is z) # False, even though the content is the
same, they are different objects.
OUTPUT:
2) in/not in
CODE:
my_list = ["FOP","MM","ITTS"]
print("FOP" in my_list) # True, because 20 is present in
my_list
print("MM" in my_list) # False, because 25 is not present
in my_list
print("Sanjana" not in my_list) # False, because 40 is
present in my_list
print("ITTS" not in my_list) # True, because 35 is not
present in my_list
OUTPUT:
6) Bitwise operators
CODE :
# Bitwise AND
a=5 # 0101 in binary
b=3 # 0011 in binary
result_and = a & b
print(result_and) # Output: 1 (0001 in binary)
# Bitwise OR
result_or = a | b
print(result_or) # Output: 7 (0111 in binary)
# Bitwise XOR
result_xor = a ^ b
print(result_xor) # Output: 6 (0110 in binary)
# Bitwise NOT
c = 10 # 1010 in binary
result_not = ~c
print(result_not) # Output: -11 (Flipping the bits of 1010)
# Left Shift
d=8 # 1000 in binary
result_left_shift = d << 2
print(result_left_shift) # Output: 32 (100000 in binary)
# Right Shift
e = 16 # 10000 in binary
result_right_shift = e >> 2
print(result_right_shift) # Output: 4 (0001 in binary)
OUTPUT:
6. Write a note on conditional statements and their advantages.Explain if-elif with example
(Find minimum number among given 3 numbers)
ANS Conditional statements (if, else, and elif) are fundamental programming
constructs that allow you to control the flow of your program based on conditions
that you specify.
Advantages of conditional statements :-
1. Control Flow: Direct how your program behaves based on conditions.
2. Decision Making: Choose actions based on evaluations.
3. Flexibility: Handle diverse scenarios and changing data.
4. Modularity: Organize code into manageable blocks.
5. Error Handling: Implement error prevention and handling.
6. User Interaction: Respond to user input dynamically.
7. Dynamic Behavior: Adapt behavior as conditions change.
8. Readability: Enhance code clarity for others and yourself.
9. Efficiency: Optimize code execution based on conditions.
10. Logic Implementation: Implement complex logical operations.
CODE :
n1 = 1
n2 = 23
n3 = 11
if (n1<n2) and (n1<n3):
print (n1,"is smallest number")
elif (n2<n3):
print (n2,"is smallest number")
else:
print (n3,"is smallest number")
OUTPUT :
7. What is string? Describe any 5 string functions with example.
ANS String is sequence of character .
Examples
1) Escape sequence
2) is alpa or numeric
3) Translate function
4) split function
5) Find function
8. Write a python script to print the first 20 even numbers.
CODE :
count = 0
num = 2 # Start with the first even number
while count < 20:
print(num)
num += 2 # Move to the next even number
count += 1
OUTPUT :
9. Write a python script to input two names and check whether both are equal or not.
CODE :
name = input('Enter your name :')
def compare(a, b):
if a == b:
return True
return False
x = int(input("Enter First Number : "))
y = int(input("Enter Second Number : "))
result = compare(x, y)
if result == True:
print("Two numbers are equal")
else:
print("Two numbers are not equal")
OUTPUT:
11. Write a python script to swap two numbers.
CODE:
name = input('Enter your name :')
num1 = input('Enter First Number: ')
num2 = input('Enter Second Number: ')
temp = num1
num1 = num2
num2 = temp
print("Value of num1 after swapping: ", num1)
print("Value of num2 after swapping: ", num2)
OUTPUT: