Programming BasicsProgramming

Introduction to Programming in Python

Computer – A Smart dumb machine

Do you think that your computer is powerful? Computers are particularly good with mathematical computations and are powerful. And they almost never make mistakes. And they don’t get bored doing the same thing repeatedly.

But there is a problem, and it is that they cannot solve problems themselves

It is us, humans, who need to give them very precise step by step instructions to solve any problem.

Computers are exceptionally good with basic CPU instructions but anything a bit complex, and it needs human intervention to get precise steps to solve the problem.

So, if we human don’t give them precise step by step instructions (Programs), computers are just a dumb machine.

What language does your computer understand natively?

if you can read this article, you sure can understand English and at least can count from 1-10, right?

Computers do not understand the human language. The natural language of computers is the binary code — 1 and 0.

Even that is a bit of exaggerations, they don’t even understand 0 and 1s.

These 0 and 1’s just represent two states: on (1) and off (0).

So, if there is voltage, computer takes it as 1, and if there is no voltage, computer takes it as 0.

Confused? That how it understands your videos, photos, word documents and everything?

Don’t worry we will get into details later in some other article.

But for now, just, understand this that, everything is converted to binary 0 and 1’s (Voltage and not voltage) for computers.

All sounds and music, videos, whatever you are working on, are stored in RAM and the hard drive as 0s and 1s.

Why we need programming languages?

We are clean about few things till this point in article.

First, that computers are not smart if you don’t give them precise step by step instructions.

Second, that they just don’t speak same language as us. They just understand 0’s and 1’s.

Passing instructions to computers in language of 0’s and 1’s is going to be difficult.

So, to pass these instructions to machines who speak binary, we do so in a language that’s closer to our own natural language.

Programming languages are close to our natural languages. But they are more structured and must be thoroughly learned.

Now there are high level languages or low-level languages.

High level programming languages are close to English and farther away from the native machine language than low level languages.

How computer understand programming languages?

So programming language is a way for us to pass out instructions to computers.

But don’t be mistaken, that computers can understand these languages directly, Remember, they only understand two things 0s – 1s.

Answer is simple. There is a translator in between.

Translator has the responsibility of converting your source code to the machine language.

Translators can be any of:

  1. Interpreters
  2. Compilers
  3. A hybrid of Interpreters and Compilers

1. Interpreters

processes the source code line by line and runs every line in the final program or app. Example. Python

2. Compilers

Convert the source code in its entirety via a compilation process to binary. The binary is then executed. If there were errors in the source code, they are detected during the compilation time and flagged.  Example: C, C++.

3. Hybrid of Interpreters and Compilers

A hybrid translator is a combination of the Interpreter and Compiler. A popular hybrid programming language is Java. Java first compiles your source code to an intermediate format known as the Bytecode.

So, whenever you will write these instructions in any programming language, you will save it as a text file (this is your program, and you will normal refer this to source code).

Depending on what language you used for your program, translator will take you code, and

Covert it to machine code for your computer.

So now computer knows what instruction you want it to follow.

What programming language you should learn as a beginner?

It should depend on that what kind of career you want in future.

Every language can do many things. But you should check that for what purpose you are learning to code.

So, suppose if you want to be a frontend developer, you can start with learning JavaScript. If you are interested in a career in data science, go for Python. If you want to be a embedded developer in future, start with C.

But if you don’t have any specific in mind and you just starting, I will suggest starting with Python as first language.

Let’s just check that how widely is python being used.

Nearly 66.7% of professional developers who responded to the 2020 Stack Overflow survey loved coding in python as per Stack Overflow surveys this year.

As per the Stack Overflow data. If we look at technologies that developers report that they do not use but want to learn, Python takes the top spot for the fourth year in a row.

And these numbers are justified too, as python is awesome for developers.

Reason for choosing python is that it is Incredibly efficient language: your programs will do more in fewer lines of
code than many other languages would require. Python’s syntax will also help you write clean” code. Your code will be easy to read, easy to debug,
and easy to extend and build upon compared to other languages.

Python has tons of packages and libraries for everything. So even though it seems easy to learn, but it does not at all mean that it’s not immensely powerful and not capable.

Python really has lot of packages and support for many things. Want to be a web developer, python has frameworks like flask and Django.

Want to analyse some data, you have data frames, pandas and scikit to use.

Want to learn machine learning? All major ML platforms and libraries have support for python.

So, If you know python, you have that flexibility to try anything.

Let’s write our first Program

Our first program is to just print something on your console.

Now, if you have already have an IDE installed on your system, you can use your own local editor.

But as we are just beginning, we will focus more on programming concepts and will cover using a local editor later.

So for now we will use an online editor reapl.it https://repl.it/.

You need to register first for repl.it and once your account is verified, you can start writing your programs and can save them online.

Here is my dashboard looks like after registration

Graphical user interface, text, application Description automatically generated

Do you see create and a + icon in the area?

Click on + icon and it will ask you language you want to code in and name of your program.

