pythonvivaquestions
pythonvivaquestions
2. List Methods
Q: What is the purpose of the insert() method in a list?
A: The insert() method adds an element at a specified index in a list. For
example, list.insert(1, 'a') inserts 'a' at index 1.Q: How does
the remove() method differ from pop()?
A: The remove() method removes the first occurrence of a specified value
from the list, while pop() removes an element at a specified index (or the last
element if no index is provided) and returns it.
3. Tuple Methods
Q: Why are tuples considered immutable?
A: Tuples are immutable because once they are created, their elements
cannot be changed, added, or removed. This property makes them useful for
fixed collections of items.Q: How can you check if an item exists in a
tuple?
A: You can check for an item in a tuple using the in keyword. For
example, item in my_tuple returns True if item exists in my_tuple.
4. Dictionary Methods
Q: What is a dictionary in Python, and how is it different from a list?
A: A dictionary is an unordered collection of key-value pairs where each key
must be unique. Unlike lists, which are ordered collections indexed by
integers, dictionaries use keys to access values.Q: What does
the get() method do?
A: The get() method retrieves the value associated with a specified key in a
dictionary. It returns None if the key does not exist instead of raising an error.
if number > 0:
print("Positive")
else:
print("Negative")
now = datetime.datetime.now()
print(now)
import math
area = math.pi * radius ** 2