Top 10 Most Asked Python Interview Questions With Answers Part 08

By Muhammad Umair

Top 10 Most Asked Python Interview Questions With Answers Part 05 By Muhammad Umair
x = ['ab', 'cd']
print(len(map(list, x)))

A TypeError occurs as the map has no len().

x = ['ab', 'cd']
print(len(list(map(list, x))))

The length of each string is 2.

a) set([[1,2],[3,4]])b) set([1,2,2,3,4])c) set((1,2,3,4))d) {1,2,3,4}

Sometimes, when we want to iterate over a list, a few methods come in handy.

filter(): Filter lets us filter in some values based on conditional logic.

list(filter(lambda x:x>5,range(8)))

Ans: [6, 7]

map(): Map applies a function to every element in an iterable.

list(map(lambda x:x**2,range(8)))

Ans: [0, 1, 4, 9, 16, 25, 36, 49]

reduce(): Reduce repeatedly reduces a sequence pair-wise until we reach a single value

from functools import reducereduce(lambda x,y:x-y,[1,2,3,4,5])

Ans: -13

Note: A palindrome is a word, phrase, or sequence that reads the same backward as forward, e.g., madam, saas, nun.

def isPalindrome(string):
left_pos = 0
right_pos = len(string) – 1
while right_pos >= left_pos:
if not string[left_pos] == string[right_pos]:
return False
left_pos += 1
right_pos -= 1
return Trueprint(isPalindrome('aza'))
def list_sum(num_List):    
if len(num_List) == 1:
return num_List[0]
else:
return num_List[0] + list_sum(num_List[1:])
print(list_sum([2, 4, 5, 6, 7]))

Sample Output: 24

#import MySQLdb module as :
import MySQLdb#establish a connection to the
database.db = MySQLdb.connect("host"="local host", "database-user"="user-name", "password"="password","database-name"="database")
#initialize the cursor variable upon the established connection:
c1 = db.cursor()#retrieve the information by defining a required query string.s = "Select * from dept"#fetch the data using fetch() methods and print it.data = c1.fetch(s)#close the database connection.db.close()
import randomdef random_line(fname):    
lines = open(fname).read().splitlines()
return random.choice(lines)
print(random_line('test.txt'))
def file_lengthy(fname):    
with open(fname) as f:
for i, l in enumerate(f):
pass
return i + 1
print("Number of lines in the file: ",file_lengthy("test.txt"))

If it makes for an introductory language to programming, Python must mean something. These are its qualities:

  • Interpreted.
  • Dynamically-typed.
  • Object-oriented
  • Concise and simple
  • Free
  • Has a large community

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Muhammad Umair

MERN Stack Developer | Software Engineer| Frontend & Backend Developer | Javascript, React JS, Express JS, Node JS, MongoDB, SQL, and Python