This article is participating in the "Python theme Month" view details about the active links
100 basic Python interview questions part one (1-20) Python topic month
Q-1: What is Python, what are the benefits of using it, and what is your understanding of PEP 8? Q-2: What is the output of the following Python code snippet? Prove your answer. Q-3: If the program does not require actions but needs it grammatically, what statements can be used in Python? Q-4: What is the process of using "~" to get the home directory in Python? Q-5: What are the built-in types available in Python? Q-6: How to find errors or perform static analysis in Python applications? Q-7: When to use Python decorators? Q-8: What is the main difference between a list and a tuple? Q-9: How does Python handle memory management? Q-10: What is the main difference between lambda and def? Q-11: Use the python reg expression module "re" to write a reg expression to verify the email ID? Q-12: What do you think is the output of the following code snippet? Is there an error in the code? Q-13: Are there switch or case statements in Python? If not, what is the reason for the same? Q-14: What is the built-in function that Python uses to iterate over a sequence of numbers? Q-15: What are the possible optional statements in Python's try-except block? Q-16: What is a string in Python? Q-17: What is a slice in Python? Q-18: What is %s in Python? Q-19: Are strings immutable or mutable in Python? Q-20: What is the index in Python?
Let's start answering basic-level Python interview questions.
Q-1: What is Python, what are the benefits of using it, and what is your understanding of PEP 8?
What is Python
Benefits of Python programming
- Python is a dynamically typed language. This means that you don't need to mention the data type of the variable in the declaration. It allows setting variables such as var1=101 and var2="You are an engineer" without any errors.
- Python supports object-oriented programming because you can define classes as well as composition and inheritance. It does not use access specifiers such as public or private).
- Functions in Python are like first-class objects. It suggests that you can assign them to variables, return from other methods and pass them as parameters.
- Development with Python is fast, but running it is usually slower than compiled languages. Fortunately, Python can include "C" language extensions, so you can optimize the script.
- Python has many uses, such as web-based applications, test automation, data modeling, big data analysis, and so on. Or you can use it as a "glue" layer to handle other languages.
PEP 8 is the latest Python coding standard, a set of coding recommendations. It guides the provision of more readable Python code.
Q-2: What is the output of the following Python code snippet? Prove your answer.
def extendList ( val, list = ): list .append(val) return list list1 = extendList( 10 ) list2 = extendList( 123 ,) list3 = extendList( 'a' ) Print "List1% S =" % List1 Print "List2% S =" % List2 Print "list3 =% S" % list3 copy the code
The result of the above Python code snippet is:
list1 = [ 10 , 'a' ] list2 = [ 123 ] = list3 [ 10 , 'A' ] to copy the code
You might mistakenly think that list1 is equal to  and list3 matches ['a'], thinking that every time you call extendList, the list parameter will be initialized to its default value .
However, the process is like creating a new list after defining the function. Whenever someone calls the extendList method without a list parameter, the same method is used. It works like this, because the evaluation of the expression (in the default parameters) occurs when the function is defined, not during the call.
Therefore, list1 and list3 run on the same default list, and list2 runs on a separate object it creates (by passing an empty list as the value of the list parameter).
The definition of the extendList function can be changed in the following ways.
def extendList ( val, list = None ): if list is None : List =  List .append (Val) return List duplicated code
With this revised implementation, the output will be:
list1 = [ 10 ] list2 = [ 123 ] list3 = [ 'a' ] Copy code
Q-3: If the program does not require actions but the syntax requires, what statements can be used in Python?
The pass statement is a no-op. Nothing happened during execution. You should use the lowercase "pass" keyword. If you write "Pass", you will encounter errors like "NameError: name Pass is not defined". Python statements are case sensitive.
letter = "hai sethuraman" for i in letter: if i == "a" : pass print ( "pass statement is execute .............." ) else : print (I) Replication Code
Q-4: What is the process of using "~" to get the home directory in Python?
You need to import the os module, and then you can do the rest with just one line.
import os print (os.path.expanduser( '~' )) copying the code
Q-5: What are the built-in types available in Python?
The following is a list of the most commonly used built-in types supported by Python:
|Python's immutable built-in data types||Variable built-in data types of Python|
Q-6: How to find errors or perform static analysis in Python applications?
- You can use PyChecker, which is a static analyzer. It identifies errors in Python projects and reveals errors related to style and complexity.
- Another tool is Pylint, which checks whether Python modules meet coding standards.
Q-7: When to use Python decorators?
Python decorators are relative changes made in Python syntax for quick adjustment of functions.
Q-8: What is the main difference between a list and a tuple?
The main difference between a list and a tuple is that the former is mutable, while a tuple is not.
Tuples can be hashed, for example, using it as a dictionary key.
Q-9: How does Python handle memory management?
- Python uses a private heap to maintain its memory. So the heap contains all Python objects and data structures. Only the Python interpreter can access this area; programmers cannot use it.
- It is a Python memory manager that handles private heaps. It performs the required memory allocation for Python objects.
- Python uses a built-in garbage collector, which can reclaim all unused memory and unload it to the heap space.
Q-10: What is the main difference between lambda and def?
- Def can hold multiple expressions, while lambda is a single expression function.
- Def generates a function and assigns a name to call it later. Lambda forms a function object and returns it.
- def can have a return statement. Lambda cannot have return statement.
- Lambda supports use in lists and dictionaries.
Q-11: Use python reg expression module "re" to write a reg expression to verify email ID
Python has a regular expression module "re".
Look at the "re" expression that can check the email ID of the .com and .co.in subdomains.
import re print (re.search( r"[0-9a-zA-Z.]+@[a-zA-Z]+\.(com|co\.in)$" , "micheal.pages@mp. com" )) Copy code
Q-12: What do you think is the output of the following code snippet? Is there an error in the code?
List = [ 'A' , 'B' , 'C' , 'D' , 'E' ] Print ( List [ 10 :]) Copy the code
The result of the above line of code is . There will be no errors like IndexError.
You should know that trying to get members from the list using an index that exceeds the member count (for example, trying to access the list given in the question) will generate an IndexError. By the way, only the slices at the starting index that exceed no are retrieved. Items in the list will not cause IndexError. It will only return an empty list.
Q-13: Are there switch or case statements in Python? If not, what is the reason for the same?
No, there is no Switch statement in Python, but you can write a Switch function and use it.
Q-14: What is the built-in function that Python uses to iterate over a sequence of numbers?
Range() generates a list of numbers for iterative for loops.
for I in Range ( . 5 ): Print (I) copying the code
The range() function takes two sets of parameters.
- Stop: it is no. The zero-based integer to be generated. E.g. Range (3) == [0, 1, 2].
Range ([start], stop[, step])
- Start: This is the start number. the sequence of.
- Stop: It specifies the upper limit of the sequence.
- Step: The increment factor of the generated sequence.
- Only integer parameters are allowed.
- The parameter can be positive or negative.
- The range() function in Python starts at the zeroth index.
Q-15: What are the possible optional statements in Python's try-except block?
You can use two optional clauses in the try-except block.
- This is useful if you want to run a piece of code when the try block does not create an exception.
- When you want to execute some running steps, it is useful regardless of whether an exception occurs.
Q-16: What is a string in Python?
A string in Python is a series of alphanumeric characters. They are immutable objects. This means that once they are assigned, they are not allowed to be modified. Python provides a variety of methods, such as join(), replace(), or split(), to change the string. But none of these changes the original object.
Q-17: What is a slice in Python?
Slicing is a string operation that is used to extract part of a string or part of a list. In Python, a string (such as text) starts at index 0, and the nth character is stored at position text[n-1]. Python can also perform reverse indexing with the help of negative numbers, that is, reverse indexing. In Python, slice() is also a constructor that generates slice objects. The result is a set of indexes mentioned by range(start, stop, step). The slice() method allows three parameters. 1. start-the starting number at which the slice starts. 2. stop-the number that indicates the end of the slice. 3. step-value to be incremented after each index (default value = 1).
Q-18: What is %s in Python?
Python supports formatting any value as a string. It may contain quite complex expressions.
A common usage is to push the value into a string with the %s format specifier. Formatting operations in Python have a similar syntax to the C function printf().
Q-19: Are strings immutable or mutable in Python?
Python strings are indeed immutable.
Let us take an example. We have a "str" variable that holds the string value. We cannot change the container, which is the string, but we can modify the content it contains, which is the value of the variable.
Q-20: What is the index in Python?
Index is an integer data type that represents a position in an ordered list or string.
In Python, a string is also a list of characters. We can access them using indexes starting from zero to length minus one.
For example, in the string "Program", indexing happens like this:
Program 0 . 1 2 . 3 . 4 . 5 duplicated code
Summary-100 basic Python interview questions
I have been writing a technical blog for a long time, and this is an interview question I shared. Hope you will like it! Here is a summary of all my original and work source code:
Recommendations of excellent articles in the past:
- Teach you to use Java to make a gobang game
- 30 Python tutorials and tips Python topic month
- [Java practice questions] Java program output | The seventh set (including analysis)
- 5 VS Code extensions that make refactoring easy (collection recommended)
If you really learned something new from this article, like it, bookmark it and share it with your friends. Finally, don t forget to support it or