Answers with Verified Solutions
What happens if you try to add a string and an integer in Python?
✔✔It will raise a `TypeError` because Python doesn’t allow concatenation of different data types
directly.
How do you remove all elements from a dictionary in Python?
✔✔You can use the `clear()` method, like `my_dict.clear()`.
What is the result of `5 % 2` in Python?
✔✔It returns `1`, as `%` is the modulus operator and gives the remainder of the division.
How can you merge two sets in Python?
✔✔You can use the `union()` method or the `|` operator, like `set1 | set2`.
What does the `sorted()` function do in Python when used with a dictionary?
✔✔It sorts the dictionary by its keys and returns a sorted list of keys.
1
,How do you check if a string contains only numbers in Python?
✔✔You can use the `.isdigit()` method, like `my_string.isdigit()`.
What is the purpose of the `try` and `except` block in Python?
✔✔It allows you to handle exceptions, preventing your program from crashing if an error occurs.
What happens when you use a negative index in a list in Python?
✔✔It accesses elements starting from the end of the list, with `-1` being the last item.
How can you combine two strings with a separator in Python?
✔✔You can use the `join()` method, like `", ".join(my_list)`.
What is the `lambda` keyword used for in Python?
✔✔It is used to create small anonymous functions, like `lambda x: x * 2`.
What does the `isdigit()` method check for in Python?
✔✔It checks if all characters in a string are digits.
2
, How do you convert a float to an integer in Python?
✔✔You can use the `int()` function, like `int(3.7)`.
What does the `pop()` method do for a dictionary?
✔✔It removes a key-value pair by key and returns the value associated with that key.
How can you create a set in Python?
✔✔You can use curly braces or the `set()` constructor, like `my_set = {1, 2, 3}`.
How do you get the index of an element in a list in Python?
✔✔You can use the `index()` method, like `my_list.index("apple")`.
What does `None` represent in Python?
✔✔`None` represents the absence of a value or a null value.
How do you check if a list is empty in Python?
✔✔You can use `if not my_list:` or check the length with `len(my_list) == 0`.
3