NEW
Font size
WorksheetsMastering Python Concepts
Total questions: 20
Worksheet time: 10mins
What is a list in Python and how is it different from a tuple?
A list can only contain numbers, while a tuple can contain any data type.
Lists and tuples are both mutable and can be changed at any time.
A list is mutable and can be changed, while a tuple is immutable and cannot be changed.
A list is immutable and cannot be changed, while a tuple is mutable and can be changed.
Explain the use of dictionaries in Python. How do you access values?
Dictionaries are used to store lists of items.
Values are accessed using 'dictionary_name.value'.
Dictionaries can only contain string keys.
Dictionaries store data in key-value pairs, accessed using 'dictionary_name[key]'.
What are sets in Python and what are their main characteristics?
Sets in Python are defined using square brackets or list() constructor.
Sets in Python are unordered collections of unique elements, defined by curly braces or set() constructor, and support operations like union and intersection.
Sets in Python are ordered collections of duplicate elements.
Sets can only contain integer values in Python.
How do you create a stack using a list in Python?
Utilize a tuple and perform stack operations by reassigning the tuple for push and using indexing for pop.
Create a stack using a set and use set.add() to push and set.remove() to pop.
Use a list and implement stack operations with list.append() to push and list.pop() to pop.
Use a dictionary to implement stack operations with dict.update() to push and dict.pop() to pop.
What is the difference between a shallow copy and a deep copy in Python?
A shallow copy and a deep copy are the same, both create a complete duplicate of the original object.
A shallow copy creates a new object with references to the same inner objects, while a deep copy creates a new object with copies of the inner objects.
A shallow copy copies the outer object only, while a deep copy copies both the outer and inner objects.
A shallow copy is faster than a deep copy because it copies all objects recursively, while a deep copy only copies the outer object.
How do you read a text file in Python?
Use 'with open("filename.txt", "r") as file: content = file.read()' to read a text file.
Use 'open("filename.txt")' to read a text file.
Read a text file using 'file.read("filename.txt")'.
Use 'open("filename.txt", "w")' to read a text file.
What is the purpose of the 'with' statement when handling files?
The 'with' statement ensures proper management of file resources by automatically closing the file after its block of code is executed.
The 'with' statement is used to create a new file.
The 'with' statement allows multiple files to be opened simultaneously.
The 'with' statement is a way to read files without closing them.
How can you write data to a file in Python?
Use open('filename.txt', 'a') to delete the file contents.
Use open('filename.txt', 'r') to read a file.
Use file.write('data') without opening the file first.
Use open('filename.txt', 'w') to open a file and file.write('data') to write data.
What is exception handling in Python?
Exception handling is a method to optimize code performance.
Exception handling is only for syntax errors.
Exception handling requires the use of 'if' statements.
Exception handling in Python is a way to manage errors using 'try' and 'except' blocks.
How do you raise an exception in Python?
raise ExceptionType
raise 'Error message'
raise ExceptionType('Error message')
throw ExceptionType('Error message')
What is the purpose of the 'try' and 'except' blocks?
To import libraries in Python
The purpose of 'try' and 'except' blocks is to handle exceptions and errors in a controlled manner.
To create loops in Python
To define functions in Python
How can you handle multiple exceptions in a single block?
Use 'try-except' without specifying exception types.
Catch exceptions using 'finally' block only.
Handle exceptions by ignoring them completely.
Use 'except (ExceptionType1, ExceptionType2) as e:' to catch multiple exceptions.
What is the difference between 'finally' and 'else' in exception handling?
'Finally' is used to catch exceptions; 'Else' is used to throw exceptions.
'Finally' only executes if no exceptions occur; 'Else' executes regardless of exceptions.
'Finally' executes regardless of exceptions; 'Else' executes only if no exceptions occur.
'Finally' can only be used with try blocks; 'Else' can be used anywhere in the code.
How do you open a file in write mode and what happens if the file already exists?
Use open('filename', 'w'). If the file exists, it will be overwritten.
Use open('filename', 'a'). The file will be deleted.
Use open('filename', 'r'). It will append to the file.
Use open('filename', 'x'). It will create a new file only.
What are the common built-in exceptions in Python?
FileNotFoundError
ImportError
ValueError, TypeError, KeyError, IndexError, ZeroDivisionError
AttributeError
How can you read a CSV file in Python?
Reading CSV files is not possible in Python
Using 'csv.load()' function
You can only read CSV files with Excel
You can read a CSV file in Python using 'csv.reader()' or 'pandas.read_csv()'.
What is the purpose of the 'json' module in Python?
To perform mathematical calculations
To create and manage databases
To format XML data in Python
The purpose of the 'json' module in Python is to handle JSON data by converting between JSON format and Python objects.
How do you handle errors when converting a string to an integer?
Ignore the error and continue execution.
Use a try-catch block to handle conversion errors.
Use a print statement to display the error.
Convert the string to a float instead.
What is the difference between 'open()' and 'file()' in Python?
The difference is that 'open()' is used in Python 3 to open files, while 'file()' is not available in Python 3.
Both 'open()' and 'file()' are interchangeable in Python 3.
'open()' is a built-in function, while 'file()' is a method of the file object.
'file()' is used in Python 2 to open files, while 'open()' is used in Python 3.
How can you check if a file exists before trying to open it?
Use 'os.path.exists(file_path)' to check if a file exists before opening it.
Use 'file.exists(file_path)' to check if a file exists.
Check the file size to determine if it exists.
Attempt to open the file and catch any errors.
