Python Interview Questions and Answers: Singleton, Lambda, Type Conversion, Functions, Memory Management, and More
This article presents a collection of common Python interview questions covering topics such as implementing the singleton pattern, using lambda functions, type conversion, function definition, memory management, reverse iteration, list‑tuple conversion, duplicate removal, file operations, random numbers, email sending, object copying, static analysis tools, global variables, regex matching, and exception handling.
1. Singleton pattern in Python can be implemented using a metaclass or a decorator. Example code shows a Singleton metaclass and a decorator approach.
2. Lambda functions are anonymous one‑line functions that can take any number of arguments and return the result of a single expression. Examples demonstrate basic usage and immediate invocation.
3. Type conversion functions such as int(), float(), and str() convert values between types, with examples of successful and failing conversions.
4. Functions are defined with the def keyword, must have a name starting with a letter, and use indentation to delimit the body. An example of an addition function is provided.
5. Memory management in Python is handled by the interpreter, relieving developers from manual allocation.
6. To iterate a sequence in reverse, use list.reverse() for lists or a reversed index loop for other sequences.
7. Converting between tuples and lists is done with tuple(seq) and list(seq), preserving element order.
8. Removing duplicate elements from a list can be done by sorting the list and deleting consecutive duplicates, as shown in the code example.
9. File operations: delete a file with os.remove() or os.unlink(); copy a file with shutil.copyfile().
10. Generating random numbers uses the random module, e.g., random.random() returns a float in [0,1).
11. Sending email via SMTP can be done with the smtplib library; a sample script reads a message from stdin and sends it.
12. Object copying uses copy.copy() or copy.deepcopy(); dictionaries have a copy() method.
13. Static analysis tools such as PyChecker and Pylint help find bugs and enforce coding standards.
14. Global variables inside a function are declared with the global statement.
15. In regular expressions, <.*> is greedy, while <.*?> is non‑greedy, affecting how HTML tags are matched.
16. re.match() checks only at the start of a string, whereas re.search() scans the whole string for a match.
17. The except clause catches exceptions; examples show catching all exceptions versus specific ones like IOError.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
