Vomiting blood summary! Python collection surface 50 questions (with answers)

Python is currently the most popular field of programming language. In this article, I will summarize the most common Python interview 50 questions. Each question provides answers to a reference, hoping to help you stand out in a 2019 job interview, find a high-paying job. These interview questions involve basic knowledge of various aspects of Python, Python programming, data analysis, and Python libraries and so on.

Q1, Python lists and tuples What is the difference?

Q2, what are the main features of the Python?

Python is an interpreted language. C language with a different language, Python does not need to be compiled before running.

Python is a dynamic language, when you declare a variable or similar variables, you do not need to declare variables of type.

Python suitable object-oriented programming, since it allows the definition and combination of classes and inheritance. Python does not have access instructions (such as C ++, public, private).

In Python, functions are first-class objects. They can be assigned to variables. The first class is the class object

Write Python code quickly, but run slower. Python allows based on the extended C, e.g. numpy libraries.

Python can be used in many fields. Web application development, automation, mathematical modeling, large data applications, and so on. It is also often used as a "glue" code.

Q3, Python is a general-purpose programming language?

Python can write a script, but the general sense, it is considered to be a general purpose programming language.

How Q4, Python is an interpreted language?

Python before running the program does not need to be explained. Therefore, Python is an interpreted language.

Q5, what is pep?

On behalf of PEP Python Enhancement Proposal. It is a set of rules that specify how to format the Python code for maximum readability.

Q6, how to manage memory in Python?

python in the Python memory management by the private heap space management. All Python objects and data structures are located in private heap. Programmers do not have access to this private heap. python interpreter is responsible for dealing with this problem.

Python object heap space allocation done by the Python memory manager. The core API provides tools programmers to write code.

Python there is a built-in garbage collector, which can recover all the unused memory, and make it available heap space.

Q7, what is the namespace in Python?

Namespace is a naming system is used to ensure the uniqueness of the name, to avoid naming conflicts.

Q8, what is PYTHONPATH?

It is used when the environment variable import module. Whenever the import module, the module will look for each directory PYTHONPATH to check whether there is imported. Interpreter uses it to determine the module to be loaded.

Q9, what is python module? What are common built-in module in Python there?

Module is a file containing Python .py Python code. This code may be a function or class variable. Some commonly used built-in module comprising: sys, math, random, data time, JSON.

Q10, what local and global variables in Python is?

Global variables: variables declared in a function or outside the global space called global variables. These variables can be accessed by any function in the program.

Local variables: Any variables declared within a function are called local variables. This variable exists in the local space, rather than the global space.

Q11, python is case sensitive?

Yes. Python is a case-sensitive language.

Q12, What is the Python type conversion?

Type conversion means to convert one data type to another data type.

int () - converts any data type is an integer type

float () - converts any data type to float

ord () - characters converted to an integer

hex () - converts integers to hexadecimal

oct () - integer octal

tuple () - This function is used to convert a tuple.

set () - This function returns after conversion type set.

list () - This function is used to any type of data into a list.

dict () - This function is used to order tuples (key, value) is converted to the dictionary.

str () - used to convert the integer to a string.

complex (real, imag) - This function converts a real number to a complex (real image) numbers.

Q13, How to install Python on Windows and set the path variable?

To install Python on Windows, follow these steps:

Install python from the following link: HTTPS: // http://www.python.org/downloads/

Once downloaded, install it on your PC. PYTHON find the location of the installation on the PC using the following command at the command prompt: cmd python.

Then go to the Advanced System Settings and add new variables and name PYTHON_NAME and paste the copied path.

Find the path variable, select the value and choose "Edit."

If the value does not exist, add a semicolon at the end of the value, and then type% PYTHON_HOME%

Q14, python whether to indent?

Python indentation is required. It specifies a code block. Circulating, class, and all other functions specified in the code block indentation. Usually four space characters to complete. If there is no need to indent your code, it will not be performed accurately and will throw an error.

Q15, Python arrays and lists what's the difference?

Python arrays and lists in the same way of storing data. However, an array can contain a single type of data elements, and the list can contain any type of data elements.

Q16, what is the function in Python?

Function is a block of code that's only done when it is called. To define a function in Python, you need to use def keyword.

