30
loading...
This website collects cookies to deliver better user experience
Day 10 We learnt about lists in Python, and various list functions like popping, appending, copying and much more. We then used these functions for creating a program for dynamic generation of lists.
Day 11 We learnt about multidimensional lists, dynamic generation of multidimensional lists and had an introduction to tuples. We learnt that tuples were non mutable lists.
Day 12 We used lists to make algorithms like bubble sorting, binary searching and sequential searching.
a=[]
for i in range(1,10):
b=i
a.append([]) #Append an empty list into a list
while(b!=1):
a[i-1].append(int(b))
#int() to prevent trailing decimal 0. eg 5.0 will be written as 5
if(b%2==0):
b=b/2
else:
b=3*b+1
a[i-1].append(1)
print(a)
[[1], [2, 1], [3, 10, 5, 16, 8, 4, 2, 1], [4, 2, 1], [5, 16, 8, 4, 2, 1], [6, 3, 10, 5, 16, 8, 4, 2, 1], [7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1], [8, 4, 2, 1], [9, 28, 14, 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1]]
This is the largest program we have ever written so far. See those nesting levels? But do not worry, everything is properly explained in the comments beside the code. For those new to LIFO and FIFO, please visit the Stack data structure
print("Please enter 'pop' for popping, 'push' for pushing, print to print and end to terminate the program.....")
stack=[] #make a list named 'stack'
while (True):
a=input("...") #get input from the user
if(a=='push'):
while True: #To check if the input is a number or not
a=input("Which number to push?... ")
if(48<=ord(a)<=57): #check
stack.append(a) #push the number
break
#if the input is number, then terminate the nested while loop,
# else continue the loop until numeric value is obtained
else:
print("Please enter only numbers ") #error return
elif(a=='pop'):
print(stack.pop()) #pop, ie. remove and return
elif(a=='print'):
print(stack) #print the list
elif(a=='end'):
break #terminate the while loop
else:
print("Unknown command ")
print("Thank you")
Please enter 'pop' for popping, 'push' for pushing, print to print and end to terminate the program.....
...push
Which number to push?... 3
...poppp
Unknown command
...pop
3
...push
Which number to push?... 4
...push
Which number to push?... k
Please enter only numbers
Which number to push?... l
Please enter only numbers
Which number to push?... 5
...print
['4', '5']
...pop
5
...print
['4']
...push
Which number to push?... 3
...end
Thank you
Please enter 'pop' for popping, 'push' for pushing, print to print and end to terminate the program.....
...push
Which number to push?... 3
...push
Which number to push?... 2
...pop
2
...pop
3
...pop
Traceback (most recent call last):
File "main.py", line 16, in <module>
print(stack.pop()) #pop, ie. remove and return
IndexError: pop from empty list
Please enter 'pop' for popping, 'push' for pushing, print to print and end to terminate the program.....
...push
Which number to push?... 2
...pop
2
...pop
Cannot pop from an empty list
...
Please enter 'Add' for Adding, 'Remove' for removing, print to print and end to terminate the program.....
...Add
Which number to Add?... 4
...Add
Which number to Add?... 5
...Add
Which number to Add?... 6
...remove
Unknown command
...Remove
4
...print
['5', '6']
...Remove
5
...Remove
6
...print
[]
...Remove
Cannot Remove from an empty list
...end
Thank you
For those who have not yet made account in Dev.to, you can have a free easy sign-up using your mail or GitHub accounts. I would suggest the budding developers to create your GitHub free account right away. You would require to register sooner or later anyways