CBSE COMPUTER SCIENCE
الذهاب إلى القناة على Telegram
QUESTION BANK, REFERENCE BOOK, LAB MANUAL , WORKSHEET ETC., PYTHON TEXT BOOK AND REFERENCE BOOKS, 01. ASK QUESTIONS AT https://t.me/joinchat/FJ1noGs-64I0OWI1
إظهار المزيد549
المشتركون
لا توجد بيانات24 ساعات
لا توجد بيانات7 أيام
لا توجد بيانات30 أيام
أرشيف المشاركات
Working with Stack using List:
The implementation of stack using list is a simple process as there are inbuilt function which we used during working with stack. Basic operations that we should know are :
1. How to create an empty stack?
2. How to add elements to a stack?
3. How to delete / remove elements from the stack
4. How to traverse or displaying elements of stack?
5. How to check for empty stack?
1. Creating an Empty Stack : An empty stack can be created by using the following code
st = [ ] or st = list( ) #Here st is an empty stack
NOTE : Working with stack is similar to working with list (we can add element by append( ), we can remove element by pop( ) and we can display element by using index value)
2. Adding an element to a Stack : We can add element in a stack by using append( ) function as shown below
st.append(5)
Here element ‘5’ is added into a stack named ‘st’
NOTE : We can add element only at the end of the list as we are implementing list as stack.
3. Deleting elements from the stack : We can delete elements from the stack as shown below :
st.pop( )
NOTE : We can remove or delete element only from the end of the list as we are implementing list as stack.
4. Displaying all elements of the stack : We can display all elements in stack as shown below :
L = len(st)
for i in range(L-1, -1, -1) : #As we have to display elements in reverse order
print(st[i])
if (st == [ ]) :
print("stack is empty")
Data Structure in Python – Question Answer
Q1. Expand the term LIFO.
Q2. What do you mean by Stack?
Q3. What is the difference between pop( ) and append( ) function of list?
Q4. What do you mean by data structure?
Q5. Write a code to create an empty stack named "st".
Q6. Write the technical term used for adding element in stack.
Q7. What happen when you try to delete an element from an empty stack?
Practical Implementation of Stack using List
Q1. Write a function push(student) and pop(student) to add a new student name and remove a student name from a list student, considering them to act as PUSH and POP operations of stack Data Structure in Python.
st=[ ]
def push(st):
sn=input("Enter name of student")
st.append(sn)
def pop(st):
if(st==[]):
print("Stack is empty")
else:
print("Deleted student name :",st.pop())
Q2. Write a function push(number) and pop(number) to add a number (Accepted from the user) and remove a number from a list of numbers, considering them act as PUSH and POP operations of Data Structure in Python.
st=[ ]
def push(st):
sn=input("Enter any Number")
st.append(sn)
def pop(st):
if(st==[]):
print("Stack is empty")
else:
print("Deleted Number is :",st.pop())
Q3. Write a menu based program to add, delete and display the record of hostel using list as stack data structure in python. Record of hostel contains the fields : Hostel number, Total Students and Total Rooms
host=[ ]
ch='y'
def push(host):
hn=int(input("Enter hostel number"))
ts=int(input("Enter Total students"))
tr=int(input("Enter total rooms"))
temp=[hn,ts,tr]
host.append(temp)
def pop(host):
if(host==[]):
print("No Record")
else:
print("Deleted Record is :",host.pop())
def display(host):
l=len(host)
print("Hostel Number\tTotal Students\tTotal Rooms")
for i in range(l-1,-1,-1):
print(host[i][0],"\t\t",host[i][1],"\t\t",host[i][2])
ASSIGNMENT SET – 5
Time: 30 min M.M. – 20
Q1. Write a program to input a number and count the occurrence of that number in the given list.
B = [34,21,3,12,34,56,76,5,4,21,12,34]
Q2. Write a program to separate the character and numeric value from a given list and store them in a separate list.
A = [1,’f’,2,’b’,3,4,’h’,j’,6,9,0,’k’]
Q3. What do you mean by sorting?
Q4. Name any two sorting techniques.
Q5. Write a program to create a list of 10 integers and sort the list in increasing order using bubble sort.
Q6. Suppose total element in a list are 7, so how many times the outer loop will be executed in bubble sort.
Q7. A = [23,45,21,78,43]
Write the order of the elements in the above list after first pass of bubble sort (in ascending order).
Q8. Write any one application of bubble sort.
Data Structure in Python
Table of Contents
1. Data structure in Python
2. What is Stack
1. Operations on Stack
2. Working with stack using list.
3. Practice Questions – Part 1
4. Practical Implementation of stack using list
3. What is Queue?
1. Operations on Queue
2. Queues in daily life
3. Working with queue using list
4. Practice Questions – Part 2
5. Practical Implementation of queue using list
Data Structure
A data structure in python can be defined as a structure which can holds related data. In other words we can say that data structure is a way of storing, organizing and fetching data in computer. There are four data structure in python :
1. List
2. Tuple
3. Dictionary
4. Set
In this handout we will learn that how List can be implemented as STACK & QUEUES
STACK :
A stack is a linear data structure in python in which addition and deletion of elements can be done at one end only. A stack is known as LIFO (Last – In, First – Out) data structure in python. LIFO means the elements which are added in the last would be the first one to remove. Examples of stack are pile of books, pile of plates or stack of carom coins.
In above pile of rings the ring which we placed first is at the bottom and the ring which we placed in last is at the Top, So we can say that Stack is linear list implemented as LIFO.
Operations on Stack:
There are two main operations on Stack:
Addition of element on the Top of the Stack is called PUSH.
1. push (4)
2. push (7)
3. push ("a")
4. push ("Suman")
Above operations will form the stack as shown below :
So you can observe that the element which we inserted first is coming at the bottom of the stack.
In the form of List the above stack can be shown as.
Class 12 Computer Science Data Structure in Python Handout
Removal of elements from the top of the Stack is called POP.
In the form of List the above stack will be represented as shown below
Q1. Write the role of reverse() function in list.
Ans reverse() function arrange the elements of list in the reverse order.
Q2. reverse() function create new list. (T/F)
Ans. False
Q3. >>> a = [1,2,3,4]
>>> a.reverse()
>>> print(a)
Write the output of above code.
Ans. [4,3,2,1]
Q4. What type of error return by index() function, if element is not present in list?
Ans. Index error
Q5. a = [1,5,7,5]
b = a.index(5)
print(b)
Write the output of above code.
Ans. 1
Q6. Write the output of the following code :
a = [1,2,3,4]
a[1] = 'a'
print(a)
Ans. [1,a,3,4]
Q7. len() function returns the ______ of the list.
Ans length
Q8. By default sort() function arrange the elements in ________ order (increasing/decreasing)
Ans. increasing
Q9. Write code to arrange elements of following list in increasing order.
A = [23,12,45,32,67,33]
Ans. A.sort()
Q10. What do you mean by nested list. Give one example of nested list.
Ans. A list inside another list is called nested list. for example
A = [1,2,3,[‘a’,’b’]]
ASSIGNMENT SET – 3
Time: 30 min M.M. – 20
Q1. Explain the following functions in reference to list with example
a. count()
b. clear()
c. pop()
d. remove()
Q2. What is the difference between del statement and pop() function?
Q3. Write the output of the following:
a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
b = a.pop(5)
c = a.pop( )
d = a.pop(-1)
print(b)
print(c)
print(d)
print(a)
Q4. Out of del and pop() which one return the deleted element.
Q5. remove() function take _______ (element/index) as argument.
Q6. Name a function/statement which can delete more than one element from the list.
Q7. Name a function which can delete only one element from the list.
Q8. Write a program to delete/remove all the negative elements from the list.
Q9. Write a program to delete/remove all the odd numbers from the list.
Q10. Write a program to delete/remove all the numbers less than 10 from the list.
ASSIGNMENT SET – 4
Time: 30 min M.M. – 20
Q1. Write a program to check whether a number (accepted from user) is present in a list.
Q2. Name a function which is used to find the largest number from a list.
Q3. ______ function returns the smallest value from the list.
Q4. max() function works in a list which have all values of same data type. (T/F)
Q5. Write the full form of ASCII value.
Q6. ASCII value of ‘A’ is ______.
Q7. ASCII value of ‘b’ is ______.
Q8. Write the output of the following:
a = [11,42,31,14]
print(max(a))
print(min(a))
print(a.index(42))
Q9. Write the output of the following:
a = [11,42,31,’a’,14]
print(max(a))
Q10. Write the output of the following:
a = [‘amit’,’Amit’,’Amita’]
print(max(a))
print(min(a))
(input numbers are : 1,2,3,4,5,6,7,8,9,10
L = [1, 3, 5, 7, 9])
a = []
for i in range(10):
num=int(input("Enter any number"))
if(num%2!=0):
a.append(num)
print(a)
Q41. Write a program to find the largest number from the following list.(without using inbuilt function)
A = [23, 12, 45, 67, 55]
Ans.
A = [23, 12, 45, 67, 55]
max=A[0]
for i in range(len(A)):
if A[i]>max:
max=A[i]
print(max)
Q42. Write a program to find the second largest number from the following list.
A = [23, 12, 45, 67, 55]
Ans
A = [23, 12, 45, 67, 55]
A.sort( )
print(A[-2])
Q43. Explain the extend() function with example.
Ans. extend() function add complete list or specified elements of list at the end of another list. for example
A = [23, 12, 45, 67, 55]
B = [“Amit”,2,3]
A.extend(B)
print(A)
OUTPUT : [23, 12, 45, 67, 55, ‘Amit’, 2, 3]
Q6. Write any one difference between insert() and append() function.
Ans. insert() function add elements at the specified index of list while append() function add the elements at the end of the list.
Q25. What do you mean by concatenation in list? Explain with example.
Ans Concatenation of list means combining the two list. For example :
>>>a = [1,2,3,4]
>>>b = [5,6,7,8]
>>> a+b
The output will come : [1,2,3,4,5,6,7,8]
Q26. Which mathematical operator is used to concatenate the list?
Ans. ‘+’
Q27. Which mathematical operator is used to replicate the list?
Ans. ‘*’
Q28. Write the output of the following:
>>> a= “python”
>>> b=list(a)
>>> b*2
>>> b+b
>>> a+a
Ans.
['p', 'y', 't', 'h', 'o', 'n', 'p', 'y', 't', 'h', 'o', 'n']
['p', 'y', 't', 'h', 'o', 'n', 'p', 'y', 't', 'h', 'o', 'n']
'pythonpython'
Q29. Can we multiply two list?
Ans. No
Q30. Index value of first element in list is _______________________
Ans. 0
Q31. Index value of last element in list is ____________________
Ans. -1
Q32. Write one difference between indexing and slicing.
Ans. By indexing we can extract only one element of list while by slicing we can fetch a sub list from main list.
Q33 The syntax of slicing is:
List[start : stop : step]
Which argument is optional out of start, stop and step?
Ans. Step
Q34. Write the output of the following:
b = “Practice in Python“
a=list(b)
1. print(a)
2. print(len(a))
3. print(a[1:4])
4. print(a[1:7])
5. print(a[3:])
6. print(a[:5])
7. print(a[4:17])
8. print(a[-2:-5:-1])
9. print(a[1:7:1])
10. print(a[1:7:2])
11. print(a[-5:])
12. print(a[:4])
13. print(a[-2:-5:-2])
14. print(a[11:15])
15. print(a[:])
16. print(a[::2])
17. print(a[-5:-1])
18. print(a[7:1:-1])
19. print(a[3:-3])
20. print(a[30:40])
21. print(a[17:-1])
22. print(a[10::-1])
Ans.
1. ['P', 'r', 'a', 'c', 't', 'i', 'c', 'e', ' ', 'i', 'n', ' ', 'P', 'y', 't', 'h', 'o', 'n']
2. 18
3. ['r', 'a', 'c']
4. ['r', 'a', 'c', 't', 'i', 'c']
5. ['c', 't', 'i', 'c', 'e', ' ', 'i', 'n', ' ', 'P', 'y', 't', 'h', 'o', 'n']
6. ['P', 'r', 'a', 'c', 't']
7. ['t', 'i', 'c', 'e', ' ', 'i', 'n', ' ', 'P', 'y', 't', 'h', 'o']
8. ['o', 'h', 't']
9. ['r', 'a', 'c', 't', 'i', 'c']
10. ['r', 'c', 'i']
11. ['y', 't', 'h', 'o', 'n']
12. ['P', 'r', 'a', 'c']
13. ['o', 't']
14. [' ', 'P', 'y', 't']
15. ['P', 'r', 'a', 'c', 't', 'i', 'c', 'e', ' ', 'i', 'n', ' ', 'P', 'y', 't', 'h', 'o', 'n']
16. ['P', 'a', 't', 'c', ' ', 'n', 'P', 't', 'o']
17. ['y', 't', 'h', 'o']
18. ['e', 'c', 'i', 't', 'c', 'a']
19. ['c', 't', 'i', 'c', 'e', ' ', 'i', 'n', ' ', 'P', 'y', 't']
20. []
21. []
22. ['n', 'i', ' ', 'e', 'c', 'i', 't', 'c', 'a', 'r', 'P']
Q35. Write a code to make copy of following list using copy function.
a = [1,2,3,4]
Ans
>>> import copy
>>> a = [1,2,3,4]
>>> b=copy.copy(a)
Q36. What do you mean by append() function?
Ans. Append function add one element or a list at the end of the list.
Q37. Write the output of the following:
>>> a = [1,2,3,4]
>>> a.append(7)
>>> a
Ans. [1,2,3,4,7]
Q38. Write the output of the following
>>> a = [1,2,3,4]
>>> a.append([7,8])
>>> a
Ans. [1,2,3,4,[7,8]]
Q39. Write a program to create list of the following (take input from the user)
a. Any five students name
b. Any five numbers
c. Any five alphabets
d. Any five name of colors.
Ans
a)
a = [ ]
for i in range(5):
nm=input("Enter student name")
a.append(nm)
print(a)
b)
a = [ ]
for i in range(5):
num=int(input("Enter any number"))
a.append(num)
print(a)
c)
a = [ ]
for i in range(5):
al=input("Enter any alphabet")
a.append(al)
print(a)
d)
a = [ ]
for i in range(5):
cl=input("Enter name of any color")
a.append(cl)
print(a)
Q40. Write a program to accept 10 numbers from the user, if the number is odd, and then add that number to the list.
Practice Questions of List in Python
Q1. What do you mean by List in Python?
Ans. A list is a data structure which is mutable and ordered sequence of elements.
Q2. The elements in the list can be of ______ type (any/fixed)
Ans. any
Q3. Elements in the list are enclosed in _____ brackets
Ans. Square([ ])
Q4. Values in the list are called _______.
Ans. Item
Q5. Lists are ________ in nature (heterogeneous/homogeneous)
Ans. Heterogeneous
Q6. List is a _________ data type. (Linear/non-linear)
Ans. Linear
Q7. Elements in the list are separated by ______.
Ans. Comma (,)
Q8. Write the code to create an empty list named ‘L’.
Ans. L = [ ]
Q9. Write a code to create list of any three colors named ‘color’.
Ans. color = [“blue”, “black”, “red”]
Note : You can write any other color of your choice
Q10. What do you mean by nested list
Ans. A list inside another list is called nested list.
Q11. Write the code to create the list of::
1. Five vegetables
2. Vowels
3. First 10 natural numbers
4. Square of first 5 natural numbers.
5. Any five names of your friends
6. All the alphabets of word “Taj Mahal”
7. First five multiples of 6.
a=[“Lady Finger”, “Carrot”, “Radish”, “Cauli Flower”, “Beetroot”] (# You can write any other vegetable also)
b = [‘a’,’e’,’o’,’i’,u’]
c = [1,2,3,4,5,6,7,8,9,10]
d = [1,4,9,16,25]
e = [‘amit’,’anuj’,’ashu’,’suman’,’mridul’] (# You can write any other names also)
f = [‘T’,’a’,’j’,’M’,’a’,’h’,’a’,’l’]
Q12. Write a code to convert the string “Practice” into list.
Ans. a = list(“Practice”)
Q13. Write the output of the following:
>>> d = “a*hj?”
>>> list(d)
Ans. [‘a’,’*’,’h’,’j’,’?’]
Q14. Write the output of the following:
>>>a = “String”
>>>list (a)
Ans. [‘S’,’t’,’r’,’i’,’n’,g’]
Q15. Write the output of the following:
>>> a = list ()
>>> a
Ans.[ ]
Q16. What is list() function?
Ans list() function is used to create empty list and it can also convert certain type of object into list.
Q17. Write the output of the following:
>>>a = input("Enter any String")
Enter any String : Practice
>>>list(a)
[‘p’,’r’,’a’,’c’,’t’,’i’,’c’,’e’]
Q18. Write the output of the following
a = [1,2,3,4,5]
print([0]
print([-1]
print([2]
print([-2]
print([1]
Ans.
1
5
3
4
2
Q19. Fill the index value in place of ‘?’ as output should come as below
P
Q
L
N
T
list1 = "Practice QuesTions of List in pythoN"
list2 = list(list1)
print(list2[ ? ])
print(list2[ ? ])
print(list2[ -? ])
print(list2[- ? ])
print(list2[ ? ])
Ans.
0
9
14
1
13
Q20. Write the output of the following code :
b = ['p','r','a','c','t','i','c','e']
for i in b:
print(i,end="?")
Ans.
p?r?a?c?t?i?c?e?
Q21. What do you mean by traversing a list?
Ans Traversing means to access each element of list
Q22. Write a program to print all the elements of given list using for loop.
A = [‘a’,’b’,’c’,’d’,’e’]
Ans.
A = [‘a’,’b’,’c’,’d’,’e’]
for i in A:
print(i)
Q23. List in python are mutable.(T/F)
Ans. True
Q24. Write the output of the following :
S. No Code Output (Ans.)
1 [1,2,3,4] == [4,3,2,1] False
2 [1,2,3,4] > [4] False
3 [1,2,3,4] != [1,2,3,4] False
4 [1,2,3,4] == [1,2,[3,4]] False
5 [1,2,3] == [1.0,2.0,3.0] False
6 >>>a = [1,2,3,4]
>>>b = [5,6,7,8,]
>>>a+b [1,2,3,4,5,6,7,8]
import mysql.connector as mcr
try:
cn=mcr.connect(host="localhost",user="root",password="admin")
if cn.is_connected:
print("Connected Successfully...")
else:
print("Not Connected")
except:
print("Exception Raised")
