December 24, 2018

Srikaanth

NetEase Most Frequently Asked Python Interview Questions Answers

How Do I Share Global Variables Across Modules?

The canonical way to share information across modules within a single program is to create a special module (often called config or cfg). Just import the config module in all modules of your application; the module then becomes available as a global name. Because there is only one instance of each module, any changes made to the module object get reflected everywhere.

For example:

config.py:

x = 0 # Default value of the 'x' configuration setting
mod.py:
import config
config.x = 1
main.py:
import config
import mod
print config.x

How Do I Copy An Object In Python?

In general, try copy.copy() or copy.deepcopy() for the general case. Not all objects can be copied, but most can.
Some objects can be copied more easily. Dictionaries have a copy() method:
newdict = olddict.copy()
Sequences can be copied by slicing:
new_l = l[:]
NetEaseMost Frequently Asked Latest Python Interview Questions Answers
NetEaseMost Frequently Asked Latest Python Interview Questions Answers

What is the purpose pass statement in python?

pass statement − The pass statement in Python is used when a statement is required syntactically but you do not want any command or code to execute.

How will you randomizes the items of a list in place?

shuffle(lst) − Randomizes the items of a list in place. Returns None.

How will you capitalizes first letter of string?

capitalize() − Capitalizes first letter of string.

How will you check in a string that all characters are alphanumeric?

isalnum() − Returns true if string has at least 1 character and all characters are alphanumeric and false otherwise.

How will you check in a string that all characters are digits?

isdigit() − Returns true if string contains only digits and false otherwise.

How will you check in a string that all characters are in lowercase?

islower() − Returns true if string has at least 1 cased character and all cased characters are in lowercase and false otherwise.

How will you merge elements in a sequence?

join(seq) − Merges (concatenates) the string representations of elements in sequence seq into a string, with separator string.

How will you get the length of the string?

len(string) − Returns the length of the string.

How will you get a space-padded string with the original string left-justified to a total of width columns?

just(width[, fillchar]) − Returns a space-padded string with the original string left-justified to a total of width columns.

How will you convert a string to all lowercase?

lower() − Converts all uppercase letters in string to lowercase.

What Are The Built-In Types Available In Python?

Here is the list of most commonly used built-in types that Python supports:

Immutable built-in types of Python

Numbers
Strings
Tuples

Mutable built-in types of Python

List
Dictionaries
Sets

How To Find Bugs Or Perform Static Analysis In A Python Application?

You can use PyChecker, which is a static analyzer. It identifies the bugs in Python project and also reveals the style and complexity related bugs.
Another tool is Pylint, which checks whether the Python module satisfies the coding standard.

When Is The Python Decorator Used?

Python decorator is a relative change that you do in Python syntax to adjust the functions quickly.

 What Is The Key Difference Between A List And The Tuple?

List Vs Tuple.
The major difference between a list and the tuple is that the list is mutable while tuple is not. A tuple is allowed to be hashed, for example, using it as a key for dictionaries.

 How Does Python Handle The Memory Management?

Python uses private heaps to maintain its memory. So the heap holds all the Python objects and the data structures. This area is only accessible to the Python interpreter; programmers can’t use it.
And it’s the Python memory manager that handles the Private heap. It does the required allocation of the heap for Python objects.
Python employs a built-in garbage collector, which salvages all the unused memory and offloads it to the heap space.


How will you convert a string to a long in python?

long(x [,base] ) – Converts x to a long integer. base specifies the base if x is a string.

How will you convert a string to a float in python?

float(x) − Converts x to a floating-point number.

How will you convert a object to a string in python?

str(x) − Converts object x to a string representation.

How will you convert a object to a regular expression in python?

repr(x) − Converts object x to an expression string.

How will you convert a String to an object in python?

eval(str) − Evaluates a string and returns an object.

How will you convert a string to a tuple in python?

tuple(s) − Converts s to a tuple.

How will you convert a string to a list in python?

list(s) − Converts s to a list.

How will you convert a string to a set in python?

set(s) − Converts s to a set.

What Are The Principal Differences Between The Lambda And Def?

Lambda Vs Def.
def can hold multiple expressions while lambda is a uni-expression function.
def generates a function and designates a name so as to call it later. lambda forms a function and returns the function itself.
def can have a return statement. lambda can’t have return statements
lambda supports to get used inside a list and dictionary.

Write A Reg Expression That Confirms An Email Id Using The Python Reg Expression Module <Re>?

Python has a regular expression module <re>.

Check out the <re> expression that can check the email id for .com and .co.in subdomain.

import re
print(re.search(r"[0-9a-zA-Z.]+@[a-zA-Z]+\.(com|co\.in)$","micheal.pages@mp.com"))


What Do You Think Is The Output Of The Following Code Fragment? Is There Any Error In The Code?

list = ['a', 'b', 'c', 'd', 'e']
print (list[10:])
The result of the above lines of code is []. There won’t be any error like an IndexError.

You should know that trying to fetch a member from the list using an index that exceeds the member count (for example, attempting to access list[10] as given in the question) would yield an IndexError. By the way, retrieving only a slice at an opening index that surpasses the no. of items in the list won’t result in an IndexError. It will just return an empty list.

Is There A Switch Or Case Statement In Python? If Not Then What Is The Reason For The Same?

No, Python does not have a Switch statement, but you can write a Switch function and then use it.

What Is A Built-In Function That Python Uses To Iterate Over A Number Sequence?

range() generates a list of numbers, which is used to iterate over for loops.

for i in range(5):
    print(i)

The range() function accompanies two sets of parameters.

range(stop)

stop: It is the no. of integers to generate and starts from zero. eg. range(3) == [0, 1, 2].

range([start], stop[, step])

start: It is the starting no. of the sequence.
stop: It specifies the upper limit of the sequence.
step: It is the incrementing factor for generating the sequence.

Points to note:

Only integer arguments are allowed.
Parameters can be positive or negative.
The <range()> function in Python starts from the zeroth index.

What Are The Optional Statements That Can Be Used Inside A <Try-Except> Block In Python?

There are two optional clauses you can use in the <try-except> block.

The <else> clause
It is useful if you want to run a piece of code when the try block doesn’t create any exception.
The <finally> clause
It is useful when you want to execute some steps which run, irrespective of whether there occurs an exception or not.

What is the output of L[1:] if L = [1,2,3]?

2, 3, Slicing fetches sections.

How will you compare two lists?

cmp(list1, list2) − Compares elements of both lists.

How will you get the length of a list?

len(list) − Gives the total length of the list.

How will you get the max valued item of a list?

max(list) − Returns item from the list with max value.

Subscribe to get more Posts :