In Python, an iterable is any object capable of returning its elements one at a time. They allow you to loop over a sequence of items, accessing each element in turn. Objects like lists, tuples, sets, dictionaries, strings, etc. are called iterables.
Lets look at the types in detail.
1. List
- Lists are mutable
- They consist of multiple data-types
- They are enclosed within " [] "
lis = [ 10 , " xyz " , 10.5 , " a " ]
This is a list with four elements and indexing begins with 0.
List Functions:
lis = [ 10 , " xyz " , 10.5 , " a " ]
1. Slicing a list
print ( lis [ 1 : 3 ] )
Output : [ " xyz " , 10.5 ]
2. Accessing Indexes
print ( lis [ 10.5 ] )
Output : 2
Because the index number of 10.5 is 2 in the above list
3. Append
It always appends the value at the end of the list
print ( lis.append ( 35 ) )
Output : [ 10 , " xyz " , 10.5 , " a " , 35 ]
4. Insert
It inserts the value at the given index
print ( lis.insert ( 2, 7.5 ) )
Output : [ 10 , " xyz " , 7.5 , 10.5 , " a " ]
5. Remove
The value entered is removed from the list
print ( lis.remove ( " a " ) )
Output : [ 10 , " xyz " , 10.5 ]
6. Pop
The value at the index entered is removed from the list
print ( lis,pop ( 2 ) )
Output : [ 10 , " xyz " , " a " ]
7. Count
Counts the number of occurrences of the value entered
print ( lis.count ( " a " ) )
Output : 1
8. Length
It can be found using len()
print ( len ( lis ) )
Output : 4
9. Sort
sorted() function can be used to sort the elements of the list in ascending order without modifying the original list.
my_list =
[ 3 , 1 , 4 , 1 , 5 , 9 , 2 , 6 , 5 ]
sorted_list
= sorted(my_list)
print(sorted_list) # Output: [1, 1, 2, 3, 4, 5, 5, 6, 9]
print(my_list) # Original list remains unchanged: [3, 1,
4, 1, 5, 9, 2, 6, 5]
2. Tuples
- Tuples are immutable
- They consist of multiple data-types
- They are enclosed within " () "
- All list functions can be performed on tuples by converting them to lists
Example:
tup=('abc',1,10.5)
print(tup) #print the tuple
tup1=list(tup) #convert tuple to list
print(tup1)
tup1.append("Python") #append to list
print(tup1)
tup=tuple(tup1) #convert list back to tuple
print(tup)
3. Sets
- Sets are unordered
- They can not be accessed by indexes
- They consist of multiple data-types
- They are enclosed within " {} "
4. Dictionaries
- key : value pairs exist
- They are ordered
- They are enclosed within " {} "
- Keys are immutable
- Values are mutable
- Can't give lists or any other iterables as key
- All iterables can be given as values
dic = { 1 : " one " , 2 : " two " , " name " : " abc " , 10 : None }
print ( dic ) # prints the entire dictionary
dic [ 3 ] = " Three " # Adds 3 : " Three " to the end of the dictionary
dic.update({" colour " : " red "}) # Is another way to add to a dictionary
print ( dic [ 2 ] ) # Prints "two"
print ( dic.get( 1 ) ) # Prints " one "
print ( dic.pop ( "name" ) ) # Removes that key:value pair from the dictionary
print ( " Keys are " , dic.keys() ) # Gives tuple of all keys
print ( " Values are " , dic.values() ) # Gives tuple of all values
If you give incorrect key in get function it returns " None " instead of an error like in normal fashion
0 Comments