Getting Started with Python Language

0
2673
Python Tutorial – Part (1)

Python Tutorial – Part (1)

In this series, we will talk about the Python language. Some people believe that Python represents the P layer of the LAMP model (sometimes called LAMP stack). Whether this is true, or the P is really meant to stand for Perl or PHP, the three languages are very powerful and each of them has its fans.

Why Python?

The first normal question somebody may ask is: why to use Python? What are its benefits?

The first reason for using Python is its Readability. Readability means that Python code is easy to read and understand. This feature facilitates not only the developing process, but also modifying and maintaining the code on need. If you are a beginner programmer, you mayn’t feel the importance of this feature now. By time, when you gain experience, you will see how developers (programmers) work in a team, so it is very important that the different team members could easily understand each others’ codes. You will also know that programs are rarely written in one shot. Some programs take months to build up, so you will need to be able to understand your own code that you may have written several months (or years) ago. Also, we are humans; so nothing is perfect, and there will be some improvements to make. That is why new versions of programs are made. In all cases, the ability to understand previously-written codes (either you wrote or others did) is vital for success.

The second reason is the Portability. A program is said to be portable if it can run unchanged (or with minor changes) on another machines with different hardware and different operating systems. Don’t be astonished when you see that the code we will write on a Linux machine can be copied to a Windows machine and run normally without any problems.

Another great feature is that Python supports Object Oriented Programming (OOP). If you have worked with C++ or Java, you would have had some knowledge about this concept, and how it can be useful when modeling real-life objects into programmable classes and objects.

A unique feature to Python is the ability to run interactively. Having an interactive prompt that waits for the instructions you type to execute for you is a nice feature that enables the programmer to test his code interactively.

Python is completely Free, and its source code is available on the internet for anybody. So friends lets start with this Python tutorial Part 1.

Paving the road

Before learning how to write Python code, we need first to know where to write this code, and how it will be interpreted and executed.

As mentioned above, Python is a portable language, which means it is platform independent. It can run on Windows, various Linux distributions, and on UNIX as well. In this section, I will show you how to install Python on a Microsoft Windows machine, then on a Linux machine.

  • On Windows:
  1. In your web browser, browse to https://www.python.org/, then select the version you want to download.

1Image 1: Downloading the Python software

  1. When the download is complete, double-click the downloaded setup file.

2Image 2: Starting the installation wizard

  1. Click Next
  2. In the Customize Step, click the drop down list “Add python.exe to Path” and select “Entire feature will be installed on local hard drive“, then click Next.

3Image 3: Customzing the Python installation

  1. Click Finish to complete the installation.
  2. To test the python installation, open the command prompt and type python.

4Image 4: Testing Python installation success in Windows,

by starting the interactive shell from the command prompt.

The Python interactive mode is started; first displaying the installed version (3.4.3), then gives a prompt that waits for the user’s input.

  • On Linux Systems:

Many Linux distributions have Python installed by default when they are installed (even with minimal options).

  1. Check whether python is installed on your Linux box or not:

  1. If Python is not found on the machine, we can install it:

yum install python

  1. Verify installation: in the terminal type the command python

5Image 5: Testing Python installation success in Linux

Get Bored?

I know you are very excited to start writing Python code, so that you may get bored with this long theoretical introduction. In fact, it isn’t long at all. I didn’t even mention all the features of Python as a High-Level, interpreted programming language. Anyway, don’t worry, we will start soon, and you will have the chance to practice and write Python code. Just few articles ahead and you will be able to write your own Python programs.

Kick-off

Now, let’s start our journey to learn Python. Good Luck!!

Our First Program

In the beginning, we will write and execute our Python programs in the interactive command line. To start the Python interactive mode shell, there are several ways:

Windows:

  • From the start menu, locate and expand the Python menu, then click either IDLE (Python GUI), or Python Command line.

6Image 6: The Python submenu in the Windows Start Menu

This will start the interactive shell.

7Image 7: Python Interactive Shell

  • Another way is to open the command prompt, and type the command python

8Image 8: Python Interactive Shell started from the command prompt.

Linux:

Open a terminal, and then execute the command python

9Image 9: Starting the Python Interactive Shell in Linux

The Interactive Shell is open, what is next?

Now, we have the interactive shell open. As its name implies: it is a shell that waits for input from the programmer, and interactive because it parses (interprets) the user’s input and execute it promptly. An interesting feature of the Python interactive shell is that it can perform calculations for you (acting as a basic calculator).

10Image 10: Get your hands used to the Python interactive shell,

by entering simple mathematical and logical operations.

Those of you who had studied Engineering or Mathematical Science may remember the MATLAB; it has a similar interactive shell that performs much more sophisticated mathematical and engineering computations. (God bless these old days).

– Wait, we didn’t write any code till now!!

Actually, we did. Don’t under-estimate these simple operations you entered on the shell above. Soon, you will know (and appreciate) their importance when we come to logic and decision making. But, to get you satisfied, type this simple program:

11Image 11: Your First Python Program

Ah, it works!!! And you have just written your first Python code. Congratulations!!

Hope you have enjoyed this Python tutorial first part, See you in Python Tutorial Part 2.

Don’t go anywhere.

LEAVE A REPLY

Please enter your comment!
Please enter your name here