Android,iOS,Gadgets,Reviews Everything About Technology

Short answers to popular questions ask in Python Interview

3,980

Python is an excellent language for a lot of different tasks: from the web to scientific calculations. In addition, it is quite simple. So much so that you can start writing on it in a day or two. However, no one will argue that Python is quite different from most popular languages ​​- such as C, C ++, C # or Java. In this article, we collected answers to common questions and explanations for some of the “oddities” of Python, which often occupy the heads of both novice and experienced professionals. 


Why do some methods ( list.index () ) for some things , and operators ( len (list) ) for some ?

This was done primarily to improve readability. The second, important reason – the opportunity to implement these methods in C.


- Advertisement -

Why does [] work faster than list () ?

[] and {} are part of the syntax. The lists and dictionaries created by this method are initialized in the bytecode at once, whereas the list () and dict () methods require a lookup in the methods table.


Why is there no switch / case in Python ?

These operators are easily replaced by if ... elif ... else or by a dictionary with collabaks.


Why is it iterated faster than a row?

This is true only for Python 2, but not for Python 3. In addition, array indexing is faster because of fewer different checks.


Why is Python using GIL (Global Interpreter Lock)?

The main advantages of GIL are better performance in single-threaded programs and easier integration with thread-safe C libraries.


Why in the name of some methods have underscores in the beginning?

According to PEP8 , one lower underscore at the beginning of the method name is used to hide the method from the list of imported (like protected in Java), two underscores – to substitute the class name. Methods with two underscores at the beginning and two at the end (for example, __init __ () ) – so called. “Magical”. Their name and purpose are strictly defined by the language standard.


How does 10,000,000,000,000 in range (1000000000000001) work so fast in Python 3?

range () is a “lazy collection” in Python 3. In addition, the __contains __ () method is implemented so that it considers whether the given number falls within the range of O (1).


Why is it necessary to write self for each method?

This idea was borrowed from Modula-3. Such an announcement and calling methods have many advantages. Including: you can call any class method on any object, there is no need for modifiers or keywords like static , more readable and understandable code.

Comments