Question: What is the difference between pop() and remove() methods in Python? Answer: The pop() method removes and returns an element from a list by its index, while the remove() method deletes the first occurrence of a specified value.
Question: Explain the difference between sort() and sorted() in Python. Answer: The sort() method sorts a list in-place, modifying the original list. The sorted() function returns a new sorted list, leaving the original list unchanged.
Question: How do append() and extend() differ in Python? Answer: The append() method adds an element to the end of a list. The extend() method takes an iterable and adds its elements to the end of the list.
Question: What distinguishes a string from a list in Python? Answer: A string is a sequence of characters, and its elements are individual characters. A list is a collection of items, which can be of various data types.
Question: Compare the for and while loops in Python. Answer: The for loop is used to iterate over a sequence, and the number of iterations is known beforehand. The while loop repeats a block of code as long as a specified condition is true.
Question: How does a dictionary differ from a list in Python? Answer: A dictionary stores data in key-value pairs, while a list stores data as an ordered collection of items.
Question: Define the difference between a syntax error and a runtime error in Python. Answer: A syntax error occurs when the code violates the language’s rules. A runtime error occurs during program execution when an operation cannot be performed correctly.