Python Interview Questions
Python Interview Questions
Exact Output
enter values2 4 3 2 7 2
enter value to be searched2
Position: 0
Position: 3
Position: 5
count is: 3
Answer:
Python code:
1. l = input('Enter values: ').split(" "): This line splits the input values into a
list.
2. a = input('Enter value to be searched: '): This line reads the value to be
searched.
3. The for loop iterates through the list l.
4. Inside the loop, if a == l[i]: checks if the current element matches the value to be
searched.
5. If a match is found, it prints the position of the match.
6. After the loop, print('Count is:', l.count(a)) prints the total count of
occurrences of the value in the list.
Program 2:
i=1
s=0
while i==10:
s=s+1
i=i+1
avg=s/10
print("the sum of first 10 numbers is",s)
print("the average of first 10 numbers is", avg)
Exact Output
the sum of first 10 numbers is 10
the average of first 10 numbers is 1
Answer:
Python code:
i = 1
s = 0
while i <= 10:
s = s + i
i = i + 1
avg = s / 10
print("The sum of the first 10 numbers is", s)
print("The average of the first 10 numbers is", avg)
Python code:
Program 3:
def count_ch(s,c):
count=0
for i in s:
if i ==c:
count+=1
return count
string=input("\n Enter a string ")
c=int(input("\n Enter character to search "))
count=count_ch(string,c)
print("In",string,c,"occurs ",count,"times")
Exact output
Enter a string lovely flowers
Enter character to search o
In lovely flowers o occurs 2 times
Answer:
Python code:
count = count_ch(string, c)
print(f"In '{string}', '{c}' occurs {count} times")
1. def count_ch(s, c):: Defines a function count_ch that takes a string s and a
character c.
2. count = 0: Initializes the count variable.
3. for i in s:: Loops through each character in the string s.
4. if i == c:: Checks if the current character i is equal to the character c.
5. count += 1: Increments the count if the character matches.
6. return count: Returns the final count.
7. string = input("\nEnter a string: "): Gets the string input from the user.
8. c = input("\nEnter character to search: "): Gets the character to search
from the user.
9. count = count_ch(string, c): Calls the function count_ch with the string and
character as arguments.
10. print(f"In '{string}', '{c}' occurs {count} times"): Prints the result
using an f-string for better readability.
This corrected code will produce the desired output:
Sql code:
Program 4:
def check_relation(a,b):
if a==b:
return 0
if a>b:
return 1
if a<b:
return -1
check_relation(3,5)
if res=='0':
print("a is equal to b")
if res=='1':
print("a is greater than b")
if res=='-1':
print("a is less than b")
Exact output
a is less than b
Answer:
1. The check_relation function is called, but its result is not stored in a variable.
2. The return values from check_relation are integers, not strings, so the comparison
should be made with integers, not strings.
3. Proper indentation and spacing are necessary.
Python code:
if res == 0:
print("a is equal to b")
elif res == 1:
print("a is greater than b")
elif res == -1:
print("a is less than b")
Css code:
a is less than b
Program 5:
for i in range(1,6):
print(i,end="")
for j in range(1,6):
print(j,end="")
print()
Exact output
12345
12345
12345
12345
12345
Answer:
The original code is almost correct, but the placement of the print() statement for a new
line is incorrect. It should be placed inside the outer loop but outside the inner loop to ensure
that a new line is printed after each row of numbers.
Python code:
Copy code
12345
12345
12345
12345
12345
Program 6:
# don’t add new line or don’t delete any line in this program
for i in range(1,10):
print(i,end="")
Exact output
13579
Answer:
To achieve the exact output 13579, we need to print only the odd numbers between 1 and 9.
The original loop iterates over all numbers in the range and prints them, but we need to add a
condition to print only the odd numbers.
Here is the corrected code:
Python code:
# don’t add new line or don’t delete any line in this program
for i in range(1, 10):
if i % 2 != 0: # Check if the number is odd
print(i, end="")
Explanation:
13579
Program 7:
string="python is easy"
yes=” “
print(yes+string[:-1])
Exact output
yes python is easy
Answer:
Python code:
Bash code:
To achieve the exact output yes python is easy, there is no need to exclude the last
character. Here is the corrected code for the exact desired output:
Python code:
Bash code:
Program 8:
# Don’t remove any ‘+’ symbol from this program
a=float(input("enter any floating point value"))
print("The integer value of "+a+"="+int(a))
Exact output
enter any floating point value4.3
The integer value of 4.3=4
Answer:
The error in the code is that the + operator is used for string concatenation, but a and int(a)
are not strings. You need to convert a and int(a) to strings before concatenating them with
other strings.
Python code:
Arduino code:
Program 9:
max1=0
max2=0
n=int(input("Enter the number of element in the list"))
l=[]
for i in range(0,n):
L.append(int(input("Enter the number")))
for i in range(0,len(L)):
if L[i]>max1:
max1=max2
max1=L[i]
elif L[i]>max2 and L[i]<max1:
max1=L[i]
print("First max",max1)
print("Second max",max2)
Exact output
Enter the number of element in the list10
Enter the number10
Enter the number90
Enter the number90
Enter the number90
Enter the number90
Enter the number90
Enter the number90
Enter the number80
Enter the number70
Enter the number60
First max 90
Second max 80
Answer:
1. The list l is initialized, but the code attempts to append values to L (case-sensitive
issue).
2. The indentation of the if and elif blocks is incorrect.
3. The logic to update max2 is incorrect.
4. The elif block incorrectly updates max1 instead of max2.
Python code:
max1 = 0
max2 = 0
n = int(input("Enter the number of elements in the list: "))
l = []
for i in range(n):
l.append(int(input("Enter the number: ")))
for i in range(len(l)):
if l[i] > max1:
max2 = max1
max1 = l[i]
elif l[i] > max2 and l[i] < max1:
max2 = l[i]
Yaml code:
Program 10:
n=int(input("Enter value of n : "))
for i in range(n//2,n+1,2):
for j in range(1,n-i,2):
print(" ",end="")
for j in range(1,i+1) :
print("*")
for j in range(1,n-i):
print(" ",end="")
for j in range(1,i):
print("*",end="")
print("\n")
for i in range(n,0,-1):
for j in range(i,n):
print(" ",end="")
for j in range(1,((i*2)-1)):
print("*",end="")
print("\n")
Exact output
Enter value of n: 10
***** *****
******* *******
********* *********
*******************
*****************
***************
*************
***********
*********
*******
*****
***
*
Answer:
The issue in the original code lies mainly in the indentation and the way loops are structured
to produce the desired pattern. Additionally, there are some inconsistencies with how spaces
and asterisks are printed. Here's the corrected code that should produce the exact output you
specified:
Python code:
This corrected code will produce the exact output pattern for n = 10 as specified in your
example.