Q17, What is __init__?

in Python __init__ method or structure. When you create a new class of object / instance, it will automatically call this method to allocate memory. All classes have __init__ method.

Q18, what are lambda functions?

lambda function, also called anonymous function, which can contain any number of parameters, but only a statement to perform the operation.

Q19, what the self is Python?

It is an example of self or object class. In Python, self contained in the first parameter. However, the situation in Java is not the case, it is optional. It helps to distinguish the methods and attributes of the classes with local variables. init method of self variable refers to the newly created object, whereas in other methods, it refers to the object whose method is called.

Q20, distinguish break, continue and pass?

Q21, [:: - 1} What does that mean?

[:: --1] reverse the order or sequence of the array is used.

Q22, how the elements in the list of randomization in Python?

You can use random shuffle function list elements. For example as follows:

 

Code output:

 

Q23, What is the python iterator?

Iterator object can traverse or iteration.

Q24, how to generate random numbers in Python?

random module for generating a random number of standard modules. This method is defined as:

 

 

random.random () method returns a floating point number in the range of [0,1]. This function generates a random floating point numbers. Method using a stochastic hidden instance binding method. Random examples can be used to display different threads to create instances of a multithreaded program. Wherein the other random generators are used:

randrange (a, b): it selects an integer and define a range between [a, b]. It is to return the element from the specified range by selecting a random element. It does not build a range object.

uniform (a, b): it selects a floating point defined in the [a, b) range

normalvariate (mean, sdev): It is used for a normal distribution, where mean is the mean, standard deviation sigma is sdev of.

Random use and instantiated class to create a plurality of separate random number generator.

Q25, range & xrange What is the difference?

In most cases, xrange and range are identical in terms of functionality. They provide a method of generating a list of integers, the only difference is the range returns a list of objects Python, X range returns a xrange object. This means xrange not actually generate static list at run time. It uses special technology called yielding to create values ​​as needed. The technique used in conjunction with an object called a generator. So if you have a very large list, then you must consider xrange.

Q26, how to write notes in python?

Comments in Python start with the # character. You may also be used doc-strings (triplet string enclosed in quotation marks) annotate.

Q27, What is pickling and unpickling?

Pickle module accepts any Python object and convert it to a string representation, and using the function to dump dump of a process called pickling. Python retrieve the original object from a string stored procedure called unpickling.

Q28, what the generator is a python?

Returns iteration itemset generator function is called.

Q29, how you put the first letter in a string of capital?

In Python, capitalize () function can be capitalized first letter of the string. If the string is already included in capital letters at the beginning, it will return the original string.

Q30, how to convert a string to all lowercase?

To convert strings to lower, you may be used lower () function.

Q31, How to comment multiple lines in python?

NOTE multiple lines of code. All comments must be in line before beginning a #. You can also use shortcuts to comment multiple lines, that is, hold down the Ctrl key and left-click and type a # at every place you want to include the # character.

Q32, what is in the document Docstrings Python?

Docstrings not actually comment, which is a document string. These documents within three strings in quotation marks. They are not assigned to any variable, it is sometimes also used for comments.

Q33, operators of is, not what function and in each?

Operators are special functions that compare one or more values ​​and generates a corresponding result. Which is: Returns true if the two operands is true (for example: "a" is 'a')

not: reciprocal return a Boolean value

in: check whether there is an element in a sequence

What is the use of Q34, Python in help () and dir () function is?

Help () and dir () Both of these functions can be accessed directly from the Python interpreter and for viewing merge dump built-in functions.

help () function: help () function is used to display the document string, you can also see the module, keyword, property and other related usage information.

dir () function: dir () function is used to display the symbol definition.

Q35, when Python exits, why not remove all memory allocation?

When Python quit, especially those Python modules with circular references to other objects or object references from global name space has not been deallocated or released.

Those unable to lift the portion of memory allocation reserved for the C library.

When you exit, due to the efficient and clean-up has its own mechanism, Python will try unassigned / destroy all other objects.

Q36, what Python in the dictionary?

Built-in data type called a dictionary in Python. It defines one relationship between keys and values. Dictionary contains values ​​and corresponding key pair. Dictionary indexed by key.

