Day 4

PS: These are notes, there could be grammar error and spelling errors.

for loops

For loop over a list goes as follows:

 ``````1 2 3 4 5 6 7 8 `````` ``````>>> a = ['Fedora', 'is', 'powerful'] >>> for x in a: ... print(x) ... # these ... are automatically created by python interpreter Fedora is powerful ``````

range() function

range function creates lists based on parameter passed in function``py

 ``````1 2 3 4 5 6 `````` ``````>>> list(range(1, 5)) # when two numbers are passed n,j List starts at n ends at j-1 [1, 2, 3, 4] >>> list(range(1, 15, 3)) # when three numbers are passed n,j,k. List starts at n ends at j after stepping at every k steps [1, 4, 7, 10, 13] >>> list(range(10)) # when only single number n is passed list starts from 0 and ends after n elements [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] ``````

Continue statement

Just like break is use to break loop, continue is used to continue loop towards iteration without further execution A combined example.

 ``````1 2 3 4 5 6 7 8 9 `````` ``````#!/usr/bin/env python3 while True: n = int(input("Please enter an Integer: ")) if n < 0: continue # this will take the execution back to the starting of the loop elif n == 0: break print("Square is ", n ** 2) print("Goodbye") ``````

Data Structures

Lists

Few other things with lists:

 `````` 1 2 3 4 5 6 7 8 9 10 11 `````` ``````# to insert a new element in list >>> a = [23, 45, 1, -3434, 43624356, 234] >>> a.append(45) >>> a [23, 45, 1, -3434, 43624356, 234, 45] # to inset at particular position we have to pass position along. First would be position then would be element >>> a.insert(0, 1) >>> a [1, 23, 45, 1, -3434, 43624356, 234, 45] # to count number of occurrences of a element >>> a.count(45) ``````

Same way we have `.reverse`, `.remove` using append we can append another list to existing list as a element of the list

 ``````1 2 3 4 `````` ``````>>> b = [45, 56, 90] >>> a.append(b) >>> a [45, 43624356, -3434, 1, 45, 23, 1, 111, [45, 56, 90]] ``````

There is another method call `.extend` which would add element of second list as element of new list

 `````` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 `````` ``````>>> a.extend(b) #To add the values of b not the b itself >>> a [45, 43624356, -3434, 1, 45, 23, 1, 111, [45, 56, 90], 45, 56, 90] >>> a[-1] # here in a[n] value of n is -1 so it would give last element 90 # to remove element which was we would pass same list or sublist to remove it. >>> a.remove(b) >>> a [45, 43624356, -3434, 1, 45, 23, 1, 111, 45, 56, 90] # .sort is use to sort list if list is sortable means elements are comparable. # del can also be used to delete a element from list >>> del a[-1] >>> a [-3434, 1, 1, 23, 45, 45, 45, 56, 90, 111] ``````

Using lists as stack and queue

Stacks work as LIFO(Last In First Out) and queue work on FIFO(First In First Out)

 `````` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 `````` ``````>>> a = [1, 2, 3, 4, 5, 6] >>> a [1, 2, 3, 4, 5, 6] # for Lifo and fifo everything would be appended at the end of list we would use append. >>> a.append(34) >>> a [1, 2, 3, 4, 5, 6, 34] # while removing element we would remove using pop # for removing element in lifo we would use .pop() without any parameter so that it would remove last element >>> a.pop() 34 # for fifo or queue where oldest element is removed first we use .pop(0) >>> a.pop(0) 1 ``````

Note: .pop removes element and return removed element while append just add element doesn’t returns anything