Graphical user interface, text, application, chat or text message Description automatically generated

Choose python as your language and name your program whatever you want It to.

I have named my program as HellpPython.py.

Add following code to you code area and run the program

#Hello Python program
print("Hello Python")

It should print ‘Hello world’ text on your console on side.

 Let’s take a closer look at what Python does when you run program . Even for this small program, Python does a fair amount of work.

When you run this program, editor then runs the file through the Python interpreter, which reads the program and determines what each word in the program means.

 For example, when the interpreter sees the word print, it prints to the screen whatever is inside the parentheses.

 As we write programs on our IDE, editor highlights parts of your program with different colors. For example, it recognizes that print is the name of a function and displays that word in orange (color will vary with different IDEs). It recognizes that “Hello Python!” is a string (and not Python code) and displays that phrase in red. This feature is called syntax highlighting and is quite useful as you start to write your own programs.

If you miss something in your code, suppose you missed a comma at the end of you screen. Syntax highlighting will seem wrong, and you can use it as hint to check that what’s wrong with your code.

See following screenshot for example.

IDE has set purple color for ‘print’ statement as its has recognized it as a function. Brackets are black and passed value ‘hello python!’ is in red.

In statement at line number 2. Do you see anything wrong? IDE could not recognize ‘prints’ as a function (as it’s a typo) so it does not color codes it and it’s a hint for user that its not a predefined function.

Not look at line no. 3, IDE has color coded ‘hello python!’ as red but then closing bracket ‘)’ is also red. It’s not black as first statement, as we have forgotten ending comma symbol to close string. And IDE could not understand that when our string ended and considered closing bracket as part of string. So, it never finds out ending ‘)’ for closing first ‘(‘ and will give compile error.

So please make it a habit to keep checking syntax highlighting, as it can help a lot in debugging.

How should you approach writing any complex program?

Computers can be programmed to solve many complex problems but for that you need but before they can be used for solving problems, the problem itself and the ways in which it could be resolved must be understood.

Computational thinking techniques help with these tasks.

Computational thinking allows us to take a complex problem, understand what the problem is and develop viable solutions.

There are four key techniques (cornerstones) to computational thinking:

  1. Decomposition – breaking down a complex problem or system into smaller, more manageable parts
  2. Pattern recognition – looking for similarities among and within problems
  3. Abstraction – focusing on the essential information only, ignoring irrelevant detail
  4. Algorithms – developing a step-by-step solution to the problem, or the rules to follow to solve the problem

So before, you start typing anything on your computer IDE, analyse the problem, you are trying to solve and make a algorithm for solving it.

Algorithm is A step-by-step solution. Where each step has clear instructions. Like a recipe.

Done with algorithm? If you are clear with steps to solve the problem, you can convert algorithm to create a pseudocode. pseudocode is n more simple form of an algorithm which involves some part of natural language to enhance the understandability of the high-level programming

Let’s take an example.

Say, we are creating a billing system for a medical shop. Condition is, that if customer is a senior citizen (Age>=60), he will get an additional 20% off. Otherwise, customer gets only 5% discount on shopping.

So, if we break this problem in smaller problems, we will get an algorithm to solve this:

  1. find out if age of customer
  2. if the customer is older than sixty, then say “You are eligible for a 20% discount”
  3. otherwise, say “You are eligible for 5% discount.”

Now let’s change this algorithm to pseudocode.

OUTPUT "How old are you?"
INPUT User inputs their age
Save the user's input in the age variable
IF age >= 60 THEN OUTPUT "You are eligible for 20% discount." 
ELSE OUTPUT "You are not eligible for 5% discount."

To convert the algorithm or pseudocode into a program, take each step, and write an equivalent instruction in your programming language.

This is how, your program would look like.

If you can not understand much of this code for now. Don’t worry much, we will get into python’s syntax soon.

Importance of Indentation in python

Most of the programming languages like C, C++, and Java use braces {} to define a block of code. But Python uses indentation for block of code.

Python uses indentation to determine the grouping of statements. You can use one or more spaces or one or more tab, just that within the same statement always use one or the other. Is recommended to use spaces instead of tabs but is not required.

A code block (body of a function, loop, conditionals etc.) starts with indentation and ends with the first unindented line.

This is how our last program looked like

age = int(input("How old are you?")) 
if age >= 60: 
  print("You are eligible for 20% discount.") 
else: 
  print("You are eligible for 5% discount.")

Do you see indentation for this program?

The lines print(‘You are eligible for 20% discount.’) and print(“You are eligible for 5% discount.”) are two separate code blocks. The two blocks of code in our example if-statement are both indented four spaces.

You must use the same number of spaces in the same block of code, otherwise Python will give you an error. Without indentation, Python does not know which statement to execute next or which statement belongs to which block. This will lead to Indentation Error.

In next chapter we will dive deeper into programming basics and will cover about editor, debugging, variables and Strings.

Leave a Reply

Your email address will not be published. Required fields are marked *