**Python training : Lists and Tuples **

# Lists

>>> a = [10,20,30,40,50,60,70,80,90] >>> len(a) 9 >>> min(a) 10 >>> max(a) 90 >>> print (a + [100,110,120]) [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120] >>> print (a[0]) 10 >>> a[0]=15 >>>print ( a) [15, 20, 30, 40, 50, 60, 70, 80, 90] >>> del a[2] >>> print (a) [15, 20, 40, 50, 60, 70, 80, 90] >>> print (a[1:5]) [20, 30, 40, 50] >>> a[1:5]=[200,300,400,500] >>> print (a) [10, 200, 300, 400, 500, 60, 70, 80, 90] >>> a[1:5] = [] >>>print( a) [10, 60, 70, 80, 90] >>> a[:] = [] >>>print(a) [] >>> a = [1,2,3] >>> print (a*3) [1, 2, 3, 1, 2, 3, 1, 2, 3]

# Python Slicing examples

a=[0,11,22,33,44,55,66,77,88,99] syntax of slicing: a[x:y:z] >>> a[0:7:1] #elements from 0 (included) to 7 (excluded) [0, 11, 22, 33, 44, 55, 66] >>> a[0:7:2] [0, 22, 44, 66] If z is not given, z is taken as 1 >>> a[0:7] [0, 11, 22, 33, 44, 55, 66] If z is positive , slicing will be from left to right , so x should be less than y. >>> a[2:7:1] [22, 33, 44, 55, 66] >>> a[7:2:1] (will not slice) [] >>> If z is negative ,slicing will be from right to left, so x should be greater than y >>> a[7:2:-1] [77, 66, 55, 44, 33] if z is positive and if x is not given , x is taken as 0 if y is not given , y is taken as the length of the list >>> a[:7:1] [0, 11, 22, 33, 44, 55, 66] >>> a[5::1] [55, 66, 77, 88, 99] if z is negative and if x is not given , x is taken as -1 (index of the last element) if y is not given , y is taken as negative of length of the list >>> a[:-5:-1] [99, 88, 77, 66] >>> a[-5::-1] [55, 44, 33, 22, 11, 0] >>> a[5:-1:1] [55, 66, 77, 88] >>> a[-1:5:-1] [99, 88, 77, 66]

# List inside list

>>> a = [10,20,[30,40,50],60] >>> print (a) [10, 20, [30, 40, 50], 60] >>> print (a[2]) [30, 40, 50] >>> print (a[2][0]) 30 >>> print (a[2][1]) 40

# List methods

>>> a=[1,2,3,4,5,2,6,7,2,8] >>> a.count(2) 3 >>> a=[1,2,3] >>> a.append(4) >>> print(a) [1, 2, 3, 4] >>> a.append([5,6]) >>> print(a) [1, 2, 3, 4, [5, 6]] >>> a.extend([7,8,9]) >>> print (a) [1, 2, 3, 4, [5, 6], 7, 8, 9] >>> a=[10,20,30,40,50,60,70] >>> a.pop() 70 >>>print( a) [10, 20, 30, 40, 50, 60] >>> a.pop(2) 30 >>> print(a) [10, 20, 40, 50, 60] An element can be removed using “remove” method also. pop() takes index as the argument and remove() takes the element as argument. >>> a=[10,20,30,40,50] >>> a.remove(30) >>> a [10, 20, 40, 50] >>> a=[10,20,30,40,50] >>>a.insert(2,25) >>> print(a) [10, 20, 25, 30, 40, 50] >>> a = [10,20,30,40,50] >>> a.reverse() >>> print(a) [50, 40, 30, 20, 10] >>> a=[8,2,15,4,6,20] >>> a.sort() >>> print(a) [2, 4, 6, 8, 15, 20] >>> a.sort(reverse=True) >>> print(a) [20, 15, 8, 6, 4, 2]

**Tuple**

Tuple is like a list , but it is immutable. (Tuple does not allow to add,del,insert,modify elements) Tuple use parentheses, whereas list use square brackets.

# Tuple methods

>>> a=(10,20,30,40,50) >>> len(a) 5 >>> min(a) 10 >>> max(a) 50 >>> a.count(50) 1 >>> a.index(50) 4 >>> a=(10,20,30,40,50) >>> b = (60,70) >>> c = a + b >>> print (c) (10, 20, 30, 40, 50, 60, 70) >>> a * 2 (10, 20, 30, 40, 50, 10, 20, 30, 40, 50)

# Python Exercises

1. Write a python program to find the sum of all numbers in a list 2. Write a python program to find largest number in a given list with out using max() 3. Write a python program to find the common numbers from two lists 4. Write a python program to print all even numbers from a given list 5. Write a python program to create a list of even numbers and another list of odd numbers from a given list 6. Write a python program to remove repeated elements from a given list without using built-in methods 7. Write a python program to find the longest word in a given sentence 8. Write a python program to find number of occurrences of given number with out using built-in methods 9. ["www.zframez.com", "www.wikipedia.org", "www.asp.net", "www.abcd.in"] Write a python program to print website suffixes (com , org , net ,in) from this list 10. Write a python program to sort a given list of numbers with out using sort() functionNext chapter: Python training : Dictionary, Dictionary methods and Two dimensional dictionary

Previous chapter: Python training : Strings and String methods