Python Interview Questions
A comprehensive collection of Python programming interview questions covering fundamental concepts, syntax, and practical applications.
Understand the Problem
Problem Statement
Overview
This is a comprehensive set of Python interview questions designed to test fundamental programming concepts and practical knowledge. The questions cover various aspects of Python including:
- Python language fundamentals and history
- Core programming concepts (functions, operators, data structures)
- File handling and I/O operations
- Memory management and namespace
- Advanced features (decorators, generators, iterators)
- String manipulation and data processing
Each question tests understanding of Python syntax, best practices, and problem-solving approaches.
Constraints
- All code examples should be syntactically correct Python 3.x
- Answers should demonstrate proper Python coding practices
- Time complexity should be considered for algorithmic questions
- Memory usage should be efficient for data processing tasks
- Code should be readable and well-structured
Examples
Question: What is the output of the following code?
list = [12,25,15,65,58,14,5]
odd = 0
for val in even:
if val%2!=0:
odd = val
break
print(val)
print('odd value found',odd)2
4
6
8
10
odd value found 11The code demonstrates the break statement in Python. It iterates through a list, prints even numbers, and breaks the loop when it finds the first odd number, then prints that odd number.
Question: How to create a Unicode string in Python 3?In Python 3, strings are Unicode by default. You can create Unicode strings using:
- Direct assignment: text = "Hello"
- Using encode: text.encode('utf-8')
- Using unicode escapes: text = "\u03A9" for ΩPython 3 treats all strings as Unicode by default, unlike Python 2 where you needed explicit unicode() calls. The string type in Python 3 is equivalent to unicode in Python 2.
Question: What is the difference between list and tuple in Python?List is mutable while tuple is immutable.
- List: my_list = [1, 2, 3] - can be modified
- Tuple: my_tuple = (1, 2, 3) - cannot be modified after creationThis is a fundamental difference in Python data structures. Lists use square brackets and can be modified (add, remove, change elements), while tuples use parentheses and are fixed once created, making them faster and safer for read-only data.
Solution
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// Example: String manipulation similar to Python strip() function
void strip_whitespace(char *str) {
char *end;
// Trim leading whitespace
while(isspace((unsigned char)*str)) str++;
// Trim trailing whitespace
if(*str == 0) return;
end = str + strlen(str) - 1;
while(end > str && isspace((unsigned char)*end)) end--;
// Write new null terminator
*(end + 1) = 0;
}
// Example: Basic array operations similar to Python list operations
void print_array(int arr[], int size) {
printf("[");
for(int i = 0; i < size; i++) {
printf("%d", arr[i]);
if(i < size - 1) printf(", ");
}
printf("]\n");
}
// Example: Function to find minimum similar to Python min() function
int find_min(int arr[], int size) {
if(size == 0) return 0;
int min_val = arr[0];
for(int i = 1; i < size; i++) {
if(arr[i] < min_val) {
min_val = arr[i];
}
}
return min_val;
}
int main() {
// Example usage
char text[] = " Hello World ";
printf("Original: '%s'\n", text);
strip_whitespace(text);
printf("Stripped: '%s'\n", text);
int numbers[] = {5, 2, 8, 1, 9};
int size = sizeof(numbers) / sizeof(numbers[0]);
printf("Array: ");
print_array(numbers, size);
printf("Minimum: %d\n", find_min(numbers, size));
return 0;
}This C implementation demonstrates several Python concepts translated to C:
- String manipulation: The strip_whitespace() function mimics Python's strip() method by removing leading and trailing whitespace
- Array operations: The print_array() function shows how to iterate through and display array elements, similar to printing a Python list
- Function implementation: The find_min() function implements the logic of Python's min() built-in function
Key differences from Python:
- C requires explicit memory management
- Arrays have fixed size determined at compile time
- String manipulation requires manual character-by-character processing
- No built-in high-level functions like Python provides