Using lists , tuples, sets, dictionaries

 

DAY 2 :

 

lists , tuples, sets, dictionaries


lists:


Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are tuple,set and dictionary, all with different qualities and usage.

Lists are used to store multiple items in a single variable.

Lists are created using square brackets

 

 

example: my_list = ["apple","banana","orange"]

                    list2 = [5,7,9,6,3]

 

methods in list :

append()Adds an element at the end of the list
clear()Removes all the elements from the list
copy()Returns a copy of the list
count()Returns the number of elements with the specified value
extend()Add the elements of a list (or any iterable), to the end of the current list
index()Returns the index of the first element with the specified value
insert()Adds an element at the specified position
pop()Removes the element at the specified position
remove()Removes the item with the specified value
reverse()Reverses the order of the list
sort()Sorts the list  


Tuple :

Tuples are used to store multiple items in a single variable.

A tuple is a collection which is ordered and unchangeable.

Tuples are written with round brackets.

 

example:         my_tuple = ("apple","banana","orange")

                            tuple2 = (5,7,9,6,"apple")

 

 methods in tuple:

        1) count( ) >>  Returns the number of times a specified value occurs in a tuple.

        2) index( ) >> Searches the tuple for a specified value and returns the position of where it was                                        found.

 

Set :

Sets are used to store multiple items in a single variable.

A set is a collection which is unordered, unchangeable, and unindexed.

Set items are unchangeable, but you can remove items and add new items.

Sets are written with curly brackets.

 

example:         my_tuple = ("apple","banana","orange")

                            tuple2 = (5,7,9,6,"apple")

 methods in set :

add()Adds an element to the set
clear()Removes all the elements from the set
copy()Returns a copy of the set
difference()Returns a set containing the difference between two or more sets
difference_update()Removes the items in this set that are also included in another, specified set
discard()Remove the specified item
intersection()Returns a set, that is the intersection of two other sets
intersection_update() Removes the items in this set that are not present in other, specified set(s)
isdisjoint()Returns whether two sets have a intersection or not
issubset()Returns whether another set contains this set or not
issuperset()Returns whether this set contains another set or not
pop()Removes an element from the set
remove()Removes the specified element
symmetric_difference()Returns a set with the symmetric differences of two sets
symmetric_difference_update() inserts the symmetric differences from this set and another
union()Return a set containing the union of sets
update()Update the set with the union of this set and others     


Dictionary

Dictionaries are used to store data values in key:value pairs.

A dictionary is a collection which is ordered*, changeable and do not allow duplicates.

As of Python version 3.7, dictionaries are ordered. In Python 3.6 and earlier, dictionaries are unordered.

Dictionaries are written with curly brackets, and have keys and values.

 examples :

                       dic={"name" : "bruce wayne","occupation" : "batman", "sidekick":"robin"}

 

 

Programs executed on day 2 :


1) WAP to sort a list in ascending and descending order.
 
print("enter no of values in list")
n=int(input())

print("enter the values in the list: ")
l=[]
for i in range(n):
    l.append(int(input()))

l.sort()

print("sorted in ascending order: ",l)
l.sort(reverse=True)

print("sorted in descending order: ",l)
 

2) WAP to add a key to a Dictionary.

dic={"name" : "bruce wayne","occupation" : "batman", }
print("dictionary before adding new keys")
print(type(dic))
dic.update({"sidekick" : "robin"})
print("updated dictionary is :")
print(dic)

 

3) WAP to check if a given key already exists in dictionary.

dic={"name" : "bruce wayne","occupation" : "batman", "sidekick":"robin"}

print("enter the key to be checked")
a= input()
if a in dic:
    print("yes key exists in the dictionary")
else:
    print("key does not exist")

 

4) WAP to remove duplicates from a list.

print("enter no of values in list")
n=int(input())

print("enter the values in the list: ")
l=[]
for i in range(n):
    l.append(int(input()))

print("the values in list are",l)

print("list after removing duplicate items from it : ")
print(list(set(l)))


 

Comments

Popular posts from this blog

Queue in Java

Blog on Pytest