Python Interview Questions Set - 4 Released

Python Interview Questions Set - 4 Released

Intermediate Level

What are comments and how can you add comments in Python?

Comments in Python refer to a piece of text intended for information. It is especially relevant when more than one person works on a set of codes. It can be used to analyse code, leave feedback, and debug it. There are two types of comments which includes: 1. Single-line comment 2. Multiple-line comment Codes needed for adding a comment #Note –single line comment “””Note Note Note”””—–multiline comment

What is a dictionary in Python? Give an example.

A Python dictionary is a collection of items in no particular order. Python dictionaries are written in curly brackets with keys and values. Dictionaries are optimized to retrieve values for known keys. Example d={“a”:1,”b”:2}

What is the difference between a tuple and a dictionary?

One major difference between a tuple and a dictionary is that a dictionary is mutable while a tuple is not. Meaning the content of a dictionary can be changed without changing its identity, but in a tuple, that’s not possible.

Find out the mean, median and standard deviation of this numpy array -> np.array([1,5,3,100,4,48])

import numpy as np n1=np.array([10,20,30,40,50,60]) print(np.mean(n1)) print(np.median(n1)) print(np.std(n1))

What is a classifier?

A classifier is used to predict the class of any data point. Classifiers are special hypotheses that are used to assign class labels to any particular data point. A classifier often uses training data to understand the relation between input variables and the class. Classification is a method used in supervised learning in Machine Learning.

How do you get a list of all the keys in a dictionary?

One of the ways we can get a list of keys is by using: dict.keys() This method returns all the available keys in the dictionary. dict = {1:a, 2:b, 3:c} dict.keys() o/p: [1, 2, 3]

How can you insert an element at a given index in Python?

Python has an inbuilt function called the insert() function. It can be used used to insert an element at a given index. Syntax: 1 list_name.insert(index, element) ex: list = [ 0,1, 2, 3, 4, 5, 6, 7 ] #insert 10 at 6th index list.insert(6, 10) o/p: [0,1,2,3,4,5,10,6,7]

How will you remove duplicate elements from a list?

There are various methods to remove duplicate elements from a list. But, the most common one is, converting the list into a set by using the set() function and using the list() function to convert it back to a list if required. ex: list0 = [2, 6, 4, 7, 4, 6, 7, 2] list1 = list(set(list0)) print (“The list without duplicates : ” + str(list1)) o/p: The list without duplicates : [2, 4, 6, 7]

What is recursion?

Recursion is a function calling itself one or more times in it body. One very important condition a recursive function should have to be used in a program is, it should terminate, else there would be a problem of an infinite loop.

Explain Python List Comprehension.

List comprehensions are used for transforming one list into another list. Elements can be conditionally included in the new list and each element can be transformed as needed. It consists of an expression leading to a for clause, enclosed in brackets. For ex: list = [i for i in range(1000)] print list

What is the bytes() function?

The bytes() function returns a bytes object. It is used to convert objects into bytes objects or create empty bytes objects of the specified size.

What are the different types of operators in Python?

Python has the following basic operators: Arithmetic (Addition(+), Substraction(-), Multiplication(*), Division(/), Modulus(%) ), Relational (<, >, <=, >=, ==, !=, ), Assignment (=. +=, -=, /=, *=, %= ), Logical (and, or not ), Membership, Identity, and Bitwise Operators

What is the ‘with statement’?

The “with” statement in python is used in exception handling. A file can be opened and closed while executing a block of code, containing the “with” statement., without using the close() function. It essentially makes the code much easier to read.

What is a map() function in Python?

The map() function in Python is used for applying a function on all elements of a specified iterable. It consists of two parameters, function and iterable. The function is taken as an argument and then applied to all the elements of an iterable(passed as the second argument). An object list is returned as a result. def add(n): return n + n number= (15, 25, 35, 45) res= map(add, num) print(list(res)) o/p: 30,50,70,90

要查看或添加评论,请登录

社区洞察

其他会员也浏览了