Assignment 2
Assignment 2
1. What is a List?
Ans:
2. What is a Tuple?
Ans:
Ans:
Ans:
In [1]:
1 l1=[12,45,32,85,15]
2 max1 = 0
3 for i in l1:
4 if i > max1:
5 max1 = i
6 else:
7 pass
8 print("Largest element in list >>>",max1)
In [2]:
1 l1=[12,45,32,85,15]
2 l1.sort()
3 print("Largest element >>",l1[-1])
Ans:
In [3]:
1 list1=["python",98,22,[10,30],"data",100]
2 print("Original list is: ",list1)
3
4 list1[0], list1[-1] = list1[-1], list1[0]
5
6 print("Updated list is : ",list1)
Original list is: ['python', 98, 22, [10, 30], 'data', 100]
Updated list is : [100, 98, 22, [10, 30], 'data', 'python']
Ans:
In [4]:
1 list2= [23,63,45,78,96]
2 print("Original list is: ",list2)
3
4 # We are swaping 1st index item with 3rd index item
5
6 list2[1], list2[3] = list2[3], list2[1]
7
8 print("Updated list is : ",list2)
Ans:
In [16]:
1 l1 = [1,2,3,4,5,6]
2 l2 = l1[::-1]
3 l2
Out[16]:
[6, 5, 4, 3, 2, 1]
In [6]:
Original list is: [10, 63, 1.556, 96, 'python', 45.63, 'machine learning',
'data science']
Reversed list is: ['data science', 'machine learning', 45.63, 'python', 9
6, 1.556, 63, 10]
In [5]:
Original list is: [10, 63, 1.556, 96, 'python', 45.63, 'machine learning',
'data science']
Reversed list is: ['data science', 'machine learning', 45.63, 'python', 9
6, 1.556, 63, 10]
Ans:
In [7]:
1 l1= [2,3,2,2,4,5,3]
2
3 for i in set(l1):
4 print(f"{i} occurs {l1.count(i)} times in list")
In [19]:
1 l1= [2,3,2,2,4,5,3]
2 ele = int(input("Enter an element :"))
3 print(f"{ele} occurs {l1.count(ele)} times in list")
Enter an element :2
2 occurs 3 times in list
Ans:
In [8]:
1 l1 = [1,2,3,4,5]
2 s = 0
3 for i in l1:
4 s = s+i
5 print("sum of all element in a list :", s)
Ans:
In [9]:
1 l1 =[ 1,2,3,4,5,6]
2 m = 1
3 for i in l1:
4 m=m*i
5 print("Multiplication of all elements :",m)
Ans:
1 Method1: you can find it by using len() function
In [12]:
1 list1 = [10,30,12,63,"python","science"]
2 print("length of given list is :",len(list1))
In [13]:
1 l1 = [10,30,12,63,"python","science"]
2 l = 0
3 for i in l1:
4 l=l+1
5 print("length of given list is :",l)
12. WAP to find the smallest and largest number in a list (Without min-max
function)
Ans:
In [14]:
1 l1 = [10,96,5,44,52]
2 for i in range(len(l1)):
3 for j in range(i+1,len(l1)):
4 if l1[i] < l1[j]:
5 l1[i],l1[j] = l1[j],l1[i]
6
7 print("sorted list :",l1)
8 print("maximum value :",l1[0])
9 print("minimum value :",l1[-1])
Ans:
In [42]:
14. Take inputs from the user to make a list. Again take one input from the user
and search it in the list and delete that element, if found. Iterate over a list
using for loop
Ans:
In [84]:
1 list1=[]
2
3 a=int(input("enter length of list:"))
4 for i in range (a):
5 list1.append(int(input("enter elements of list:")))
6 print("list given by user :",list1)
7
8 f = int(input("enter element you want to search:"))
9 for item in list1:
10 if item == f:
11 list1.remove(item)
12 print("list after removing element is:",list1)
13 break
14 else :
15 print("element not found")
16
1 list1=[]
2
3 a=int(input("enter length of list:"))
4 for i in range (a):
5 list1.append(int(input("enter elements of list:")))
6 print("list given by user :",list1)
7
8 f = int(input("enter element you want to search:"))
9 for item in list1:
10 if item == f:
11 list1.remove(item)
12 print("list after removing element is:",list1)
13 break
14 else :
15 print("element not found")
16
15. You are given a list of integer elements. Make a new list that will store a
square of elements of the previous list. (With and without list
comprehension)
i. Input_list = [2,5,6,12]
Ans:
In [2]:
1 Input_list = [2,5,6,12]
2 print("Input list :",Input_list)
3
4 sq_list =[i**2 for i in Input_list]
5 print("Output list :",sq_list)
16. WAP to create two lists, one containing all even numbers and the other
containing all odd numbers between 0 to 151
Ans:
In [3]:
Even numbers>> [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30,
32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 6
8, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 102, 1
04, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126, 128, 130, 132,
134, 136, 138, 140, 142, 144, 146, 148, 150]
**************************************************************************
****************
Odd numbers>> [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31,
33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 6
9, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99, 101, 103, 1
05, 107, 109, 111, 113, 115, 117, 119, 121, 123, 125, 127, 129, 131, 133,
135, 137, 139, 141, 143, 145, 147, 149, 151]
Ans:
In [35]:
1 l1=[11,20,41,52,66,96,78,200]
2 even = [i for i in l1 if i%2==0]
3 odd = [i for i in l1 if i%2!=0]
4 print("Count of even numbers >>",len(even))
5 print("Count of odd numbers >>",len(odd))
18. WAP to make new lists, containing only numbers which are divisible by 4, 6,
8, 10, 3, 5, 7, and 9 in separate lists for range(0,151)
Ans:
In [44]:
19. From a list containing ints, strings, and floats, make three lists to store them
separately.
Ans:
In [4]:
1 l1 =[ "p",12,63,3.6,"y","t",6.9]
2 s = [i for i in l1 if type(i) == str]
3 i = [i for i in l1 if type(i) == int]
4 f = [i for i in l1 if type(i) == float]
5 print(f"List of int data types :",i)
6 print(f"List of float data types :",f)
7 print(f"List of string data types:",s)
8
In [5]:
1 list1=["python",12,6.9,9.66,"data_science",255,50,"fibonacci","data",45.6,98.2]
2 s_list=[]
3 f_list=[]
4 i_list=[]
5
6 for i in list1:
7 if type(i)==int:
8 i_list.append(i)
9 elif type(i)==float:
10 f_list.append(i)
11 else:
12 s_list.append(i)
13
14 print(f"List of int data types :",i_list)
15 print(f"List of float data types :",f_list)
16 print(f"List of string data types:",s_list)
17
20. What’s The Difference Between The Python append() and extend()
Methods?
Ans:
1 append():
2
3 1. This method will add only one item at a time in a existing list.
4 2. It doesnt return new list of items , but will modify original list by adding
new item at the end of list
5 3. after executing append function size of the list will be increased by 1
6 3. syntax: list1.append(item)
7
8 extend():
9
10 1. This method will add all the elements of iterable (list,tuple,string) to the
end of the list
11 2. we can use it to simply join two lists
12 3. syntax: list1.extend(iterable)
In [6]:
1 #Example of append()
2
3 l1=[20,30,63,89,45]
4 print("Original list is:",l1)
5
6 l1.append("python")
7 print("Updated list is :",l1)
In [7]:
1 #Example of extend()
2
3 l1=[20,50,30,93]
4 print("items in list1 :",l1)
5
6 t1=(20,63,69)
7 print("items in tuple1:",t1)
8
9 l1.extend(t1)
10 print("Updated list is:",l1)
Ans:
In [9]:
In [8]:
Ans:
In [47]:
1 l1 = [1,52,63,74,12,364,313,41]
2 for i in range(len(l1)):
3 for j in range(i+1,len(l1)):
4 if l1[i] < l1[j]:
5 l1[i] , l1[j] = l1[j] , l1[i]
6 print("sorted list >> ", l1)
7 print("third largest element >> ",l1[2])
sorted list >> [364, 313, 74, 63, 52, 41, 12, 1]
third largest element >> 74
23. Write a Python program to get the frequency of the elements in a list.
Ans:
In [65]:
1 l1 = [11,2,3,1,3,1,3,15,55,7,55]
2 l2 = []
3 for i in l1:
4 if i not in l2:
5 l2.append(i)
6 for i in l2:
7 print(f"{i} occurs {l1.count(i)} times ")
11 occurs 1 times
2 occurs 1 times
3 occurs 3 times
1 occurs 2 times
15 occurs 1 times
55 occurs 2 times
7 occurs 1 times
In [49]:
1 l1 = [11,2,3,1,3,1,3,15,55,7,55]
2 for i in set(l1):
3 print(f"{i} occurs {l1.count(i)} times")
1 occurs 2 times
2 occurs 1 times
3 occurs 3 times
7 occurs 1 times
11 occurs 1 times
15 occurs 1 times
55 occurs 2 times
Ans:
In [66]:
1 list1=[100,[200,30,50],"python","data"]
2 for i in list1:
3 if type(i)==list:
4 print("List contains Sublist")
5 break
6 else:
7 print("List does not contain sublist")
In [67]:
1 list1=[100,200,30,50,"python","data"]
2 for i in list1:
3 if type(i)==list:
4 print("List contains Sublist")
5 break
6 else:
7 print("List does not contain sublist")
Ans:
In [13]:
1 list1=[100,200,30,50,[10,20,30],[30,56]]
2 for i in list1:
3 if type(i) == list :
4 print("sublist >>>>",i)
5
26. Write a Python program to find common items from two lists
Ans:
In [15]:
1 #logic1:
2
3
4 l1=[23,12,63,"python","data",[0,1],[5,1]]
5 l2=["python",22,[0,1,],20,63,"data",56,5.9]
6 c = []
7
8 for char in l1:
9 for val in l2:
10 if char == val:
11 c.append(char)
12 print("Common elements >>",c)
Ans:
In [18]:
1 # Example
2
3 list1=[[10,20,30],["python",2.3,63.9],["data","science"]]
4
5 flatten_list =[]
6
7 for char in list1:
8 if type(char)==list:
9 for v in char:
10 flatten_list.append(v)
11 print(flatten_list)
28. How to sort a list in ascending and descending order without using the sort
function?
Ans:
In [16]:
1 l1= [10,25,74,52,41,96]
2 for i in range(len(l1)):
3 for j in range(i+1,len(l1)):
4 if l1[i] < l1[j]:
5 l1[i],l1[j]=l1[j],l1[i]
6 print("Descending order >>",l1)
In [17]:
1 l1= [10,25,74,52,41,96]
2 for i in range(len(l1)):
3 for j in range(i+1,len(l1)):
4 if l1[i] > l1[j]:
5 l1[i],l1[j]=l1[j],l1[i]
6 print("Ascending order >>",l1)
Ans:
1 >> for sorting tuple , we have to first convert tuple into list.
2 >> then by using sort() function , we can sort list.
3 >> now we have to again convert our list into tuple
4 >> this way we can sort tuple
In [90]:
1 t1 = (12,41,10,63,96)
2 l1 = list(t1)
3 l1.sort()
4 t2 = tuple(l1)
5 t2
Out[90]:
In [91]:
1 t1 = (12,41,10,63,96)
2 t2 = tuple(sorted(t1))
3 t2
Out[91]:
30. Write a Python program to convert a list of multiple integers into a single
integer
a. [11, 33, 50] >>> 113350
Ans:
In [95]:
1 l1 = [11,33,50]
2 print("Input :",l1)
3 print("Output : ",end="")
4 for i in l1:
5 print(i,end="")
Ans:
1 clear()
2
3 >> it is list function
4 >> it used to delete all items from list and make a list empty
5 >> syntax: list1.clear()
6
7
8 del
9
10 >> it is a keyword
11 >> it is use to delete whole list or particular item from list by using its index
value
12
13
In [28]:
1 #Example of clear()
2
3 l1=[1,2,3,4,5,6,7]
4 l1.clear()
5 print(l1)
[]
In [31]:
1 #Example of del
2
3 l1=[1,2,3,4,5,6,7]
4 del l1
5 print(l1) # list is deleted, thats why it is showing this error
--------------------------------------------------------------------------
-
NameError Traceback (most recent call las
t)
~\AppData\Local\Temp/ipykernel_11644/3534540325.py in <module>
3 l1=[1,2,3,4,5,6,7]
4 del l1
----> 5 print(l1)
Ans:
1 remove()
2
3 >> it will remove first matching element from list
4 >> syntax: list1.remove(element)
5
6 pop()
7
8 >> it will removes item at given index from the list
9 >> syntax: list1.pop(index)
In [33]:
1 #example of remove()
2
3 l1=["mumbai","pune","hyderabad","banglore"]
4 print("Original list :", l1)
5
6 l1.remove("hyderabad")
7 print("After using remove function :",l1)
8
1 #example of pop()
2
3 l1=["mumbai","pune","hyderabad","banglore"]
4 print("Original list :", l1)
5
6 l1.pop(1)
7 print("After using pop function :",l1)
8
Ans:
1 indexing:
2
3 >> indexing is used to access 1 item of particular index value from the given
list
4 >> syntax: list1[index]
5
6 slicing:
7
8 >> it is used to obtain a sublist from the given list by giving range of start
and end index value
9 >> syntax: list1[start_index (includig): end_index (excluding): step size]
10
In [45]:
1 # Example of indexing:
2
3 l1=[10,12,63,78,99,"python"]
4 index=5
5 a=l1[5]
6
7 print(" item:'{}' is present at an index :{} ".format(a,index))
8
1 # Example of slicing:
2
3 l1=[10,12,63,78,99,"python"]
4
5 # suppose i want access char only from index value 1 to index value 4
6
7 b=l1[1:4+1]
8
9 print(" items from index value 1 to 4 includes:",b)
Ans:
1 sort()
2
3 >> sort() is a list function, syntax: list1.sort()
4 >> by default it will sort list in ascending order.
5 >> if you want to sort in descending order , you can do that by changing default
value to (reverse=True)
6 >> but you cannot assign result to the new variable by using sort () function
7
8
9 sorted()
10
11 >> sorted() is built in function , syntax: var = sorted(list1)
12 >> you can assign result to new variable by using sorted() function
13
In [58]:
Ans:
1 reverse()
2
3 >> reverse() is a list function, syntax: list1.reverse()
4 >> it will convert list in reverse order.
5 >> but you cannot assign result to the new variable by using reverse() function
6
7
8 reversed()
9
10 >> reversed() is built in function , syntax: var = reversed(list1)
11 >> you can assign result to new variable by using reversed() function
12
In [59]:
In [61]:
Ans:
In [76]:
Ans:
In [62]:
1 l1 = [1,2,3,4,5]
2 if len(l1) == 0:
3 print("list is empty")
4 else:
5 print("list is not empty")
In [11]:
1 l1=[]
2
3 if l1 == []:
4 print(" list is empty")
5 else:
6 print("list is not empty")
7
list is empty
In [15]:
1 ## Example
2
3 l1=[10,20,30,40,50]
4 l2=[60,70,80,90,100]
5
6 l1.extend(l2)
7
8 print(l1)
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
Ans:
1 >> We can find occurances of elements in python in list by using count() function
In [77]:
1 list1=[10,20,63,120,20,89,20]
2 element=20
3 c=list1.count(element)
4
5 print("Occurances of 20 inside list1 is:",c)
Ans:
1 # Example
2
3 list1=[[10,20,30],["python",2.3,63.9],["data","science"]]
4
5 flatten_list =[]
6
7 for char in list1:
8 if type(char)==list:
9 for v in char:
10 flatten_list.append(v)
11 print(flatten_list)
In [18]:
In [109]:
Enter a number :7
It is a prime number
Enter a number :8
Fibbonacci Series >>>> 0 1 1 2 3 5 8 13