Question 8: Difference between append and extend in python list ?
Answer:
Append: Append method in python list is used add an element at the end of the list.
Extend: extend() method concatenates the first list with another list (or another iterable).
iterable: Iterable is anything that can be loop over. (string, tuple, list etc...)
Q: Write a Python program to extend a list without append.
#extend a list
x = [10, 20, 30]
y = [40, 50, 60]
string="goeduhub"
x.extend(y)
print(x)
x.extend(string)
x
|
Output
[10, 20, 30, 40, 50, 60]
[10, 20, 30, 40, 50, 60, 'g', 'o', 'e', 'd', 'u', 'h', 'u', 'b']
Example of append in list
#append an element to list
x = [10, 20, 30]
x.append("hello")
x.append(4)
print(x)
|
Output:
[10, 20, 30, 'hello', 4]
Note that append method add only one element at a time at the end of list and extend method concatenates the other iterable (list, tuple, string, set ) to the first one.
Question 9: How to insert an element into python list at specified position ?
Answer: insert method is used to insert an element at specified position in python list. As we know append and extend methods are not use a specified position.
see the example below
#insert an element to list
x = [10, 20, 30]
x.insert(0, 'red')
x.insert(2, 452)
print(x)
|
Output:
['red', 10, 452, 20, 30]
Question 10: What is difference between object oriented language and Procedural Programming language ?
Answer : We know that python support both Object Oriented and Procedural Programming language. But what is OOP and POP ?
Both approaches used to construct a programming structure.
Procedural oriented programming: As the name suggest, Procedural oriented programming follows a step-by-step approach to break down a task into a collection of variables and function. Each step follow the previous step so that computer can understand what to do after each step. POP is less secure then OOP. Procedural programming follows top down approach.
Example: Pascal , C etc...
Object oriented language:
Object oriented programming is based on real world. The concept is similar as we all time surrounded object.
For example if you are working on a computer you are surrounded by objects like computer , water bottle , pen etc... and the work of particular object is fixed and limited. For example we use pen to write something not to drink.
The similar concept is apply on the object oriented programming.
In object oriented programming, program is divided into small parts called objects.
Object oriented programming follows bottom up approach.
Example: C++, Java, Python, C# etc.
Question 11: Write a python program to show keys and values of a dictionary in pair also sort it by keys?
Answer : Python Dictionaries : Click here to see the basics of python dictionaries
#python dictionary keys and values in pair
d = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
print(sorted(d.items()))
|
Output:
[(0, 0), (1, 2), (2, 1), (3, 4), (4, 3)]
Note: This is a very simple dictionary question but why I choose this question is just to show you the item() function in dictionary.
You can see that item() in dictionary return the key, value pair. Sorted function sort a dictionary by its keys as you see in the output.
Question 12: Write a python program to merge two dictionaries ?
Answer :
#merging two dictionaries
d1 = {'a': 10, 'b': 20}
d2 = {'x': 30, 'y': 40}
d1.update(d2)
print(d1)
|
Output:
{'a': 10, 'b': 20, 'x': 30, 'y': 40}
Note: Update method is used to entre or insert a new key, value in dictionary.
Question 13: Difference between pop() and del in python ?
Answer : pop() - pop remove an item at particular index and return it.
del - del keyword just remove an item or an object (dictionary, list etc...).
See the example below (pop() and del on dictionary in python)
#pop method in python
d1={1:'a',2:'b',3:'c',4: 'd',5:'e'}
d1.pop(3)
|
Output:
'c'
#priting d1 after popping key 3
print (d1)
|
Output:
{1: 'a', 2: 'b', 4: 'd', 5: 'e'}
#delete in python
d1={1:'a',2:'b',3:'c',4: 'd',5:'e'}
del d1[4]
print(d1)
|
Output:
{1: 'a', 2: 'b', 3: 'c', 5: 'e'}
Note: You can see in the examples that pop() and del both remove the items form dictionary. But in case of pop(), pop() return the popped item ('c' in this case).
Question 14: Write a Python program to remove an empty tuple(s) from a list of tuples.
Answer: Python tuples : Click here for basic concept of python tuples
#removing empty tuples from list
L = [(), (), ('',), ('a', 'b'), ('a', 'b', 'c'), ('d')]
t=()
l=[]
for i in L:
if i is not t:
l.append(i)
print(l)
|
Output
[('',), ('a', 'b'), ('a', 'b', 'c'), 'd']
Question 15: Write a python program to add an element in tuple ?
Answer : As we know that tuples are immutable object type in python we can not add add new elements means we can't update a tuple. We can create a tuple . Let's see how it works
#creating a tuple
tup = (1,2,3,4,5,6,6,7,8,9)
new_tup = tup + (10,)
print("adding elements in tuple",new_tup)
#adding element in a specific index
s_tup = (1,2,3,4,5,6,6,7,8,92,44,67,865,44)
s_tup = s_tup[:4] + (11,34,56) + s_tup[:6]
print("adding elements in tuple at specific index",s_tup)
|
Output
adding elements in tuple (1, 2, 3, 4, 5, 6, 6, 7, 8, 9, 10)
adding elements in tuple at specific index (1, 2, 3, 4, 11, 34, 56, 1, 2, 3, 4, 5, 6)
Python Tutorial
Machine Learning Tutorial