+254 728 680 519

Learning how to walk

Hello to you hope your Sunday is good, it can’t be any worse than the day before can it with this virus disrupting everything? What do you miss the most because of the pandemic? I am sure that's a long list anyway back to Python. 

But wow the world is changing and in a big way and most of these changes will be the status quo. We have seen students using the internet to learn, people working from home, pastors conducting online sermons, etc. the internet is starting to become a basic need and the need will only grow with time. This is the best time to start learning python if you haven’t already. If you have gone through my previous posts you should have python installed. Before we go further I have somethings I want to go through due to the response I have received from the posts.

‘The learning material is not straight forward for a beginner’ well to be honest before you dive into python you should have basic I.T knowledge it is like the saying goes before you learn how to run you must walk first. Now to be specific to this blog, our blog will assume this from you i.e. you know things like navigation around discs in your computer, knowledge of stuff like windows Linux mac depending on the o.s you are using and well basic IT stuff. I will try to write posts which are very easy to follow but somethings I cannot teach you. Before my dive into the language, I was a bachelor in IT and had covered the CCNA course but still, it has taken me this long to comfortably say I know the program and can do a bunch of things with it. If you are totally new in I.T this is a little out of your league and you have to go further back to the drawing board.

Now to the second issue, I want to talk about the content. In our post how do computers play, we talked about the four branches of a language that you need to know before you could speak and write that language fluently and these were alphabets, lexical, syntax, and semantics. By now you should know these and what they mean to python. Now one of the mistakes that has taken me this long to read and write this language is that most of the python tutorial content out there starts with teaching the alphabets, lexical, syntax then semantics. The way to easily learn python according to this site will be we will be starting with semantics then fit the other three as we go on. What do I mean by this?

Let’s look at an example. You need to create a program that tracks Coronavirus patients’ temperatures during the quarantine period which is now 14 days. Below will be the output we want to be calculated from our system:

  • The highest average temperature of all the patients.
  • At what temperature is the patient in a critical state
  • How does the temperature change with medicine A taken
  • How does temperature relate with ages of patients over the quarantine period

Now all this is very fictional and in no way do I know anything medical it is just a literal example.

Now we have the output of our program now we think of how to make this work. Everything starts on paper that is what I always say. So the first thing we need to think about is the requirements of our system, what does our system need to capture;

  • Collect patient name, age, gender, location, demographics basically
  • The temperature of the patient. (Maybe collected from a sensor fitted on the patients' bed or room)

From the above requirements we need to think what our program needs to do;

  • Capture the info
  • Process the info
  • Output the info

Capture the info

Here we will be thinking of things like what data types do we need to collect this info. By datatype, I do not mean the basic data types like for name using text, for age use number I mean the datatypes within python. The core datatypes within python are strings, list, tuples, dictionaries, numbers, and sets (more on this later). Looking at the data we are collecting, all of it originates from a single human being who has a unique attribute say a national identification number. So the age, gender, location can be directly linked with this id number. How best is it to capture this info? in my view, we will go for the dictionary data type which works just like a dictionary where you have a word then the meaning of the word follows so in our case might be John: age 24,  location Nairobi, sex male. We also need to capture the temperature which in this case is a number in degrees Celsius which is always written using a single decimal place so the normal data type is a decimal type in this case. But let’s look at something very crucial. The temperature just like age belongs to John but john has one age at any particular time of the quarantine period (assuming his birthday doesn’t find him in the quarantine of which he will still have a certain number representing age) but with this system, John within 24 hours will have 24 temperatures reading one for each hour. How do we capture this info?

So from our above scenario, we have two kinds of attributes related to john. We have one to many relationships where john has 24 temperature readings within a day and a one to one relationship where at any given time this data is specific say john’s age which is 24. So far our data sample may look like something like this

{123456:{'name':'John',' age' : 24, 'location' : 'Nairobi', 'sex' : 'male'}}

Now we need to add these temperatures within the entry john. This is where python is really useful, what we can do is add a tuple data type to the entry. What is a tuple; a tuple is an immutable data type which means that this data once entered cannot be changed and this is the logic we need for temperatures. The opposite is lists which are mutable and can be altered at any time. Now we need to merge this with john. Below is how now our data will look like

{123456:{'name': 'john','age': 24, 'location': 'Nairobi', 'sex': 'male', 'temp': (35.2, 38.5, 40.8, 42.0, 38.1)}} # A temp reading for every hour

This is called nesting. It is a bit advanced because we have a dictionary within a dictionary and also the tuple data type in a single structure. Don't worry we will get this covered for the meantime get the concept i.e the semantics.

Process the info

We are done with collecting data now we need to analyze what our system will do this temperature. So the requirement is we need to calculate;

  • The highest average temperature of all the patients- Here we will need to collect the highest temperature recorded per patient on each given day, add all this data, and divide by the number of patients to get the average temp.
  • What times of the day are the temperatures normally highest? For all patients retrieve the highest temp recording and at what time of the day.
  • How does the temperature change with medicine 'A' taken? Capture all relevant info based on the type of medicine given and do a comparison etc.

I hope you see what we are trying to achieve. Basically we are trying to create a flow sequence that we will convert to machine language using the high-level programming level python language. Now the nitty-gritty begins of writing all these functions in python for us to complete the system and output the info.

All the best and STAY HOME and do some googling!


Playing Basketball, Arsenal fan, Python, and coffee!