Se ha denunciado esta presentación.
Se está descargando tu SlideShare. ×

# Lecture-5.pdf

Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Próximo SlideShare
Lecture-2.pdf
Cargando en…3
×

1 de 16 Anuncio

# Lecture-5.pdf

Anuncio
Anuncio

## Más Contenido Relacionado

Anuncio

### Lecture-5.pdf

1. 1. Lists List can store a sequence of values belonging to any datatype. 'Hi' 3 2.6 ‘S’ ‘Two’ L = [‘Hi’, 3, 2.6, ‘S’, ‘Two’] or L = list(<sequence>) L :
2. 2. Playing with Lists ■ Creating Lists L = list(‘LockDown’) print(L) [‘L’, ‘o’, ‘c’, ‘k’, ‘D’, ‘o’, ‘w’, ‘n’] L = list( input(‘Enter elements : ')) print(L) Takes one string, make each character as one element in List.
3. 3. Playing with Lists ■ Lists vs Strings Similarities : Len( ), Indexing & Slicing, Membership, Concatenation & Replication Differences : Storage : Strings store single character at each block, Lists store references. Mutability : Strings-Immutable Lists-Mutable
4. 4. Playing with Lists ■ Appending Elements L=[2,5,17] L[3]=12 or L.append(12) ■ Updating Elements L[1]=7 ■ Deleting Elements del L[2] deletes element at index 2. del L[1:4] deletes slice of 1:4 i.e. 1,2 & 3 indices. del L deletes the whole list. ■ Extending Elements L = [2,5,17] L.extend( (21, ‘hi’, [3,6,7]) )
5. 5. Playing with Lists (other operations are same as strings) ■ Making a True Copy Colors = [‘red’, ‘blue’, ‘green’] L2 = Colors ❌ L2 = list(Colors) ✔ In first case, any changes in Colors will reflect in L2 as they both are referencing the same list.
6. 6. Lists Functions Python also offers many built in functions for Lists. ● The index method gives the index of the <element> L.index(<element>) exception error if not found. ● The extend method L.extend(<list>) appends <sequence> into L. append( ) & extend( ) discussed before
7. 7. Lists Functions ● The insert method L.insert(<pos>,<item>) inserts <item> at <pos>th index. ● The pop method L.pop(<index>) deletes & returns element at <index>. L.pop( ) deletes & returns last element.
8. 8. Lists Functions ● The remove method L.remove(<value>) removes the first occurence of <value>. ● The clear method L.clear( ) clears the whole list, [ ], this remains. ● The count method L.count( <item>) counts the <item> & returns
9. 9. Lists Functions ● The reverse method L.reverse( ) reverses the whole list. ● The sort method L.sort( ) sorts the list in ascending order. L.sort(reverse = True) sorts the list in descending order.
10. 10. Practice Time Q1. List=[1,2,3,4,5,6,7,8,9] print(List[ : : 3]) Q2. What will be the output of the following code snippet? a. A = [ ] for i in range(1,4) : A.append(i) print(values) b. b=[ 1,2,3,4,5 ] print( b[3:0:-1] )
11. 11. Tuples Tuples are basically immutable Lists. Lists are made by square brackets [ ]. Tuples are made by curved brackets ( ). t=(1, 'hello', 3.2, ‘w’) t=tuple('Harsh')
12. 12. Tuples vs Lists Following things are same as before :- ● Creating ● Creating using input ● Indexing & Slicing ● Membership ● Concatenation & Replication ● Traversing
13. 13. T = (1, 2, ‘A’, ‘B’) w, x, y, z = T variables on the left side must match the number of elements in tuple. print(w,x,y,z,sep=’*’) OUTPUT : 1*2*A*B Unpacking a Tuple
14. 14. Tuple Functions ● The len method (same for string & List too) len(<tuple>) returns length of the tuple. ● The max/min methods max(<tuple>) returns max element in the<tuple>. min(<tuple>) returns min element in the<tuple>.
15. 15. ● The index method returns index number of <item>. t.index(<item>) exception error if not found. ● The count method t.count(<item>) returns the count of <item> in the tuple. ● The tuple method used to create tuples. t=tuple('abc') (‘a’, ‘b’, ‘c’) t=tuple([1,2,3]) tuple from list t=tuple( ) empty tuple Tuple Functions
16. 16. Practice Time Q1. t = ‘a’ , ‘b’ t2= (‘a’, ‘b’) print(t==t2) Q2. What is the length of this tuple? t=( ( ( (‘a’,1 ), ‘b’, ‘c’ ), ‘d’, 2 ), ‘e’, 3 ) Q3. A= ( ‘Hello’, ‘Harsh’ , ‘How’re’, ‘you ?’ ) ( a,b,c,d ) = A print( a, b, c, d ) A = ( a, b, c, d ) print( A[0][0]+A[1][1], A[1] )