Beginning with a python program: The tail



The beginning 

This blog is thought for the people who want to start to learn python programming . We start with the tail because a python can swallow you if you go untrained towards the tongue. Once we will be familiar with the python, I will discuss advanced computational libraries like Numpy, Scipy, Pandas, Glob  and hybrid coding by mixing other programming languages with python.

Why Python?

There are many programming languages which one can use. Python is good because it is simple. It uses  simple syntax compared to many other programming languages. The code is short and can be understood easily. There are though questions, if python is good for high-performance computing (the tongue)? There are ways to make python efficient even for those cases. Anyway here we do not want to talk about efficiency as we are learning simple programming (the tail). In few words, python is good because:
  1. It is easy to learn.
  2. Syntax is simple.
  3. It is flexible in use
  4. It is free.
  5. It is used by a wide range of communities e.g. physicists, mathematicians, all other scientists,  hackers and list goes on...
  6. It has a wide range of libraries and resources freely available. 
  7. You can use python for planning your appointment calendar, statistical data analysis, plotting figures, high performance computing to solve difficult mathematical problems etc.
  8. If you still need more reasons to learn python... just google and you will find many more.

The logic

The main thing is to understand logic behind the program you are going to write. When you have an idea in brain, you can express it with some language you speak. But if you don't know what to speak then the language is of no use. In the same way you need to know the algorithm of your program before you can start programming. If you understand what you are doing then choose a language  e.g. python, fortran, c, c++ etc. In our case we choose Python to be this language. So let us try to understand an algorithm.

The first Algorithm

Before we can start learning python, we need to know how to begin. To begin we need to know the operating system. The first algorithm is to find out how to begin with Python.

Switch on your computer
Check your operating system
IF operating system is WINDOWS:
    Install LINUX
    If you don't want to install linux :
        Start some command-line environment and python or install anaconda for the best use.
        If you have done that:
            start commandline/anaconda to test if python is installed 
            #there is a nice tutorial here how to use anaconda to run python on windows
ELSE IF operating system is LINUX/UNIX  :
    Start Konsole/Terminal
    type python in terminal to test if python is installed 
    There are multiple ways to install python on linux machines
    #A tutorial on how to install python on linux or anaconda can be installed on linux as well
If you are able to run python command in command-line you are ready to write your first program. 

This was an example of logic. It might seem idiotic to write switch on your computer but then we should know that each detail is very important in programming. 

The very first program

Let us start with writing a python program. Before we can start to write a program we need to know, what we want to do? Let us write a very simple program.
Start python by loading anaconda/terminal/command-line environment. Going historically let us write a hello world program. This program just say hello to the world.

Type python in terminal /command-line environment

user@machine:~$ python 
Python 2.7.10 (default, Oct 14 2015, 16:09:02)
[GCC 5.2.1 20151010] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> Hello ="Hello Python world, I just joined you."
>>> print Hello  
>>> Hello Python world, I just joined you.

In this example we define, Hello ="Hello Python world, I just joined you." and asked python to print Hello. Python answered with Hello Python world, I just joined you

We can do the same thing in another way: Open a text editor and copy paste the following code to the text editor. Here inverted commas (" some string") are important so that python can understand that it is a string. We will talk about how python differentiates between text, numbers (types) etc in the next post. 


Hello ="Hello Python world, I just joined you."  
print Hello 
# using hashtag means that this line is commented out
# Let us write multiple things in the program
Name = "Your Name"
print Name

Paste this code in a text file and save it as hello.py ! Now go to the directory where you saved it and run following command:
python hello.py 

user@machine:~$ python hello.py  
Hello Python world, I just joined you.
Your Name

So you see that you can print what you wanted. 





    
        

Comments

Popular Posts