
Hi everyone it has been long time to write a technical post on TOS. I have been learning Python (a high level programming language) these days and decide to write tutorial in the form of posts and i name it as “Python Days“. I will be writing it in clear English so that any one can understand it.
The main purpose of the writing is to improve my programming skills and at the same time contributing my knowledge to others.
Who should read?
Any one can read this, whether you are totally new to python or programming language or if you are planning to just brush up your python skills.
Please Note: If you find any semantic or syntax mistakes please notify it to me by emailing to talkonsomething@gmail.com or just dropping a comment in the same post where you found an error.
Contents:
Part 1
1. Introduction to Python
2. Installing and Running Python
3. Totally Basic
Introduction to Python:
Python is a programming language, but a high level programming language which is much similar to other high level programming languages like JAVA, .NET, RUBY.
So i think you came to know what it is.. well its a high level programming language.
So what is High Level??
If you are new to programming itself then this is for you. High level language is something like you and me can easily understand when we see the code. Technically speaking it uses english like statement so that programming on this kind of language is fast, flexible and also leads to very less errors and even zero.
But why High Level?
Initially, those days’ people used to write in machine level / assembly level languages. Machine level language is the one which only machine can understand. There are only two characters “0″ and “1″. And they say if its “0″ then it is “OFF” if its “1″ then its “ON”.
But writing a machine language is tough, face lot of errors and was too much difficult to understand.
So people started a new intermediate language called Assemble Level language.
This again used a little bit English like words and this was in-turn converted to Machine Level Language by assemblers.
A little example of Assembly level language (adding two numbers)
MOV EAX, A
MOV EBX, B
ADD EAX, EBX
MOV C, EAX
So in the above example we are adding the value in A to the value in B and finally saving into C.
The above syntax is little bit clear for programmers. But still it takes lot of lines to code. We use some registries to store the values temporarily. ie EBX, EAX. These two are registries and I am not going to explain about them, because it’s not necessary.
We come across MOV (i.e. Move) to move the value form A to EAX
And another word ADD (i.e. Addition) to add the values that are stored in the registry.
Final Notes: We move the value from A to a registry and B value to another registry and finally add both the values and save it to C.
It’s done. Even thought it looks easy for adding two numbers, but it’s difficult to program something powerful. It takes lot of line to code; we need to think about the memory and lot more.
Now the High Level Language:
High level language uses English like statements. There are compilers which convert high level language to machine level. They are coded in common English and uses simple mathematical notations.
Here is an example of High Level Language to add two numbers:
c = a + b
So it’s very easy as you see. Adds a and b, saves the result to c. High level language reduced the lines of code. And any normal person by reading it can understand it.
So hope you understood the Level of Languages. Now back to Introduction to Python.
Python is a high level language. It’s a cross platform support i.e. works on Windows, MAC, Linux and it’s even been ported to Java and .net Virtual Machines.
Its a open source community driven language, and most importantly it is Object Oriented, automatically manages memory (where as in Assembly level you need to manage memory) unlike Perl even python is well know being used as scripting language.
Python was implemented by Guido van Rossum in mid 1980′s. To know more about it then just jump to Wikipedia.
Where is it Used?
Python is being used in several fields.
Some apps that are being developed using Python are
- Google (Web)
- Bit Torrent (Desktop)
- Reddit.com (Web)
- You Tube (Web)
- Morpheus (Desktop)
- Trac (Web)
- Calibre (Web)
- Battlefield 2 (Game)
- Civilization (Game)
- Freedom force (Game)
Fore more just check this link http://en.wikipedia.org/wiki/List_of_Python_software
2. Installing & Running Python
To install python just go to the download page of the python website. Click here and download it according to your OS. I have downloaded the Windows Installer as i will be using Windows. Currently there is python 3.1.1 and i am using the same version.
Installing is as simple as installing a software. Just click the installer and it will do the rest for you.
Now we have installed. Let us start Python.
Python requires any text editor to write our code and then later we can execute it through DOS (or terminal).
So which editor to choose?
You can choose any kind of editor, a normal notepad, but it is recommended to try syntax highlighting editors like Notepad++. Well I will be using Pythons own Shell called IDLE (it’s a python GUI shell written in python itself). That’s more than enough. And it comes bundled with Python. I came to know IDLE is not found in LINUX, if so you can download it from this link: http://love-python.blogspot.com/2008/03/install-idle-in-linux.html
IDLE is a kind of IDE. Using IDE really helps us to manage programming in later stages more easier.
As soon as you start IDLE you get this:
Python 3.1.1 (r311:74483, Aug 17 2009, 17:02:12) [MSC v.1500 32 bit (Intel)] on win32
Type “copyright”, “credits” or “license()” for more information.
>>>
This actually tells the version of python and its platform. At the last line you can see “>>>” that notifies you to put some input.
3. Totally Basics
Now we are ready to start writing our first basic code. Let us start our first most used example of every programming language “Hello World”
In the IDLE click File > New Window
A New Window opens up. This is where you actually write your code
Now write this code
# Printing Hello World Text print(“Hello World”)
Now save it by clicking File > Save and save it as “helloworld.py”. Please note that you have to save the code in .py extension.
Now Click Run > Run Module or (press F5) to run the code and you can see the out put as
Hello World
A little explanation of what the code actually does.
- Question: What we need to do?
- Answer: We need to display “Hello world”.
Line 1 in the above code shows a comment. Any line which starts with a # is said to be comment. And using comment in any language is very important. It helps us in many ways to identify what the code does and also us for any modifications to be made in the future.
Line 2 is where the actual code starts. We are using the python “print” command, this command is used to display the data (can be on your monitor or printer). As we can see the print command is followed by (‘Hello World’) where the quotation represents the string. We will learn about Strings in the later session. The command tells that it needs to print the characters which are in the Quotes.
Hence forth we are able to output on the screen as Hello World when we run the module.
So we just to learn how to write a simple Hello World programming on python. But this is just a starting point. In this code we learnt a bit about print. But there is much more you will be learning later.
Give a Try:
Now give a try by your own. Write a code to display “I am a fan of Talk on Something”
Printing the same Hello World in separate lines:
In the above Hello World program when we execute it as
Hello World
But now we need to get the output of each word in separate line.
Hello
World
To do this we use escape sequence
Here is the modified code
# Printing Hello World Text in Separate lines print(“Hello\n World”)
And this result will be:
Hello
World
In Line 2 we can see we have used an escape sequence “\n” between Hello and World. \n helps us to create New Line.
Here are several other escape sequences you need to get to know
| Escape Sequence | Meaning |
| \n | Create New Line |
| \t | Horizontal Tab, moves the pointer to the next tab stop |
| \r | Carriage Return |
| \a | Alert (alarm) |
| \b | Backspace |
| \’ | Print a Single Quote |
| \” | Print a Double Quote |
| \\ | Print a Backslash |
Give a Try:
Now give a try by your own using the above escape sequences and get to know what outputs you get.
This is the End of Part 1 of Python Days, but this is not an end we are just beginning it.
In the next part you will be invited to learn Data Structures. We will be learning about data types, and more.
Hope you like this tutorial. Please comment on this post.
Part 2 will be posted very soon. Subscribe to our feeds or email newsletter for notification.
Click here for Part 2 of Python Days
Good to see you back dude. I will follow you regularly to learnpython. Anyways I learnt php from you.