Girl on Jupyter : An Introduction to Python

Week 2 : AI6 Ilorin

Eniola
ai6-ilorin
4 min readJan 3, 2020

--

Before now, I’d listened to other developers discuss the high-level programming language called ‘Python’. According to them, everyone was learning it now and it was easier to use as it allows one focus on the core functionalities of the software application being developed, by taking care of the common programming tasks that were involved.

Apparently, there are several reasons why devs should consider writing software applications in Python and while I wasn’t quite sure I needed it at the time, I still opened my heart once again to the unknown.

Introduction to Python

After successfully installing and launching all that was necessary, and as instructed, I sat, poised like a pro, ready to begin programming.

Your girl was live on Jupyter notebook — a web app that helps you create and share documents that contain live code (such as Python’s) visualizations and text written in markdown syntax. In my mind I was ready for Interactive Python.

Our tutor began with data types, as expected, easing us into a cursory confidence that only lasted minutes — well, some of us.

As if certain some of us would get confused, he began to use relatable examples that would occasionally get the sanguine hollering with laughter.

Everything from the concatenation of strings, to the sequencing and slicing of arrays was explained. Lists, tuples, matrices, selections and iterations were not left out. It was so much fun getting lost and then finding myself again in a matter of seconds.

However, foreheads creased and smiles turned upside down in no time when the code wouldn’t run, sending a few paragraphs of an error message. It was always insane when we eventually traced the root of the problem.

Imagine not getting your output because you left out a semicolon, a comma or a single measly quote.

Or consider this,

🤦🏽‍♀️🤦🏽‍♀️🤦🏽‍♀️

As tough as it seems trying not to make mistakes, there’s still the need to write clean readable code. Legends space their codes appropriately to make it appear as neat as possible (marlians don’t 😶).

A little demonstration of what I learnt and researched on ( freeCodeCamp Submissions) seems in order…

On Python Dictionaries

When working with lists, we can ‘call’ an element using an index, an integer that describes the position of the element inside the list. For example,

[“Kendrick”, “please”, “drop”, “an”, “album”]

In the above list, the element “Kendrick” has the index of 0, “please” is index 1 and so on. Also, this list obviously consists of single, independent values, separated by commas. Even if they were integers, without strings, they are still single values.

What if we need to store 2 related values and keep them visibly connected in the code?

For example, to store names of people and connect with their ages respectively.

We do that with Dictionaries.

{“Lola” : 21, “Ebun” : 30}

Notice how [] became {}

Dictionaries are key-value pairs in curly braces. Here, “Lola” is the key and 21 is the value.

A Key is a value used to access another value.

Key == Indices , A key is also a value.

A Value is accessed with their corresponding key. A key-value pair is a key with its corresponding value.

Now, there are certain rules for choosing a key. Failure to adhere to the rules would result in certain error messages after you try to run an output.

Key Rules (pun intended)

  • Keys have to be unique within one dictionary.
  • Keys have to be immutable, i.e must be constant or does not change.
  • If a key is a tuple, it can only contain strings, numbers or tuples. (Tuples can be used as keys if they contain only strings, numbers, or tuples; if a tuple contains any mutable object either directly/indirectly, it cannot be used as a key.
  • Lists cannot be keys, because they are mutable. Lists can be ‘edited’ using append[], replace[], extend[] etc. and so are not immutable.

Values can either be mutable or immutable. They have no specific rules.

Consider this,

To access values using keys;

ages = {“Lola” : 21, “Ebun” : 30}

  • Write the name of the variable that references the dictionary (in this case ‘ages’), followed by []
  • Within [], write the key that corresponds with the value:

Syntax — <variable>[<key>]

Code — ages [“Lola”]

Output — 21

The key “Lola” is being used as an index; it would have been zero if it were a list.

To add key-value pairs;

  • Write out the variable that references the dictionary
  • Followed by the new key within []
  • Then, assign new key with new value using an assignment operator (=)

ages [“Teni”] = 40

(Run it yourself)

To modify a key-value pair;

  • Write the variable that references the dictionary
  • Followed by an existing key in []
  • Now assign it a new value

( The key-value pair will be modified. Only values can be modified, because keys are immutable remember?)

Thanks to Malhub and AIIlorin for the platform, not excluding our two brilliant instructors; the one who began the introduction and the one who tested us vigorously to see if we were actually getting it. Can’t wait for the next class.

--

--