NAKAWEESI JOGIA
5 min readMar 4, 2019

--

AN INTERESTING STORY ABOUT PYTHON PROGRAMMING LANGUAGE.

Every programmer has a story about how they learned to write their first program. For the one week I have spent when I was intensively learning python as a programming language.

I have personally found out that python is a learn able language if I’m to say it in simple language. I got a chance to study about data structures in great depth.

We call them data structures. Accessing, inserting, deleting, finding, and sorting the data are some of the well-known operations that one can perform using data structures.

There are mainly seven data structures but that I understood. But if I’m to explain data structures in an a non-technical way, I would say this;

Like every new technology coming up always have a necessity also does data structures had one. Taking an example of going to unorganized library and you try to look for a book, you have to put in several hours organizing and searching for the book.

And the next day the librarian steps in the library and looks at the mess you have creates then she/he tries to organize the library in a different way.

Just like you can organize books in a library in many different ways, you can structure data in many different ways. So, we need to find a way to organize our books (read: data) so that we can find our book (read: data) in a very efficient and fastest way.

I have found out that they are mainly seven data structures and they include;

Arrays

Stacks

Queues

Linked lists

Hash Tables

Trees

Graphs

But within the course of the week I realized that that lists and dictionaries stands out more about me in python, though I got a chance of learning more other things.

A list is a collection of items in a particular order. You can make a list that includes the letters of the alphabet, the digits from 0–9, or the names of all the people in your family

You can put anything you want into a list, and the items in your list don’t have to be related in any particular way. Because a list usually contains more than one element, it’s a good idea to make the name of your list plural, such as letters, digits, or names.

In Python, square brackets ([]) indicate a list, and individual elements in the list are separated by commas. Here’s a simple example of a list that contains a few kinds of bicycles:

bicycles.py bicycles = [‘trek’, ‘cannondale’, ‘redline’, ‘specialized’] print(bicycles)

If you ask Python to print a list, Python returns its representation of the list, including the square brackets:

[‘trek’, ‘cannondale’, ‘redline’, ‘specialized’]

Because this isn’t the output you want your users to see, let’s learn how to access the individual items in a list.

Organizing a list; often, your lists will be created in an unpredictable order, because you can’t always control the order in which your users provide their data.

Although this is unavoidable in most circumstances, you’ll frequently want to present your information in a particular order.

Sometimes you’ll want to preserve the original order of your list, and other times you’ll want to change the original order. Python provides a number of different ways to organize your lists, depending on the situation.

I mainly understood so much how to write sorting of lists and I was able to write a program which sorts lists shown below;

for i in lista:

if isinstance(i, int):

if i % 2 == 0:

even.append(i)

else:

odd.append(i)

elif isinstance(i, str):

characters.append(i)

mydict[‘evens’] = sorted(even)

mydict[‘odds’] = sorted(odd)

mydict[‘chars’] = sorted(characters)

return mydict

print(list_sort([2,0,6,5,1,7, ‘a’, ‘z’]))

And it returns out this dictionary below;

\Desktop\lists\listsort>python list_sort.py

{‘evens’: [0, 2, 6], ‘chars’: [‘a’, ‘z’], ‘odds’: [1, 5, 7]}

Sorting a List Permanently with the sort() Method.

Python’s sort() method makes it relatively easy to sort a list. Imagine we have a list of cars and want to change the order of the list to store them alphabetically. To keep the task simple, let’s assume that all the values in the list are lowercase.

cars.py cars = [‘bmw’, ‘audi’, ‘toyota’, ‘subaru’] u

cars.sort() print(cars)

The sort() method, shown at u, changes the order of the list permanently. The cars are now in alphabetical order, and we can never revert to the original order:

You can also sort this list in reverse alphabetical order by passing the argument reverse=True to the sort() method. The following example sorts the list of cars in reverse alphabetical order:

cars = [‘bmw’, ‘audi’, ‘toyota’, ‘subaru’]

cars.sort(reverse=True) print(cars)

Again, the order of the list is permanently changed:

[‘toyota’, ‘subaru’, ‘bmw’, ‘audi’]

Sorting a list alphabetically is a bit more complicated when all the values are not in lowercase. There are several ways to interpret capital letters when you’re deciding on a sort order, and specifying the exact order can be more complex than we want to deal with at this time. However, most approaches to sorting will build directly on what you learned in this section.

Also loved so much when I was performing so tests on the sort list.

import unittest

from listsort.list_sort import list_sort

class listsortTest(unittest.TestCase):

def test_integer_input(self):

self.assertEqual(list_sort(5), ‘Invalid Input’)

def test_string_input(self):

self.assertEqual(list_sort(‘string’), ‘Invalid Input’)

def test_empty_list(self):

self.assertEqual(list_sort([]), {‘evens’: [], ‘odds’: [], ‘chars’: []})

def test_no_string(self):

self.assertEqual(

list_sort([4, 3, 2]),

{‘evens’: [2, 4], ‘odds’: [3], ‘chars’: []}

)

def test_no_even(self):

self.assertEqual(

list_sort([9, 3, 5, 1, ‘d’, ‘a’]),

{‘evens’: [], ‘odds’: [1, 3, 5, 9], ‘chars’: [‘a’, ‘d’]}

)

def test_no_odd(self):

self.assertEqual(

list_sort([10, 2, 8, ‘c’, ‘f’]),

{‘evens’: [2, 8, 10], ‘odds’: [], ‘chars’: [‘c’, ‘f’]}

)

def test_complete_list(self):

self.assertEqual(

list_sort([4, 9, 2, 3, 5, 1, ‘d’, ‘a’, ‘c’, ‘f’]),

{‘evens’: [2, 4], ‘odds’: [1, 3, 5, 9], ‘chars’: [‘a’, ‘c’, ‘d’, ‘f’]})

if __name__ == ‘__main__’:

unittest.main()

When I run it it returned the code below;

e:\Desktop\lists>python list_sort_test.py

{‘evens’: [0, 2, 6], ‘odds’: [1, 5, 7], ‘chars’: [‘a’, ‘z’]}

…….

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

Ran 7 tests in 0.003s

OK

Dictionaries in python. Here I was able to learn how to use

Python’s dictionaries, which allow you to connect pieces of related information.

You’ll learn how to access the information once it’s in a dictionary and how to modify that information. Because dictionaries can store an almost limitless amount of information.

I’ll show you how to loop through the data in a dictionary. Additionally, you’ll learn to nest dictionaries inside lists, lists inside dictionaries, and even dictionaries inside other dictionaries.4

--

--