Interview Question Pythons 2
Interview Question Pythons 2
A) The <yield> keyword in Python can turn any function into a generator. Yields work like a
standard return keyword.
But it’ll always return a generator object. Also, a function can have multiple calls to the
<yield> keyword.
Example:
def testgen(index):
weekdays = ['sun','mon','tue','wed','thu','fri','sat']
yield weekdays[index]
yield weekdays[index+1]
day = testgen(0)
print next(day), next(day)
A) When we want to convert a list into a string, we can use the <”.join()> method which joins
all the elements into one and returns as a string.
Example:
weekdays = ['sun','mon','tue','wed','thu','fri','sat']
listAsString = ' '.join(weekdays)
print(listAsString)P
A) By using Python <tuple()> function we can convert a list into a tuple. But we can’t change
the list after turning it into tuple, because it becomes immutable.
Example:
weekdays = ['sun','mon','tue','wed','thu','fri','sat']
listAsTuple = tuple(weekdays)
print(listAsTuple)
Example:
weekdays = ['sun','mon','tue','wed','thu','fri','sat','sun','tue']
listAsSet = set(weekdays)
print(listAsSet)
A) In Python list, we can count the occurrences of an individual element by using a <count()>
function.
Example # 1:
weekdays = ['sun','mon','tue','wed','thu','fri','sun','mon','mon']
print(weekdays.count('mon'))
Output: 3
Example # 2:
weekdays = ['sun','mon','tue','wed','thu','fri','sun','mon','mon']
print([[x,weekdays.count(x)] for x in set(weekdays)])
output: [[‘wed’, 1], [‘sun’, 2], [‘thu’, 1], [‘tue’, 1], [‘mon’, 3], [‘fri’, 1]]
A) NumPy arrays are more flexible then lists in Python. By using NumPy arrays reading and
writing items is faster and more efficient.
1) import numpy
numpy.array([])
2) numpy.empty(shape=(0,0))
A) Python has a special feature like a negative index in Arrays and Lists. Positive index reads
the elements from the starting of an array or list but in the negative index, Python reads
elements from the end of an array or list.
Output:
0 Python
1 Interview
2 Questions
13) What is data type SET in Python and how to work with it?
A) The Python data type “set” is a kind of collection. It has been part of Python since version
2.4. A set contains an unordered collection of unique and immutable objects.
# Print set.
print(objects)
print(len(objects))
print(items)
# ** Output
{'Python', 'coding', 'tips'}
# Output:
A) We can generate random numbers using different functions in Python. They are:
#1. random() – This command returns a floating point number, between 0 and 1.
#2. uniform(X, Y) – It returns a floating point number between the values given as X and Y.
#3. randint(X, Y) – This command returns a random integer between the values given as X
and Y.
A) We can print sum of the numbers starting from 1 to 100 using this code:
print sum(range(1,101))
# In Python the range function does not include the end given. Here it will exclude 101.
# Sum function print sum of the elements of range function, i.e 1 to 100.
17) How do you set a global variable inside a function?
A) Yes, we can use a global variable in other functions by declaring it as global in each
function that assigns to it:
globvar = 0
def set_globvar_to_one():
global globvar # Needed to modify global copy of globvar
globvar = 1
def print_globvar():
print globvar # No need for global declaration to read value of globvar
set_globvar_to_one()
print_globvar() # Prints 1
names2[0] = 'Alice'
names3[1] = 'Bob'
sum = 0
for ls in (names1, names2, names3):
if ls[0] == 'Alice':
sum += 1
if ls[1] == 'Bob':
sum += 10
print sum
A) 12
19) What is the output, Suppose list1 is [1, 3, 2], What is list1 * 2?
A) [1, 3, 2, 1, 3, 2]
21) Can you write a program to find the average of numbers in a list in Python?
Output:
23) Write a program to find the sum of the digits of a number in Python?
n=int(input("Enter a number:"))
tot=0
while(n>0):
dig=n%10
tot=tot+dig
n=n//10
print("The total sum of digits is:",tot)
Output:
Enter a number:1928
The total sum of digits is: 20
n=int(input("Enter number:"))
temp=n
rev=0
while(n>0):
dig=n%10
rev=rev*10+dig
n=n//10
if(temp==rev):
print("The number is a palindrome!")
else:
print("The number isn't a palindrome!")
Output:
Enter number:151
The number is a palindrome!
n=int(input("Enter number:"))
count=0
while(n>0):
count=count+1
n=n//10
print("The number of digits in the number is:",count)
Output:
Enter number:14325
The number of digits in the number is: 5
Output:
Output:
Enter number: 7
Number is prime
Output:
Output:
sum1=0
num=int(input("Enter a number:"))
temp=num
while(num):
i=1
f=1
r=num%10
while(i<=r):
f=f*i
i=i+1
sum1=sum1+f
num=num//10
if(sum1==temp):
print("The number is a strong number")
else:
print("The number is not a strong number")
Output:
Enter a number:145
The number is a strong number.
31) Write a Python Program to Find the Second Largest Number in a List?
a=[]
n=int(input("Enter number of elements:"))
for i in range(1,n+1):
b=int(input("Enter element:"))
a.append(b)
a.sort()
print("Second largest element is:",a[n-2])
Output:
32) Write a Python Program to Swap the First and Last Value of a List?
a=[]
n= int(input("Enter the number of elements in list:"))
for x in range(0,n):
element=int(input("Enter element" + str(x+1) + ":"))
a.append(element)
temp=a[0]
a[0]=a[n-1]
a[n-1]=temp
print("New list is:")
print(a)
Output:
string=raw_input("Enter string:")
if(string==string[::-1]):
print("The string is a palindrome")
else:
print("The string isn't a palindrome")
Output:
string=raw_input("Enter string:")
vowels=0
for i in string:
if(i=='a' or i=='e' or i=='i' or i=='o' or i=='u' or i=='A' or i=='E' or
i=='I' or i=='O' or i=='U'):
vowels=vowels+1
print("Number of vowels are:")
print(vowels)
Output:
35) Write a Python Program to Check Common Letters in Two Input Strings?
Output:
What is Python?
Python Advantages
Python For Beginners
Python For Machine Learning
Machine Learning For Beginners
TAGS
Interview Questions
Python
Python Coding Interview Questions
Python Interview Questions
Coding Compiler
SHARE
tweet
Interview Questions
Interview Questions
2 COMMENTS
Reply
there are even wrong answers, next to many spelling and grammar mistakes. Take
question 10: The correct answer ist 1,2,3, a[-3] calls the third element from right,
which is 1 and so on.
Reply
LEAVE A REPLY
Latest Posts
Popular Categories
Interview Questions200
Programming58
C Programming50
Java Interview Questions24
Python22
Blockchain20
EDITOR PICKS