Monday 11 May 2020


Back again in python programming. So we looking at List, sets, tuples and Dictionaries today.

Lists
Is a changeable and ordered collection which supports duplicate members. Lists are written in square brackets 
e.g fruits = ["banana", "Oranges", "cherries"] As the illustration below:-
You can access the list items by using the index number, e.g print(fruits[1]) this snippet will display Oranges because is at index 1.
also we can access the list by negative indexing, this means starting from the end to the beginning of the list, e.g print(fruits[-1]) this snippet will display cherries because is at index -1.



You can also access the list by ranging, whereby you access the list from a specific index to a specific index of choice e.g print(fruits[0:2]).


Other than ranging, we can access a list through looping whereby it will print all the list values 
for x in fruits:
    print(x)
Other than accessing the list we can manipulate the list by re-declaring the index number in the list. e.g fruits[1] = "mango"

Another manipulation technique in a list, we can add items in the list by using the append( ) method, by use of this method we can only add new items at the end of the list. e.g fruits.append("guava")
Unlike append, by the use of insert( ) method, we can add new items at a specific index of choice. Remember the method insert takes 2 arguments for its functioning. e.g fruits.insert(2, "berry")
we can remove items in the list by using pop( ), clear( ), remove( ) and del( ) methods.

pop( ) method
This method removes an item at a specified index e.g fruits.pop(0) and removes the last item if index is not specified.

clear( ) method
This method clears all the items in the list. e.g fruits.clear( )

remove( ) method
This method removes the specified item e.g fruits.remove("banana")

del( ) method
This method can delete the list e.g del fruits and as well removes the specified index e.g fruits.del[1] the opposite of remove.
Exercise: Research about other list methods and try to put them in practice.

Sets
These are unordered and unindexed collection and they are identified with the curly brackets { }. e.g
set1 = { "cat", "dog", "cow"}
print(set1)


we can access a set not by indexing but by looping through the set or by use of bit wise operators(in)
by looping
for x in set1:
    print(x)


by keyword in
print("cat" in set1)
in manipulating sets, we cant change items like list, but we can only add items by use of method add( ) and method update( ).

add( ) Method
This method adds an item in the set by appending it to the end e.g set1.add("rat")
update( ) Method
This method adds numerous items in the set. e.g set1.update([" donkey "," chicken "," fowl "])
we can remove items from the set by using the discard( ) method. Why discard because discard tends to be error free when the item to be removed doesn't exist in the set. e.g set1.discard( "donkey")
Exercise: Research about other set methods and try applying them.

Tuples
Tuples are collections which are ordered but unchangeable, and they are known for round brackets ( ).
e.g 
tup1 = ( "banana"," guava"," melon " )
print(tup1)
we can access tuples bu indexing like lists e.g print(tup1[2]) same applies to ranging and negative indexing and also the famous looping(for loop).
as we all know tuples cant me changed, but you can by conversion method, whereby you convert the tuple to a list then you add an item in the list and you convert the list back to a tuple.
e.g 
x = list(tup1)
x[2] = " oranges "
y = tuple(x)
print(y)
NB:On creating a tuple with one item you add a comma after the item or else it will become a string variable e.g tup2 = ("milk",)
you cannot remove items from a tuple but you can delete the whole tuple. e.g del tup2.
Exercise: research on other tuple methods or workarounds and keep them on practice.

Dictionaries
A python dictionary is an unordered, indexed and changeable collection which have key and values . Well know for their curly brackets { }

e.g profile = { " name " : " aineah ",
                   " country " : " Kenya ",
                   " car " : " hilux "
                }
Access of the dictionary is by key value enclosed in square brackets. e.g print(profile["name"]), also by the use of method get( ), e.g print(profile.get("name")). We can also access a dictionary by the famous loop(for loop). We can also print out all data in a dictionary by using the method value( ) in the for loop
e.g
for x in profile.values():
    print(x)
by using items we can loop all items in the dict 
e.g
for x, y in profile.items( ):
    print(x ,y)
we can change the dictionaries value by referring to its key name e.g profile["name"] = " ainea musuya" or also use the same to add a new value in the dictionary e.g profile["car color"] = " black". We can remove items from a dictionary by using the pop( ), popitem( ), del( ) and clear( ).

pop( )
This method removes a specified key from the dictionary. e.g profile.pop("name").
popitem( )
This method removes the last item inserted in the dictionary e.g profile.popitem().
del( )
this method removes a specified key name e.g del profile["car"], also removes the whole dictionary e.g del profile.
clear( )
this method empties the dictionary rendering it empty.

Exercise: research more on dictionary methods and nested dictionaries and practice on them. 
alright folks that's all for today we meet up tomorrow we look into loops.ADIOS!

Post a Comment: