Saturday, 19 September 2020

Part Two: Lesson One: Collections

In part one we learned about variables whereby a value is assigned to it. Example x = 4. Here, x is the variable and 4 is the value currently assigned to it.

However, it is possible to assign more than one value at a time to a variable and this is where the concept of collections come in python 3. The type of collections are lists, tuples, sets and dictionaries. What they have in common is that they can store multiple values per variable and these values can be manipulated but they may differ in how this is done.

Let us discuss the most used of the collections first, that is lists. Please follow along by typing each of the lines below and running them in an IDE like PyCharm or on repl.it.

Lists

Lets have a variable

    x = 4

    print(x)

If we wanted to store four values from we would use a list using square brackets

    x = [1, 2, 3, 4]

    print(x)

Another way to create lists is to use the built-in function, list(). we can also create an empty list which we would populate later.

    x = list()

    x = []

A list can contain multiple types of values like strings, integers, floats and even other lists

    x = [1, 2, ‘Jay’, 0.4, (12, ‘dozen’)]

    usernames = [‘Jay’, ‘Ali’, ‘Mike’]

    print(x)

    print(usernames)

Here are some ways to manipulate lists. We can check the length or number of items in the list with len() function

    print(len(x))

    print(len(usernames))

We can access the index of an item in a list. The index are numbered from 0.

    print(x[0])

This would be 1.

    print(x[1])

This would be 2.

    print(x[-1])

This would give the last value.



We can access a range of values

    print(x[0:2])

This would 1, 2 but not including the third index ‘Jay’.

Other options are

    print(x[:3])

This would count from index 0 up to index 2.

    print(x[1:])

This would count from index 1 to the last index.

    print(x[:])

This would give all the items in the list

It is possible to add items to a list and to also remove items from a list using methods

    x.append(5)

    print(x)

This would add 5 to the end of the list

    x.insert(1,6)

    print(x)

This would insert 6 at index 1 in the list

    x.remove(5)

    print(x)

This removes 5 from the list

    print(x.pop())

This removes the last item in the list

Other methods used with lists include sort, reverse, max, min, sum.

Tuples

The main difference between lists and tuples is that lists can be modified, that is they are mutable whereas tuples are not mutable. Hence, they have less methods associated with them compared to lists.

Once a tuple is created you can not append to it.

    y = ()

    y = tuple()

These create empty tuples that can be populated later.

    passwords = (1234, 5678, 9001)

    print(passwords)

We would not be able to append or update tuples.

Sets

Sets are an unordered collection and there are no duplicates in sets

    name = set()

The above create empty set to be populated later

Sets are usually applied to collections where duplicates are not permitted.

Methods associated with sets are union, intersection, difference.

Dictionaries

Just like the normal dictionary we know, dictionaries are a type of collection that have keys and values. Examples below:

    name = dict()

    name = {}

these creates empty dictionaries to be populated later on.

    student = {‘name’: ‘Ali’, ‘age’: 22, ‘subjects’ : [‘English’, ‘Maths’]}

we can access a key like subjects as follows:

    print(student[‘subjects’])

We can also use .get method especially if are not sure a key is in the dictionary to avoid error messages

    print(student.get(‘name’)

we can add, update or delete keys and values as follows:

    student[‘name’] = ‘Jay’

the above updates the name to Jay instead of Ali.

    student[‘phone’] = 555222

this adds a new key ‘phone’ and a value 555222 to the student dictionary.

To delete we use the del command or the .pop method

    del student[‘age’]

or

    student.pop(‘name’)

Congratulations, we can now use and manipulate lists, tuples, sets and dictionaries.

In the next lessons we would put them to use.



No comments:

Post a Comment