What are Python’s 8 main data types?
int, float, str, bool, list, tuple, set, dict.
What is the difference between a list and a tuple?
Lists are mutable (can change), tuples are immutable (cannot change).
How do you create a dictionary in Python?
Using curly braces: my_dict = {“key1”: value1, “key2”: value2}.
What is a Python set?
An unordered collection of unique elements.
How do you check the type of a variable?
Use type(variable).
What is the difference between == and is?
== checks value equality, is checks object identity (memory location).
How do you write a Python function?
Using def: def my_function(arg1, arg2): return result.
What are *args and **kwargs used for?
*args allows passing variable number of positional arguments, **kwargs for keyword arguments.
How do you handle exceptions in Python?
Using try, except, finally blocks.
How do you read a CSV file using Python?
Using pandas: df = pd.read_csv(‘file.csv’).
How do you iterate over a list?
Using for item in my_list: or list comprehensions [x for x in my_list].
What is a Python list comprehension?
A concise way to create lists: [x*2 for x in my_list if x>0].
What are Python modules and packages?
Modules are .py files; packages are directories of modules with __init__.py.
How do you import a module?
import module_name or from module_name import function_name.
What is the difference between shallow and deep copy?
Shallow copy copies the object but references nested objects; deep copy copies everything recursively (copy.deepcopy()).
What is Python’s lambda function?
An anonymous function: lambda x: x**2.
What is the difference between append() and extend() for lists?
append() adds one element, extend() adds all elements from an iterable.
How do you check if a key exists in a dictionary?
Using if key in my_dict:.
What is Python’s enumerate() function?
Returns index and value when iterating: for i, v in enumerate(my_list):.
How do you sort a list in Python?
Using my_list.sort() (in-place) or sorted(my_list) (returns new list).