Blog available for sell
This blog is available for sale. Please 'contact us' if interested.
Advertise with us

Python Interview Questions
21. Why are identifier names with a leading underscore disparaged?
A: Since Python does not have a concept of private variables, it is a convention to use leading underscores to declare a variable private. This is why we must not do that to variables we do not want to make private.
22. How is multithreading achieved in Python?
A: A thread is a lightweight process, and multithreading allows us to execute multiple threads at once. As you know, Python is a multithreaded language. It has a multi-threading package.
The GIL (Global Interpreter Lock) ensures that a single thread executes at a time. A thread holds the GIL and does a little work before passing it on to the next thread. This makes for an illusion of parallel execution. But in reality, it is just threaded taking turns at the CPU. Of course, all the passing around adds overhead to the execution.
23. What are negative indices?
A: A negative index begins searching from the right.
>>> mylist=[0,1,2,3,4,5,6,7,8]
>>> mylist[-3]
>>> 6
24. How would you randomize the contents of a list in-place?
A: Import the function shuffle() from the module random.
>>> from random import shuffle
>>> shuffle(mylist)
>>> mylist
25. Explain join() and split() in Python.
A: join() lets us join characters from a string together by a character we specify.
>>> ','.join('12345')
>>> '1,2,3,4,5'
split() lets us split a string around the character we specify.
>>> '1,2,3,4,5'.split(',')
>>> ['1','2','3','4','5']

26. How would you convert a string into lowercase?
A: We use the lower() method for this.
>>> 'pYthnOnCircle'.lower()
>>> ‘pythoncircle’
To convert it into uppercase, then, we use upper().
>>> 'pYthnOnCircle'.upper()
‘PYTHONCIRCLE’
27. Explain the //, %, and ** operators in Python.
A: The // operator performs floor division. It will return the integer part of the result on division.
>>> 5//2
>>> 2
The normal division would return 2.5 Similarly, ** performs exponentiation. a**b returns the value of a raised to the power b.
>>> 2**3
>>> 8
Finally, % is for modulus. This gives us the value left after the highest achievable division.
>>> 13%7
>>> 6
28. How can you declare multiple assignments in one statement?
A: There are two ways to do this:
>>> a,b,c=3,4,5  #This assigns 3, 4, and 5 to a, b, and c respectively
>>> a=b=c=3  #This assigns 3 to a, b, and c
29. What Is The Output Of The Following Python Code Fragment?
def extendList(val, list=[]):
    list.append(val)
    return list

list1 = extendList(10)
list2 = extendList(123,[])
list3 = extendList('a')

print "list1 = %s" % list1
print "list2 = %s" % list2
print "list3 = %s" % list3
A:
list1 = [10, 'a']
list2 = [123]
list3 = [10, 'a']
You may erroneously expect list1 to be equal to [10] and list3 to match with [‘a’], thinking that the list argument will initialize to its default value of [] every time there is a call to the extendList.
However, the flow is like that a new list gets created once after the function is defined. And the same get used whenever someone calls the extendList method without a list argument. It works like this because the calculation of expressions (in default arguments) occurs at the time of function definition, not during its invocation.
The list1 and list3 are hence operating on the same default list, whereas list2 is running on a separate object that it has created on its own (by passing an empty list as the value of the list parameter).
The definition of the extendList function can get changed in the following manner.
def extendList(val, list=None):
  if list is None:
    list = []
  list.append(val)
  return list
With this revised implementation, the output would be:
list1 = [10]
list2 = [123]
list3 = ['a']
30. What Is The Statement That Can Be Used In Python If The Program Requires No Action But Requires It Syntactically?
A: The pass statement is a null operation. Nothing happens when it executes. You should use “pass” keyword in lowercase.
for i in range(10):
    if i%2 == 0:
         pass
    else:
         print(i)





DigitalOcean Referral Badge

© 2022-2023 Python Circle   Contact   Sponsor   Archive   Sitemap