Search

Introduction to Programming with Python

Updated: Oct 2


I was afraid of Programming not very long ago. But as it turned out, I got interested in Data Science and Machine Learning. Having chose this field, there was really no choice with me bu to take my fears heads on. I decided to learn computer programming, and as it turned out, there is nothing to fear about. There is no dearth of reading material(in fact, the opposite is true. There is so much of reading material that it is overwhelming.). There is so much abundance of content that it poses another challenge, where to start and what is the right path to tread.

That is where I am going to help you with. I will hold your hand and walk with you on this exciting journey to start a new chapter in your learning experience. Let's learn how to talk to your computer, which is essentially what Computer Programming is.


1 What is Programming?


We all have experienced the end result of computer programming. When we play our favorite games on mobile or computers, open any mobile application or computer software, take a digital image, see photos on any screen or even send an email. There are computer programs running in the background, doing all the work, which we never see.

Computer is an obedient servant, but not a very wise one. They understand exactly what we tell them, nothing more, nothing less. Hence, when we give them instruction (by way of programs), these instructions must be specific and sequential.

The most suited analogy I can think of is a recipe. Lets take an easy recipe to make a bread (chapati or baked bread, or both). There are only few ingredients like flour, water and yeast (in case of baking). Each ingredient must be in the specific proportion. You put one cup water instead of half cup and the dough is too runny to hold its shape. In the same way, the instructions we give to the computer must be specific in order to get the desired results. The computers are very literal. They try to execute our commands exactly. If we give them bad instructions, the commands may not run at all. Or worse, it can have bug or it can crash altogether.

The other characteristics of computer program is, they are sequential, just like the steps in a recipe. The order of the steps will determine the final product. What if, while making a chapati, we put it on fire directly, instead of heating them on hot plate (tava) for some time? They will simply break and lose shape. Similarly, What if the flour was first put in oven, and then water was added to make dough? The cooked flour will not make dough. Order of actions matters. The same thing can be said about programming. It's all about giving computers the right instructions and the correct sequence of steps to produce the desired result.

So, to recap, programming is how we communicate with the computer. It's characteristics are instructions that are specific and sequential. And just like a recipe, when we get our programs right, we get to enjoy the final product.


2 How does a computer program work?


In order to instruct the computer to do anything, the instruction must be written in form of a computer program. To write a computer program, you have to tell the computer step by step, exactly what you want it to do. The computer then runs or executes the program and follows it step by step, to complete the task.

When you are telling the computer what to do, you also tell it how to do it. This how is the computer algorithm.


2.1 Computer Algorithm

Definition: In computer science, an algorithm is a finite sequence of well-defined, computer-implementable instructions, typically to solve a problem or to perform a computation. Algorithms are always unambiguous and are used as specifications for performing calculations, data processing, automated reasoning, and other tasks.

In computer systems, an algorithm is basically an instance of logic written in software by software developers, to be effective for the intended target computer(s) to produce output from given input. An optimal algorithm would produce faster results than a non-optimal algorithm for the same purpose. That is why algorithms, like computer hardware, are considered technology.

Let me give you few examples of Algorithm, which will make it more understandable. Lets take a real life example first, and them computational examples.

Real life Example of algorithm

Write an algorithm of how to make tea. Ingredients are water, sugar, milk and tea leaves to make two cups of tea.

  • Step 1: Start

  • Step 2: Take a pan with one cup of water in it.

  • Step 3: Light the burner and put the pan on it.

  • Step 4: Bring the water to a boil.

  • Step 5: Add two table spoon sugar and two table spoon Tea leaves in the boiling water.

  • Step 6: Add one cup of milk to the boiling mixture.

  • Step 7: Boil the contents in the pan for one minute.

  • Step 8: Switch off the burner.

  • Step 9: Filter the tea leaves from tea using a sieve.

  • Step 10: Pour the tea in cups and serve it.

  • Step 11: Stop

Computation Example of algorithm

Write an algorithm of how to sum 2+2 to give 4 as result.

  • Step 1: Start

  • Step 2: Type 2 + 2

  • Step 3: Press Enter

  • Step 4: Stop

