The article aims to help prepare for an interview as a Python developer. It will help in the preparation of both beginners and professionals!
The questions presented will help you, prepare for a technical interview and qualifying online tests, which are usually held at universities.
- What commands are used to copy objects in Python?
The command used to copy objects in Python includes:
Copy () function: This operation copies an object from its original location to the requested one. The function returns a shallow copy of the argument passed;
deepcopy (): This operation also copies the file from the source location to the requested one. The difference is that this operation returns a full copy of the passed argument.
The dictionary consists of all objects and the copy () method, which is used as shown below:
newdict = olddict.copy()
The assignment operator does not copy objects, but it creates a relationship between the object being searched and the object that is used by mutable elements. Oddly enough, the purpose of copying is to store a copy of the object. To do this, the copy () method uses the appropriate modules, which make it possible to do full and shallow copying.
- Differentiate full and shallow copying?
Surface copying is used when an instance of a new type is created and the value stored in this instance needs to be copied to a new instance. While full copying is used to store values that have already been copied.
Surface copy is used to copy references to objects; the copy mechanism is similar to copying the value of an object. References to the source objects and changes that were made to them by any member of the class affect the original copy. While full copying does not copy references to objects. A full copy creates a link to an object and a new object referenced by other objects is saved. Changes to the source object do not affect other copies using this object in any way.
When using shallow copy, the program runs faster, but it also depends on the size, data used (copied). While with a full copy, the program runs slower due to the fact that a specific copy of each called object is created.
- Give an example of using the ternary operator in Python.
A ternary operator is an operator that is used to demonstrate a condition, that is, instead of a conditional construct. It consists of true or false values and the expression that must be calculated for these values. The following is an example of a ternary operator:
x, y = 25, 50
big = x if x < y else y
The ternary operator has the lowest priority used to make a decision that is based on the value of the condition, whether it is true or false. In our example, the expression “if x <y, otherwise y” will be calculated as follows: if x is less than y, then the return value will be big = x, if it is not, the return value will be big = y.
- How can a string be converted to a number?
In order to convert a string to a number, you can use the built-in functions of the Python language, for example, using the int () constructor. In this way, you can convert data of the type: int (‘1’) == 1.
The float () function can also be used to display a number in the format float (‘1’) = 1.
By default, all numbers are represented as decimal, which means that if you try to perform the conversion in this way: int (‘0x1’), you will receive a “ValueError” error message. This is due to the fact that the int (string, base) input parameters function is designed to convert a string to a number, the conversion process looks something like this: int (‘0x1’, 16) == 16. If the (base) parameter is defined as 0, then this indicates that the number will be octal, and if as 0x, this will mean that the number is hexadecimal.
There is also an eval () function that can be used to convert a string to a number, but it works a little slower and causes a lot of security problems, for example, __import __ (‘os’). System (“rm -rf $ HOME”) – use This will delete the system home directory.
- What is the function / purpose of a negative index?
Any sequences in Python are indexed, and they consist of both positive and negative indices. The indexing of positive numbers begins with ‘0’, that is, the first element is ‘0’, and the second is ‘1’ and so on. Indexing of negative numbers starts with ‘-1’, this index denotes the last element in the sequence, and ‘-2’ denotes the penultimate element, and then indexing goes forward by analogy with positive numbers. Negative indexes are used to remove extra spaces from the string, as well as to allow the string to skip the last character, which is represented as: S [: – 1]. Negative indexes are also used to indicate that indexing in a row is in the correct order.
- What is “self” in functions for?
“Self” is a variable indicating that the object belongs to itself. In Python, this definition and transfer of variable is explicit. The first argument, which is created in an instance of class A, automatically passes the parameters of the method. Individual instances of the variable are accessed for each individual object. This is the first argument that is used in the instance of the class, and the “self” method is defined explicitly for all used and existing methods. The name of the variables follows this pattern: “self.xxx”.
- How can one explicitly define “self” in a method?
“Self” is a reference variable and an attribute of an instance that is used instead of a local variable inside the class. A function or self variable, such as self.x or self.meth (), can be used if the class is unknown. Such a class does not have variables declared as local. It does not have any syntax, but you can explicitly pass a reference in it or call the method that this class uses. Using writebaseclass.methodname (self, <argument list>) shows that the _init_ () method can be extended to the base methods of the class. It also solves the syntax problem that occurred between the assignment operator and local variables. This approach tells the interpreter when class instance variables will be used, and when local variables.
- Why is the join () method used more often when working with strings rather than lists and tuples?
To ensure the full functionality of all methods and functions in string, the string module is used. The presented string module uses the join () method:
“, “.join([‘1’, ‘2’, ‘4’, ‘8’, ’16’])
The variable of type string used allows fixed string constants to use the names that are needed to bind strings together. The join () method is a string method that provides a separator string so that you can use the function over the string sequence, that is, so that you can insert the necessary string in the right place. A method can take any number of arguments that correspond to some requirements that are set for sequence objects, and they are already defined inside its class. Join () is used in the string module to combine string characters into one string, as shown in the program below. An example looks like this:
string.join([‘1’, ‘2’, ‘4’, ‘8’, ’16’], “, “)