Q37, how to use the ternary operator in python?

Ternary operator conditional statements for displaying operator. This includes true or false value, and must evaluate its statement. The basic syntax is:

Ternary operator conditional statements for displaying operator. This includes true or false value, and must evaluate its statement. The basic syntax is:

[on_true] if [expression] else [on_false] x,y = 25,50big = x if x <y else y

Q38, Why use * args, ** kwargs?

When we are not sure how many will pass parameters to a function, or we want to store a list or tuple of parameters passed to the function, we use * args. ** When we do not know how many will pass keyword arguments to use when kwargs function, or it can be used to value the dictionary as a key parameter. Identifier args and kwargs is a convention, you can also use * bob and ** billy.

Q39, len () function What is the role?

len () function can be used to determine the length of the strings, lists, arrays, and the like.

Q40, in Python split (), sub (), subn () function.

To modify the string, Python of "re" module provides three methods. they are:

split () - using a regular expression pattern given string "split" to the list.

sub () - Find all substrings regular expression pattern matching, and then replace them with a different string

subn () - which is similar to sub (), and also returns the new string.

Q41, what is the negative exponential, what is the function?

Python is an index of the sequence, which consists of positive and negative numbers. Use positive numbers '0' as the first index, '1' as the second index, the process continues to use.

Negative indices from '-1', the index represents the last in the sequence, '- 2' as the penultimate index as a positive number as the sequence proceeds.

A negative index is used to remove any line breaks from the string, and allowing the string except as S [: - 1] of the last character is given. A negative index is also used to display the index string representing the correct order.

Q42, What is the Python package?

Python is a package comprising a plurality of modules namespace.

Q43, how to delete files in Python?

To delete a file in Python, you need to import OS module. After that, you need to use os.remove () function.

Q44, what is the python built-in types?

Python built-in types are as follows: integer, floating point, complex numbers, strings, Boolean and so on.

Q45, which function operation NumPy Python list there?

Python is a list of generic container efficient. They support (relatively) efficient insertion, deletion, addition or connection, Python list derived makes them easy construction and operation.

They have some limitations: they do not support the addition and multiplication, etc. pixelated "vectorized" operation, and they may contain different types of objects to be stored Python This fact means that the type of information for each element, and must perform a scheduling type when the operation code is performed for each element.

NumPy not only more efficient; it is also more convenient. You can get a lot of free vector and matrix operations, which sometimes can avoid unnecessary work. They are also effective embodiments.

NumPy arrays faster, you can use NumPy, FFT, convolution, quick search, basic statistics, linear algebra, histogram built.

Q46, how to add value to an array of python?

Can append (), extend () and insert (i, x) function to add elements to the array.

Q47, how to delete an array of value python?

You can use pop () or remove () method removes array elements. The difference between these two is that the former function returns the value has been deleted, and the latter is not returned.

Q48, Python has OOps concept it?

Python is an object-oriented programming language. This means that any program can be resolved in python by creating an object model. While the Python programming language and can be seen as structured language.

Q49, deep and shallow copy copy What is the difference?

A shallow copy when creating a new instance of a type, and retention of the replication in the new instance. Shallow copy for copying a reference pointer, the same value as copying. These references point to the original object, and any member of the class changes will also affect its original copy. Shallow copy execution program allows faster, depending upon the size of the data used.

Deep copy to store values ​​copied. Deep copy does not copy the reference pointer to the object. It refers to an object and store some new objects to other objects points. The original copy of the change will not affect any other copies made use of the object. Because some of the copies created for each object is called, and therefore the execution speed of the program will make a deep copy slow.

Q50, how to implement multiple threads in Python?

Python has a multi-threaded library, but with the effect of multi-threaded code to speed is not so good,

Python has a structure Global Interpreter Lock (GIL) is named. GIL can only ensure the implementation of a "thread." A thread gets GIL perform related operations, and then transferred to the next thread GIL.

Although it looks to be executed in parallel multi-threaded program, but they actually just take turns using the same CPU core.

All of these passes have increased the cost of GIL performed. This means that multiple threads does not make the program run faster.

Guess you like

Origin blog.csdn.net/sinat_38682860/article/details/94763641