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.

No comments:

Post a Comment