Untitled Document
List , Tuples and Sets Data type
1. Program - To Create List Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program To Create List 
       
       # List - list is represented in square bracket  []
       # you can create list refering below example
       country = ["India","Israel","Russia","Brazil"]
       print(country)

       
2. Program - How to get length of list ( i.e number of elements present in a list ) Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program - How to get length of list ( i.e number of elements present in a list )
       
       country = ["India","Israel","Russia","Brazil"]
       print(len(country))
       
3. Program - Different ways of Accessing list elements Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program - Different ways of Accessing list elements 
       # Accessing list elements using Index ; Index of list always start with 0
       # Now to get/ print Israel from list ; we need to pass index number as 1 ; Here is teh example  
       
       country = ["India","Israel","Russia","Brazil"]
       print(country[1])
       
       #Printing last element from last element from list ; for this you can use -ve index where -1 will start to read element from last 
       print(country[-1])
       
       # printing list element using range ; remeber here 2 is nonclusive so we will get only first two elemnets from list
       print(country[:2])
       
       #Below example will print list from inddex1 to last element
       print(country[1:])
       
       
4. Program - Different LIST Methods Click Here
       #!/bin/ksh 
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program - Different LIST Methods
       
       country = ["India","Israel","Russia","Brazil"]
       
       #1 . append --> This method will add element at the last of list       
       country.append("South Africa")
       print(country)
       
       #2 . insert --> it will insert element in a particular location ; this method takes two arguments ( index location , "value")
       country.insert(2,"UAE")
       print(country)
       
       #3 . extend --> if you want one list into another then we need to use extend method ; this will add elements at the lastt of the list 
       country1=["Japan","Bhutan"]
       country.extend(country1)
       print(country)
       
       #4 . remove -- > this method will remove specified element from list
       country.remove("UAE")
       print(country)
       
       #5. pop --> This method will remove last element from list ; this method also returns the removed element so we can capture it in variable 
       popped=country.pop()
       print(popped)
       print(country)
       
       #6. reverse --> This method will reverse the list
       country.reverse()
       print(country)
       
       #7. sort -- > this method will sort elements in list ( By default it sorts alphabatically )
       nums =[5, 6, 3, 1, 9]
       country = ["India","Israel","Russia","Brazil"]
       country.sort()
       print(country)
       nums.sort()
       print(nums)
       
       # Sorting list in Descending order 
       nums =[5, 6, 3, 1, 9]
       country = ["India","Israel","Russia","Brazil"]
       country.sort(reverse=True)
       nums.sort(reverse=True)
       print(country)
       print(nums)
       
       #8. sorted --> The above sort function sorts element in original list ; if you don't want to make changes in original list and want to get                      sorted version of list then use sorted function 
       
       country = ["India","Israel","Russia","Brazil"]
       country_sorted=sorted(country)
       print(country_sorted)
       print(country)
       
       #9. min,max and sum --> these functions will return minimum , maximum an sum of elements from list
       
       nums =[5, 6, 3, 1, 9]
       print(min(nums))
       print(max(nums))
       print(sum(nums))
       
       #10. index --> this function will print index of the given element from the list
       
       country = ["India","Israel","Russia","Brazil","Japan"]
       print(country.index("Japan"))
       
       #11 . in operator --> this operator will help to check whether element is present in list or not ; it will return True or False 
       
       country = ["India","Israel","Russia","Brazil","Japan"]
       print ("India" in country )

       
5. Program - To Print List elements using for loop Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program - To Print List elements using for loop
       
       country = ["India","Israel","Russia","Brazil","Japan"]              
       for item in country:            
       	print(item)
       
       #enumerate --> this function is used to print list elements along with its index . Refer below exaple
       country = ["India","Israel","Russia","Brazil","Japan"]
       for index,item in enumerate(country):
       	print(index , item)
       
       # Now if you want to give your own start index then just run below  code ; here i have given start index as 2 
       for index,item in enumerate(country,start=2):
       	print(index, item)
       
6. Program - To Convert a list into a string Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program - To Convert a list into a string
       # Note - You can use any chararcter for joining in place of "-"
       
       country = ["India","Israel","Russia","Brazil","Japan"]
       country_str = "-".join(country)            
       print(country_str)
       
       # Another Example using "" ( No Seperator)
       
       country = ["India","Israel","Russia","Brazil","Japan"]
       country_str = "".join(country)            
       print(country_str)
 
       
7. Program - To Convert a string into list Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program - To Convert a string into list
       # We will use split method for it 
       
       country = ["India","Israel","Russia","Brazil","Japan"]
       country_str = "-".join(country)            
       print(country_str)
       coutry_list=country_str.split("-")
       print(coutry_list)
 
       
8. Program - To create simple Tuple Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program - To create simple Tuple
       #TUPLES are same as list the only difference is list is MUtable ; however tuples are immutable 
       # Here is simple example of tuple 
       
       my_tuple=(10, 20, 30, 40)
       print(my_tuple)
 
       
9. Program - To Create a SET Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program - To Create a SET
       # SETS and unordered data set , which doesn't allow duplicte values 
       # SETS are represent in curly brackets  
       
       my_set ={'South Africa', 'Russia', 'Japan', 'Israel', 'India', 'Brazil'}
       print(my_set)            
       
10. Different Method in Sets Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Different Method in Sets 
       
       #Example - 1. intersection --> this method will tell what are the common elemnets from two sets  
       my_set2 ={"Japan","USA","CANANDA"}
       my_set ={'South Africa', 'Russia', 'Japan', 'Israel', 'India', 'Brazil'}
       print(my_set.intersection(my_set2))   
       
       #Example - 2. differnce --> This method will tell the elements which are missing in second set  
       my_set2 ={"Japan","USA","CANANDA"}
       my_set ={'South Africa', 'Russia', 'Japan', 'Israel', 'India', 'Brazil'}
       print(my_set.difference(my_set2)) 
       
       #Example - 3. union --> Combining two sets 
       my_set2 ={"Japan","USA","CANANDA"}
       my_set ={'South Africa', 'Russia', 'Japan', 'Israel', 'India', 'Brazil'}
       print(my_set.union(my_set2))      
       
       
11. How to create Empty List, Tuple and Set Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Additional Info
       # How to create Empty List, Tuple and Set 
       
       # Empty List 
       empy_list = []   
       empty_list =list()  
       
       # Empty Tuple   
       empty_tuple = ()   
       empty_tupple = tuple ()  
       
       #Empty Set 
       empty_set = set()  
       
       
Untitled Document