p

python tutorial - Python django interview questions - learn python - python programming



python interview questions :71

Vector of pairs in python?

  • It is storing in a dictionary (Hashmap)
input = ['str1', 'str2', 'str3']
stored_as = {'str1': {'start': 1, 'end': 2}, 'str2': {'start': 0, 'end': 2}, 'str3': {'start': 1, 'end': 1}}
click below button to copy the code. By Python tutorial team
  • This gives you a better representation. If you are tight on space, then you can store it as either of the following:
stored_as = [(1,2), (0,2), (1,1)]
click below button to copy the code. By Python tutorial team
  • or
stored_as = [[1,2], [0,2], [1,1]]
click below button to copy the code. By Python tutorial team

python interview questions :72

Converting a Python dict to Octave equivalent?

return dict(Fx = fxp, Fy = fyp, MO = MOp)
click below button to copy the code. By Python tutorial team

python interview questions :73

How to retrieve data from a table in MySQL database through Python code?

  • 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”)
click below button to copy the code. By Python tutorial team
  • 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()

python interview questions :74

Explain about ODBC and Python ?

  • ODBC (“Open Database Connectivity) API standard allows the connections with any database that supports the interface, such as PostgreSQL database or Microsoft Access in a transparent manner
  • There are 3 ODBC modules for Python:
    • PythonWin ODBC module - limited development
    • mxODBC - commercial product
    • pyodbc - it is an open source Python package.

python interview questions :75

How would you define a protected member in a Python class?

  • All the members of a class in Python are public by default. You don’t need to define an access specifier for members of class.
  • By adding ‘_’ as a prefix to the member of a class, by convetion you are telling others please don’t this object, if you are not a subclass the respective class.
class Person:
empid = None
_salary = None #salary is a protected member & it can accessible by the subclasses of Person
click below button to copy the code. By Python tutorial team

python interview questions :76

How do you remove duplicates from a list?

  • sort the list
  • scan the list from the end.
  • while scanning from right-to-left, delete all the duplicate elements from the list

python interview questions :77

Differentiate between append() and extend() methods. ?

  • Both append() and extend() methods are the methods of list. These methods a re used to add the elements at the end of the list.
    • append(element) - adds the given element at the end of the list which has called this method.
    • extend(another-list) - adds the elements of another-list at the end of the list which is called the extend method.

python interview questions :78

Name few Python Web Frameworks for developing web applications?

  • There are various web frameworks provided by Python. They are
    • web2py - it is the simplest of all the web frameworks used for developing web applications.
    • cherryPy - it is a Python-based Object oriented Web framework.
    • Flask - it is a Python-based micro-framework for designing and developing web applications.

python interview questions :79

How do you check the file existence and their types in Python?

  • os.path.exists() - use this method to check for the existence of a file. It returns True if the file exists, false otherwise. Eg: import os; os.path.exists(‘/etc/hosts’)
  • os.path.isfile() - this method is used to check whether the give path references a file or not. It returns True if the path references to a file, else it returns false. Eg: import os; os.path.isfile(‘/etc/hosts’)
  • os.path.isdir() - this method is used to check whether the give path references a directory or not. It returns True if the path references to a directory, else it returns false. Eg: import os; os.path.isfile(‘/etc/hosts’)
  • os.path.getsize() - returns the size of the given file
  • os.path.getmtime() - returns the timestamp of the given path.

python interview questions :80

Name few methods that are used to implement Functionally Oriented Programming in Python?

  • Python supports methods (called iterators in Python3), such as filter(), map(), and reduce(), that are very useful when you need to iterate over the items in a list, create a dictionary, or extract a subset of a list.
    • filter() - enables you to extract a subset of values based on conditional logic.
    • map() - it is a built-in function that applies the function to each item in an iterable.
    • reduce() - repeatedly performs a pair-wise reduction on a sequence until a single value is computed.
  • The list1 and list3 are hence operating on the same default list, whereas list2 is running on a separate list 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
click below button to copy the code. By Python tutorial team
  • With this revised implementation, the output would be:
list1 = [10]
list2 = [123]
list3 = ['a']
 

Related Searches to Python django interview questions