Python Interview Questions [part 3]
for part 2, check this link.
1. Difference between set and dictionary.
Set:
- st = {1, 2, 3}
- Unchangeable — we cannot use a list inside a set(due to mutability), but we can use a tuple inside a set(due to immutability).
- It is unordered.
- It is unique.
- It is mutable.
Dictionary:
- Dic = {1:”Sanjana”, 2: “Varun”}
- Keys are unique.
- It is ordered.
- Keys are immutable, but values are mutable.
2. State some operations of the set.
s = {1, 2, 3}
s.add(4) # Adds a single element
print(s) # Output: {1, 2, 3, 4}
s.update([5, 6, 7]) # Adds multiple elements (list, tuple, and set)
print(s) # Output: {1, 2, 3, 4, 5, 6, 7}
s.remove(3) # Removes 3, raises KeyError if not found
print(s) # Output: {1, 2, 4, 5, 6, 7}
s.discard(10) # Removes 10, no error if 10 is missing
print(s) # Output: {1, 2, 4, 5, 6, 7}
s.pop() # Removes a random element
print(s) # Output: {2, 4, 5, 6, 7}
s.clear() # Removes all elements
print(s) # Output: set()
# iterating over set
s = {10, 20, 30}
for item in s:
print(item)3. State some operations of the dictionary.
d = {"name": "Alice", "age": 25}
d["city"] = "New York" # Adds a new key-value pair
print(d)
'''
Output:
{'name': 'Alice', 'age': 25, 'city': 'New York'}
'''
d["age"] = 26 # Updates an existing value
print(d)
'''
Output:
{'name': 'Alice', 'age': 26, 'city': 'New York'}
'''
d.update({"country": "USA", "email": "alice@example.com"}) # Adding multiple items
print(d)
'''
Output:
{'name': 'Alice', 'age': 26, 'city': 'New York', 'country': 'USA', 'email': 'alice@example.com'}
'''
del d["city"] # Removes 'city' key
print(d)
'''
Output:
{'name': 'Alice', 'age': 26, 'country': 'USA', 'email': 'alice@example.com'}
'''
d.pop("email") # Removes email and returns its value
print(d)
'''
Output:
{'name': 'Alice', 'age': 26, 'country': 'USA'}
'''
d.popitem() # Removes the last inserted item (in Python 3.7+)
print(d)
'''
Output:
{'name': 'Alice', 'age': 26}
'''
d.clear() # Removes all items
print(d)
'''
Output:
{}
'''
# iterating over dictionaries
d = {"name": "Alice", "age": 26, "city": "New York"}
for key in d.keys(): # iterate over keys
print(key)
for value in d.values(): # iterate over values
print(value)
for key, value in d.items(): # iterate over key and value
print(key+ ": " + value)4. State some operations of the tuple.
tup = (1, 2, 3, 4)
print(tup[1]) # 3
print(tup[-1]) # 4
tup = (1,2,3,2,4,2)
print(tup.count(2)) # Output: 3 (Number of times 2 appears)
print(tup.index(3)) # Output: 2 (First occurrence of 3)
tup = (10, 20, 30)
a, b, c = tup # Unpacking tuple into variables
print(a, b, c) # Output: 10 20 30
for item in tup:
print(item)5. State some operations of the list.
lis = [1, 2, 3]
lis.append(4) # Adds 4 at the end of the list
print(lis) # [1, 2, 3, 4]
lis.insert(1, 10) # Adds 10 at index 1
print(lis) # [1, 10, 2, 3, 4]
lis.extend([4, 5, 6, 7]) # Adds multiple elements
print(lis) # [1, 10, 2, 3, 4, 4, 5, 6, 7]
lis.remove(10) # Removes the first occurence of 10 and if 10 doesn't exists, it will throw ValueError Exception
print(lis) # [1, 2, 3, 4, 4, 5, 6, 7]
lis.pop(3) # Removes the element at given index
print(lis) # [1, 2, 3, 4, 5, 6, 7]
del lis[6] # Removes an element from 6th index
print(lis) # [1, 2, 3, 4, 5, 6]
lis.clear() # Removes all the elements
print (lis) # []
lis = [1, 2, 3, 4]
lis.sort(reverse = True) # Sorts in descending order
print(lis) # [4, 3, 2, 1]
print(lis.count(2)) # count occurrences of 2 in the list, here output: 1
for item in lis:
print(item) # Access every item in a given list5. What is a swapcase in Python?
- A string’s function converts all the upper-case characters into lowercase and vice-versa.
- This method creates the copy of the string which contains all the characters in the swapcase.
string = "SanjanaPoptani"
string.swapcase()
print(string)
# Output:
# sANJANApOPTANI6. Built-in datatypes in Python.
Numeric:
- The numeric data type in Python represents the data with a numeric value.
- A numeric value can be an integer, a floating number, a Boolean, or a complex number.
Sequences:
- The Sequence datatype in Python is the ordered collection of heterogenous datatypes.
- A sequence type can be a String, List, Tuple, or a range.
Mapping:
- The mapping objects are used to map hash table values to arbitrary objects.
- In Python, there is currently only one common mapping type, the dictionary.
- It is a key-value pair, where the key is immutable and values are mutable.
Get Bigie Billie’s stories in your inbox
Join Medium for free to get updates from this writer.
Set:
- In Python, a Set is an unordered collection of data types that is iterable, mutable, and has no duplicate elements.
7. Which sorting technique is used by sort() and sorted() functions of python?
- Python uses the Tim Sort algorithm for sorting.
- It’s a stable sorting whose worst case is O(N log N).
- It’s a hybrid sorting algorithm, derived from merge sort and insertion sort, designed to perform well on many kinds of real-world data.
8. Does Python support multiple inheritance?
- Yes
9. How to delete a file using the Python?
- We can delete a file using Python by following approaches:
- os.remove()
- os.unlink()
10. What is namespace in Python?
- A namespace is a system that has a unique name for every object in Python.
- An object might be a variable or a method.
- Python itself maintains a namespace in the form of a Python dictionary.
- The Python interpreter understands what exact method or variable one is trying to point to in the code, depending upon the namespace.
- So, the division of the word itself gives a little more information. Its Name (which means name, a unique identifier) + Space(which talks about something related to scope).
Types of namespaces:
- Global namespace
- Local namespace
- Builtin namespace
The built-in namespace encompasses the global namespace, which contains the local namespace.
Scope of Objects in Python:
- Scope refers to the coding region from which a particular Python object is accessible.
- Hence one cannot access any particular object from anywhere from the code, the accessing has to be allowed by the scope of the object.
# Python program showing
# a scope of object
def some_func():
print("Inside some_func")
def some_inner_func():
var = 10
print("Inside inner function, value of var:",var)
some_inner_func()
print("Try printing var from outer function: ",var)
some_func()Inside some_func
Inside inner function, value of var: 10
Traceback (most recent call last):
File "/home/1eb47bb3eac2fa36d6bfe5d349dfcb84.py", line 8, in
some_func()
File "/home/1eb47bb3eac2fa36d6bfe5d349dfcb84.py", line 7, in some_func
print("Try printing var from outer function: ",var)
NameError: name 'var' is not definedThe lifetime of a namespace :
- The lifetime of a namespace depends upon the scope of objects, if the scope of an object ends, the lifetime of that namespace comes to an end.
# var1 is in the global namespace
var1 = 5
def some_func():
# var2 is in the local namespace
var2 = 6
def some_inner_func():
# var3 is in the nested local
# namespace
var3 = 7- But in some cases, one might be interested in updating or processing global variables only, as shown in the following example, one should mark it explicitly as global and the update or process.
- Note that the line “count = count +1” references the global variable and therefore uses the global variable, but compare this to the same line written “count = 1”.
- Then the line “global count” is needed according to scope rules.
# Python program processing
# global variable
count = 5
def some_method():
global count
count = count + 1
print(count)
some_method()6Stay tuned for the next part…

![Django Interview Questions [Part -1]](https://miro.medium.com/v2/resize:fit:1358/format:webp/1*h9k2K8ICHBf_crOfayVMgQ.png)

![Python Interview Question [last part]](https://miro.medium.com/v2/resize:fit:1358/format:webp/1*dgJQH0faYd3EPAIW64kIuw.png)


