Python Coding Interview Questions1) What is the best way to debug a Python program?This command can be used to debug a Python program. 2) What does the Python
|
List | Tuple |
---|---|
Lists are editable, which means that we can change them. | Tuples (which are just lists that we cannot alter) are immutable. |
Lists are comparatively slower | Tuples are more efficient than lists. |
Syntax: list1 = [100, 'Itika', 200] | Syntax: tup1 = (100, 'Itika', 200) |
Although we can use Python to write scripts, it is primarily used as a general-purpose programming language.
Any scripting language not in machine code before execution is called an interpreted language. Python is thus an interpreted language.
PEP (Python Enhancement Proposal) is the acronym for Python Enhancement Proposal. It's a collection of guidelines for formatting Python code for better readability.
The advantages of utilizing Python are as follows:
Decorators are just employed to add certain layout patterns to a method without affecting the structure of the function. Typically, decorators are identified before the event they will be improving. We should first define a decorator's function before using it. The function to which We will implement the decorator's function is then written, and the decorator function is simply positioned above it. In this instance, the @ symbol comes preceding the decorator.
Dictionary & list comprehensions are yet another means of defining dictionaries and lists in a simple manner.
This is example of list comprehension
Code
Output:
[0, 1, 2, 3] This is example of dictionary
Code
Output:
{0: 2, 1: 3, 2: 4, 3: 5, 4: 6, 5: 7, 6: 8, 7: 9, 8: 10, 9: 11}
Numbers- Integers, complex numbers, and floating points are Python's most prevalent built-in data structures. For example, 1, 8.1, 3+6i.
List- A list is a collection of objects that are arranged in a specific order. A list's components could be of multiple data kinds. For example, [[10,'itika',7] .4]
Tuple- It's also a set of items in a specific order. Tuples, not like lists, are immutable, meaning we cannot modify them. For example, (7,'itika',2)
String- A string is a collection of characters. Single or double quotations are used to declare them. "Itika," "She is learning coding through Javatpoint", and so on.
Set- A set is a group of unrelated elements which are not in any particular sequence. (2, 3, 4, 5)
Dictionary- A dictionary is a collection of key and value combinations in which each value may be accessed by its key. The sequence of the items is irrelevant. For example, {3:'ape', 6:'monkey'}
Boolean- True and False is indeed the two possible boolean values.
The Python code we save is contained in the .py files. The .pyc files are created when the program is integrated into the current program from some other source. This file contains the bytecode for the Python files that we imported. The interpreter reduces processing time if we transform the source files having the format of .py files to .pyc files.
Global Variables: Global variables are those that have been declared outside of a function. The scope outside of the function is known as global space. Any program function has access to these variables.
Local Variables: Any variable declared inside a function is referred to as a local variable. This variable does not exist in the global domain; it only exists locally.
Code
Output:
In local scope: 7 Adding a global scope and a local scope variable: 63 In global scope: 56
It will generate an error if you attempt to access the local variable exterior of the function addition().
In Python, arrays and lists both store data similarly. On the other hand, arrays can only have a single data type element, while lists can contain any data type component.
Code
Output:
array('i', [3, 6, 2, 7, 9, 5]) [4, 'Interview', 7.2] 'str' object cannot be interpreted as an integer
In Python, __init__ is a function or function Object() { [native code] }. When a new object/instance of a class is created, this function is automatically called to reserve memory. The __init__ method is available in all classes.
Here's an instance of how to put it to good use.
Code
Output:
Itika 10
A lambda function is a type of nameless function. This method can take as many parameters as you want but a single statement.
Code
Output:
Sum using lambda function is: 18
A self is a class instance or object. This is explicitly supplied as the initial argument in Python. However, in Java, in which it is optional, that's not the case. Local variables make it easy to differentiate between a class's methods and attributes.
In the init method of a class, the self variable corresponds to the freshly generated objects, whereas it relates to the entity whose method can be called in the class's other methods.
Break | The loop is terminated when a criterion is fulfilled, and control is passed to the subsequent statement. |
Pass | You can use this when you need a code block syntactically correct but don't want to run it. This is a null action in essence. When it is run, nothing takes place. |
Continue | When a specified criteria is fulfilled, the control is moved to the start of the loop, allowing some parts of the loop currently in execution to be skipped. |
Consider the following scenario:
Code
Output:
Original list: ['Python', 'Interview', 'Questions', 'Randomise', 'List'] After randomising the list: ['List', 'Interview', 'Python', 'Randomise', 'Questions']
The Pickle module takes any Python object and then transforms it into the representation of a string, which it then dumps into a file using the dump method. Unpickling is the procedure of recovering authentic Python items from a saved string representation.
The lower() function could be used to reduce a string to lowercase.
Code
Output:
javatpoint
Multi-line comments span many lines. A # must precede all lines that we will comment on. You could also use a convenient alternative to comment on several lines. All you have to do is press down the ctrl key, hold it, and click the left mouse key in every area where you need a # symbol to appear, then write a # once. This will add a comment to the lines wherever you insert your cursor.
Docstrings stands for documentation strings, which are not just comments. We enclose the docstrings in triple quotation marks. They are not allocated to any variable, and, as a result, they can also be used as comments.
Code
Output:
Result of multiplication: 1755
Python's "re" module provides three ways for modifying strings. They are as follows:
split() "splits" a string into a list using a regex pattern.
sub() finds all substrings that match the regex pattern given by us. Then it replaces that substring with the string provided.
subn() is analogous to sub() in that it gives the new string and the number of replacements.
We can use the append(), extend(), as well as insert (i,x) methods to add items to an array.
Code
Output:
array('d', [1.0, 2.0, 3.0, 8.0]) array('d', [1.0, 2.0, 3.0, 8.0, 4.0, 6.0, 9.0]) array('d', [1.0, 2.0, 9.0, 3.0, 8.0, 4.0, 6.0, 9.0])
The pop() or remove() methods can be used to remove array elements. The distinction between these 2 methods is that the first returns the removed value, while the second does not.
Code
Output:
4.0 8.0 array('d', [3.0, 8.0, 1.0, 4.0, 2.0])
The phrase "monkey patch" in Python exclusively references run-time dynamic alterations to a module.
Code
The monkey-patch testing will then be done as follows:
An empty class has no statements contained within its blocks. It can be produced by using the pass keyword. But you can make an object outside the class. The PASS statement doesn't do anything in Python.
Code
Output:
Name = Javatpoint
Code
Output:
[13, 14, 23, 23, 64, 64, 86]
Code
Output:
[0, 1, 1, 2, 3, 5, 8, 13, 21]
Code
Output:
The given number is a prime number
Below is the program.
Code
Output:
The sequence is a palindrome
Using the code below.
Code
Output:
[3 1]
The following method can be used to determine percentiles.
Code
Output:
3.5
Following is the code.
Code
Output:
given number is binary
Following is the code.
Code
Output:
Factorial of 12 is 1449225352009601191936
Following is the code
Code
Output:
LCM of 24 and 92 = 552
Following is the code.
Code
Output:
Following is the code.
Code
Output:
Required string without vowels is: Jvtpnt
Following is the code.
Output:
Reverse order of array is 75 24 86 24 86 76 23 24 5 12 23
Following is the code.
Code
Output:
After implementing right rotation the list is : [24, 75, 23, 12, 5, 24, 23, 76, 86, 24, 86]