Saturday, 15 April 2017

How to become a Python Jedi - A Python programming Tutorial Part 2 - Instruction flow control

We all know that programming is a set of instructions, but many a times we forget to think that this set of instructions should make sense to be called a computer program. You can get your dog to fetch a stick just by yelling “FETCH” but if type an instruction like “calculate” it will do nothing other than just sitting there and gaping at your face with that ugly python command prompt look.

To command a machine to do a task we need to pass a set of “precise and clear set of instructions that flows seamlessly from one step to another”. This flow of instruction is known as flow control or instruction flow control in the programming world. One handy tool to do this is a flowchart.

What the hell is a flowchart?
Well simply put a flowchart is a graphical representation of instructions/logic sets that helps you to visualize the logic that you have formulated for a task.
Let’s get into an example….

Let’s say that you want to automate the login to your facebook account and want to code the entire process in python, but before you get cosy with your keyboard you need to think and devise a series of steps as below-

  • Type the facebook url
  • Search for the user id & password text box
  • Enter your credentials
  • Press the Enter button

It’s time to visualize the steps via a flowchart like the one below -

The above flowchart is a visual representation of the steps that you want your code to follow. The boxes represent the different units/steps of the logic that was devised earlier and the arrows represents the “flow” of the program control between different steps of the logic. In actual practice your program should flow through the steps in the same manner as is represented in the above flowchart.

Devising steps/algorithm for a computer program is not as straightforward as is represented in the above example but I to get a taste of what a flowchart is and how to design a basic flowchart the above example will suffice.
Below are some practice exercises that will help you get going till the next tutorial which will be coming your way soon.

  1. You want to prepare a large jug of lemonade. Write down the steps by step procedure for the same and devise a flowchart that represents the steps.

  1. Devise a flowchart to visualize the steps to login to Gmail.

  1. Create a flowchart to represent a up of coffee. Also include the necessary steps to check if the gas stove is on or not and whether water has boiled already etc.

So, folks this was all for this week but we shall return with another part of this tutorial with some new concepts to learn.

Subscribe to my youtube channel to watch new python programming tips and tutorials.

If you have any questions, suggestions or comments you can post your comment here or you can also bug me on Quora, Twitter or on Facebook.

Monday, 10 April 2017

My new Youtube channel

New YouTube channel

I always say that thecodingproject exists because of you, it exists because I want to share with you the knowledge and experience that I have gathered over time and make it available for all of you so that you can pursue your passion and love for coding. It's been my continuous endeavor to present you content that is both interesting and useful so that learning coding is no longer boring. To achieve this I have launched my first YouTube channel . Now all of my python coding tips & tricks are also available on YouTube. Visit the link and subscribe to my channel.

Saturday, 1 April 2017

How to become a Python programming Jedi - A Python programming Tutorial Part 1

Long Long ago in a Galaxy far far away there was a planet named Quora. There was a growing agitation among the planet’s inhabitants to learn the way of the Python programming Force and become a master in the way of the Python programming Jedi, but very few knew that the secret art of the Jedi is embedded in a book named as TheCodingProject….

So, let’s begin the journey of learning the secret art of the Python programming Force and we shall together make the journey towards becoming the Python Jedi….

What is Python?
As per Wikipedia “Python is a widely used high-level, general-purpose, interpreted, dynamic programming language.” Python is an open source programming language which was conceived in the late 1980s and its implementation began in December 1989 by Guido van Rossum (more on it here). For us it’s sufficient to know that Python is :

  • High level language - Python is a high-level programming language because it provides strong “:abstraction” in the sense that it automates the tasks like “memory management” , “garbage collection” etc. so that the programmer does not have to handle that explicitly while coding which makes the life of lazy programmer like me a simple affair. More on :high-level programming language here.

  • Interpreted - Python is an Interpreted programming language because it doesn't runs off a compiler rather it has to be processed further by another code so that the coding that we have done is understood by the computer. So, such programming language can be written using “natural language” which again helps people like me to spend less time in writing code.

  • Dynamically typed language - Python is dynamically typed because in python every variable is bound to an object (more on Objects later) and every object has to be assigned to a Type (we will dive into what “Types” are later).