The algorithm are made of 3 kind of statements. Any combination of these 3 kinds can be present in an algorithm.

  1. Sequence Statements

  2. Selection Statements

  3. Looping or Iterating Statements

2.2 Sequence Statements

In these Programs, one statement is executed after the another, in a sequential manner.

The above two examples, making the tea and calculating 2+2, both are examples of Sequence statements. Lets take on more example.

Example Calculate age of a person given his year of birth.

  • Step 1: Start

  • Step 2: Take the year of birth and Store in YOB.

  • Step 3: Take the current year and store in CY.

  • Step 4: Subtract YOB from CY and save in Age.

  • Step 5: Print Age.

  • Step 6: Stop

2.3 Selection Statements

In these Programs, some part of the program is executed based on a certain condition being fulfilled or not. If the condition is fulfilled, computer will execute one part of the program, and if the condition is not fulfilled, the other part of the program is executed.

Example Write and algorithm to check if a person has fever or not. Condition is, its fever if temperature is above 99 Degree Fahrenheit.

  • Step 1: Start

  • Step 2: Take the temperature and Store in TEMP.

  • Step 3: Check TEMP value. If its more than 99 Degree Fahrenheit, then go to step 4, otherwise go to Step 5.

  • Step 4: Print "He has Fever" and go to step 6.

  • Step 5: Print "He does not has Fever".

  • Step 6: Stop

2.4 Looping or Iterating Statements

In some programs, certain steps are executed again and again, based on conditional test. These repetitions are called iterations. The iteration is done using one or more looping technique. Hence this kind of programs are called Looping or Iterating statements.

Example Print all the numbers from 1 to 10.

  • Step 1: Start

  • Step 2: Store 1 in i.

  • Step 3: Check if value of i <= 10. If yes, go to step 4. If False, go to step 7.

  • Step 4: Print i.

  • Step 5: Increase the value of i by 1.

  • Step 6: Go to Step 3.

  • Step 7: Stop

In this example, the Steps 3 to 5 are iterated till the condition in the step 3 is fulfilled. Once it returns false, the loop ends.

2.5 Flowchart

The algorithms can be written in graphical form, using some symbols. This graphical representation is know as flowchart.

Flowcharts have some standard symbols for representation of functions. The same are tabulated below.



Let us see an example of flowchart.

Example Write the program to check if a number is even number or odd number.


In simple terms, Programming is ideas converted into 'step by step' instructions that a computer can understand. This step by step instruction, is called algorithm.

These algorithms and statements form a program. But computer is not going to understand these if the instructions (programs) are not formalized. The normal human language (If there is any such thing !) is very far from what the computer can understand. Hence we need to talk to the computer in it's own language. The language that the computer can accept.

Language is the keyword.


3 Human Languages vs Computer Languages

History of languages is as old as history of human civilization itself. It started as expression in form of cave drawings, then people started communicating by speaking specific words, which carried meaning. The languages used by us humans have evolved over time, but for a long long period, it remained verbal. Languages as medium of communication date back to more than 50,000 years, however we started writing only roughly 6000 years back.


I cannot doubt that language owes its origin to the imitation and modification, aided by signs and gestures, of various natural sounds, the voices of other animals, and man's own instinctive cries. — Charles Darwin, 1871. The Descent of Man, and Selection in Relation to Sex

In essence, we humans use language as a tool to express our thoughts, and communicate with each other. It's not only what we speak, but also what we express through voice modulations, Body language and of course the word (both verbal and writing).

So it was natural that when we created computers, and we wanted to talk to them, to give them instructions, we created some languages for this purpose. These languages are of two types.

  1. Machine Language (or Machine Code) - It is made of "0" and "1" and only computers understand this.

  2. Programming Language - These are the languages in which we humans give instruction to the computers (Machines).

The programming language is converted to machine language, so that computers understand human instructions (Programming language).

The question arises, Why do the computers need machine language? Why can't we talk to them in plain English (or any other human language)?

The reason lies in the lack of intelligence in computers. The computers are not intelligent beings like us. So they can not understand the nuances of advanced (or very high level) languages like humans. They can not understand that same words mean different things, when spoken in different tones or pitch or when combined with different body languages or facial expressions.




The computers understand only one language, that is Machine Language, written in binary digits of 0 and 1.