PYTHON REVISION TOUR -II

1. What is a string in Python?
Ans :- A sequence of characters enclosed in quotes (`’`, `”`, or `”’`).
2. How do you create a multi-line string?
Ans :- Using triple quotes (`”’` or `”””`).
multiline = ”’This is a
multi-line string.”’

3. What is string slicing?
Ans :- Extracting a part of the string using indices.
s = “Python”
print(s[1:4]) # Output: yth

4. How do you find the length of a string?
Ans :- Using `len()` function.
len(“Python”) # Output: 6
5. What is the difference between `find()` and `index()` methods in strings?
Ans :- `find()` returns `-1` if the substring is not found, while `index()` raises an error.
6. How can you reverse a string in Python?
Ans :-
s = “Python”
print(s[::-1]) # Output: nohtyP

7. What is the output of `”hello”.upper()`?
Ans :- `”HELLO”`
8. What does the `strip()` method do?
Ans :- Removes leading and trailing spaces.
9. What is a list in Python?
Ans :- A collection of items that are ordered, mutable, and allow duplicate elements.
10.How do you add an element to a list?
Ans :- Using `append()` or `insert()`.
Example:

lst = [1, 2, 3]
lst.append(4)
11. How do you remove an element from a list?
Ans :- Using `remove()` or `pop()`.
12. **How do you access elements in a list?**
Ans :- Using indices.
Example:
lst = [10, 20, 30]
print(lst[1]) # Output: 20

13. What is list slicing?
Ans :- Extracting a part of the list using indices.
lst = [1, 2, 3, 4]
print(lst[1:3]) # Output: [2, 3]

14. How do you concatenate two lists?
Ans :-
lst1 = [1, 2]
lst2 = [3, 4]
print(lst1 + lst2) # Output: [1, 2, 3, 4]

15. What does the `sort()` method do?
Ans :- Sorts the list in place in ascending order.
16. How do you copy a list?
Ans :-
lst = [1, 2, 3]
new_lst = lst.copy()

17. What is the difference between `extend()` and `append()`?
Ans :- `extend()` adds elements of another list; `append()` adds the entire list as a single element.
18. How do you check if an item exists in a list?
Ans :-
lst = [1, 2, 3]
print(2 in lst) # Output: True

19. What are the different ways to sort a list?
Ans :- Using `sort()` (in-place) or `sorted()` (returns a new list).
20. How do you sort a list in descending order?
Ans :-
lst = [3, 1, 2]
lst.sort(reverse=True)

21. What does the `key` parameter do in sorting?
Ans :- Specifies a function to determine the sorting criteria.
Example:
lst = [“apple”, “banana”, “cherry”]
lst.sort(key=len)

22. How do you sort a list of tuples by the second element?
Ans :-
lst = [(1, 2), (3, 1), (5, 0)]
lst.sort(key=lambda x: x[1])
23. What is the difference between `sort()` and `sorted()`?
Ans :- `sort()` modifies the original list, while `sorted()` returns a new sorted list.
24. What is a tuple in Python?
Ans :- A collection of items that is ordered and immutable.
25. How do you create a tuple with one element?
Ans :- Add a trailing comma.
Example:- t = (1,)

26. What is tuple unpacking?
Ans :- Assigning tuple elements to variables.
Example:
t = (1, 2, 3)
a, b, c = t
27. Can you modify a tuple?**
Ans :- No, tuples are immutable.
28. How do you concatenate two tuples?
Ans :-
t1 = (1, 2)
t2 = (3, 4)
print(t1 + t2) # Output: (1, 2, 3, 4)
29. How do you check if an item exists in a tuple?
Ans :-
t = (1, 2, 3)
print(2 in t) # Output: True

30. What is a dictionary in Python?
Ans :- A collection of key-value pairs.
31. How do you create a dictionary?
Ans :- d = {“name”: “Alice”, “age”: 25}

32. How do you access a value in a dictionary?
Ans :- Using the key.
Example: print(d[“name”])

33. How do you add a new key-value pair?
Ans :- d[“city”] = “New York”

34. How do you remove a key-value pair?
Ans :- Using `del` or `pop()`.
del d[“name”]
d.pop(“age”)

35. What is the `keys()` method?
Ans :- Returns a view object of all keys in the dictionary.
36. What does the `get()` method do?
Ans :- Returns the value of a key, or a default value if the key doesn’t exist.
37. Can dictionary keys be mutable?
Ans :- No, keys must be immutable types like `int`, `str`, `tuple`.
38. What is dictionary comprehension?
Ans :- A concise way to create dictionaries.
Example: squares = {x: x**2 for x in range(5)}

39. How do you iterate over a dictionary?
Ans :- for key, value in d.items():
print(key, value)
40. What happens if you try to access a key that doesn’t exist?
Ans :- It raises a `KeyError`.
 

Leave a Reply

Your email address will not be published. Required fields are marked *

sign up!

We’ll send you the hottest deals straight to your inbox so you’re always in on the best-kept software secrets.