Q6.
Find the output of the following code:
X=0
Z=1
For I in range(1,3):
For j in range(1,i):
Z = i+j-1
If (z%2==0):
X=x+z
Elif (z%3==0):
X=x+z-2
Print(“x =”,x)
ANS. X = 2
Q11. Wap to find the largest number among the three inputted
Numbers.
ANS. a = int(input("enter the first number")
b = int(input("enter the second number")
c = int(input("enter the third number")
if (a>b):
if (a>c):
print(a," is the largest number")
else :
print(c," is the largest number")
else:
if (b>c):
print(b," is the largest number")
else:
print(c," is the largest number")
Q3. wap that reads two numbers and an arithmetic operator and
displays the results.
Ans. num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
operator = input("Enter the arithmetic operator (+, -, *, /): ")
if operator == '+':
return num1 + num2
elif operator == '-':
return num1 - num2
elif operator == '*':
return num1 * num2
elif operator == '/':
if num2 != 0:
else:
print("Error: Invalid input. Please enter numeric values.")
Q4. Write a program that reads a string and displays the longest
substring of the given string.
Ans. Code:
Str1= input (“enter a string:”)
Word= str1 . split ()
Maxlength=0
Maxword= “ “
For I in word:
X = len (i)
If x>maxlength and i. isalpha()==true:
Print (i)
Maxlength=x
Maxword=i
Print(“substring with maximum length is:”,maxword)
Output
Enter a string : Hello python
Hello
Python
Substring with maximum length is: python
Q5.write a program to check whether the string is a palindrome or
not.
Ans. Code:
Str=input(“enter the string:”)
L=len(str)
P=1-1
Index=0
While (index<p):
If(str[index]==str[p]):
Index=index+1
P=p-1
else:
print(“string is not a palindrome”)
break
else:
print(“string is a palindrome”)
Q1. Wap to remove vowels from string.
Ans. Code:
str1=input("enter the string")
str2=""
for i in range(len(str1)):
if str1[i] not in"ieouaAEIOU":
str2= str2+str1 [i]
print("original string:",str1)
print("new string is :",str2)
output:
enter the string we are learning python
original string: we are learning python
new string is : w r lrnng pythn
Q7.wap to remove ‘I’ from a string.
Ans: code :
def remove_char(s, char_to_remove):
# Replace all occurrences of char_to_remove with an empty string
result = s.replace(char_to_remove, "")
return result
def main():
input_string = input("Enter a string: ")
char_to_remove = 'i' # Character to remove
result = remove_char(input_string, char_to_remove)
print(f"The string after removing '{char_to_remove}' is: {result}")
if __name__ == "__main__":
main()
output:
Enter a string: This is an example
The string after removing 'i' is: Ths s an example
Enter a string: Python is interesting
The string after removing 'i' is: Python s nterestng
Q8. Write a script to partition the string ‘institute’ at the occurrence
of letter ‘T’
Ans. Code:
lower_s = s.lower()
letter = letter.lower()
index = lower_s.find(letter)
if index == -1:
return [s]
return [s[:index], s[index+1:]]
def main():
input_string = 'institute'
letter_to_partition = 'T'
result = partition_at_letter(input_string, letter_to_partition)
print(f"Original string: '{input_string}'")
print(f"Partitioned parts: {result}")
if __name__ == "__main__":
main()
output:
Original string: 'institute'
Partitioned parts: ['in', 'stitute']
Q9. Consider the following code statement.
a = int(input("enter an integer: "))
b = int(input("enter an integer: "))
if a <= 0:
b =b +1
else:
a= a+1
if a > 0 and b >0:
print("w")
elif a > 0:
print("x")
if b > 0:
print("y")
else:
print("z")
write letters will be printed if the user enters 0 or both A and B ?
Ans. Y will be printed
Q10. Read the program given below carefully.
a,b=1,1
print(id(a))
140711006898616
print(id(b)) #statement 1
140711006898616
b=b+a
print(id(b)) #statement 2
140711006898648
Give the appropriate output of statement one and statement 2.
Ans. In the above program for statement a ,B = 1, 1
the same value is assigned to both the variables hence location shall
also be the same in other words both A&B shall be pointing to same
memory address hence
the output shall be:
statement1: 140711006898616
statement2: 140711006898616
Q2. Convert the following for loop into while loop:
i. for k in range(10,20,5):
print(k)
Ans. K=10
while(K<20) :
print(k)
k+=5
ii. max=1
for a in range(1,11)
d=int(input("enter data:"))
if max<d:
max=d
print("maximum=",max)
Ans. A=1
Max=1
While a<11:
D=int(input(“enter data:”))
If max<d:
Max=d
A=a+1
Print(“maximum=”,max)
Q12. Wap accept a decimal number and display its binary number.
Ans. Code:
n= int(input("enter no:"))
a=0
m=0
c=1
while n>0:
a=n%2
m=m+(a*C)
c=c*10
n=int(n/2)
print(m)
output:
Binary representation: 1101