How do I install Python?
Installation of Python is very straightforward. The Python installer and the installation instruction can be found here. Now that Python is installed on our system and since we are the supergeeks that we are, we don't want to waste time and dive straight onto typing our first Python code. But in order to run the python interpreter from the command line or executing a python file from command line our machine should know the folder where python is installed.

  • We have to visit the following path-

Under system variables we create a variable “PythonPath” and enter the folder name where python is installed and click on ok button. For me it’s C:\Python34, your directory path may vary.

In Linux python is installed by default (perks of using Linux 😉. So, congratulation Linux users you don't have to worry setting pythonpath.

IDLE is an “Integrated DeveLopment Environment” for Python, which is bundled with every Python distribution.  It is completely written in Python and the Tkinter GUI toolkit

How do I do it in windows?
In windows goto the start menu → All programs → search for the folder where python is installed (for me it's Python 3.4 ) → click on the folder → click on the IDLE (Python 3.4 GUI – 32 bit)

Once IDLE is launched you will be greeted by the following screen –

How do I do it in Unix?
Did you forget what I said above? Linux users get Python by default 😎 In Unix just open the terminal and type ”python” for python 2.X or python3 for 3.x and you will be greeted by the python prompt and I tell you python prompt in Linux is as good as IDLE for windows.

What should I do with the python Interpreter?
Now that we are armed with the knowledge to invoke the python interpreter, what the interpreter is used for? Well the first thing that we can do what computers do best, yes number crunching but before we do calculation we should know some programming terms.
Expressions - In programming an expression is an way of defining things to the computer so that it understands what we are commanding it to do. Some examples of expressions are when we want to do addition of two numbers say “2+2”, this way of adding “2” with “2” is what we call as an expression.
Operators - We can call operators as the agent which enable us to make an expression. In the above example of an expression “2+2” the “+” symbol is an operator. Some examples of expressions are -
  1. + # Addition
  2. - # Substraction
  3. * # Multiplication
  4. /  # Division
Now enough talk let’s get to action. Fire up the interpreter as mentioned in this section and follow the below examples-
  1. >>> 2+2
  2. 4
  3. >>> 2 * (5-6)
  4. -2
  5. >>> 100 * (20-10)
  6. 1000
  7. >>> 238/5
  8. 47.6
  9. >>> 3444 * (456/43)
  10. 36522.41860465116
  11. >>> 2**2                       # using ** tells python to calculate the power 4
Now look at this ↓
Suppose there is a party for 4 people and you want to order beer cans for the guests , each beer can I $21 ( I don’t drink beer so cut me some slack on the price) and u want to calculate total price of your purchase and since you are a geek we don't use normal calculators or smartphones like mere mortals instead we command python code to do something like this-
  1. >>> beer_price = 21
  2. >>> guests = 4
  3. >>> total_price = beer_price * guests
  4. >>> total_price
  5. 84

Okay , What did you do just now?
Let’s break that into more granular level. When we type “beer_price = 21” we are assigning the value 21 to “beer_price” and here “beer_price “ is known as a variable.
A variable can be thought of as a container which has an item i.e. a number “21”. Let’s have another example to make it easy to understand.

  1. >>> android = "KitKat"  # a variable “android” which contains the name “KitKat”
  2. >>> android  # checking what is contained in the variable android
  3. 'KitKat'
  4. >>> android = "Lollipop"  # now we give another name to variable
  5. >>> android  # checking what is contained in the variable android  
  6. 'Lollypop'   # the content of the variable Is now changed
Initially the variable “android” contained the value “KitKat” but after that we assigned a different value to it and now the variable contains the value as “Lollypop” . In short “android” will contain the most recent value that we assign to it and that’s why it’s known as a variable since the information that it contains can vary again and again.

Now that we have learned about expressions so we shall gather up your jedi skills as we have to pass the following test which will test our worthiness to continue our journey further...
  1. Launch the python interpreter.
  2. Type and expression in the interpreter which will give the sum of the two numbers “200” & “300”.
  3. Use an expression to calculate the difference of “549” and “679” and multiply this difference with the sum of “999” & “790”.
  4. Use an expression to divide “9999” with “0” and assign the result to a variable.

....To Be Continued...

Subscribe to my youtube channel to watch new python programming tips and tutorials.

If you have any questions, suggestions or comments you can post your comment here or you can also bug me on Quora, Twitter or on Facebook.