Introduction to Python Programming/Python Programming - Installing Python

Chapter 2: Installing Python

edit

Here in this chapter, I will tell you about installing Python on your local machine. There are two versions of Python available from the main Python website namely the Python 2.7.XXXX and Python 3.0.XXX. I prefer to use, teach Python 2.7.XXX versions. In this book, I will be talking, and writing about Python 2.7.XXX. What’s the difference, one may questions. There are differences between the two versions with syntax and a few other minor changes.

Coming to the question, which Python is right for you?

Python like any other open source software is a constantly evolving where the users from all over the world are contributing, making and updating features. There is a board of members that vote on a particular feature and decide whether the feature needs to be integrated or not. The documentation that acts as the supporting is constantly evolving at the same time. So decide on which one you want to use depending on your preferences and evaluations.

That said, it’s more than enough that you know one of these either versions, knowing one will make it a breeze to know the other versions. Just keep that in mind, and start talking Python.

After deciding which version of Python to learn, the first thing you need to do with Python is install it.

2.1. Installing version of Python

edit

After you have decided which version of Python to learn, you need to install it. Linux and Mac OSX machines come with preinstalled Python packages so you need not worry about installing. Run the command terminal and type in the keyword python, and the machine will tell you if you have Python installed or not. Go figure that, Good luck with that!

Since I have only a Windows machine, I will guide you through installing Python on a Windows computer. Go figure

As a matter of fact, Windows does not come with Python pre installed. Go ahead and download the version of Python software that you want to use from the Python official website.

Installing Python from Python dot org

  1. Download the latest Python Windows installer by going to http://www.python.org/ftp/python/ and selecting the highest version number listed, then downloading the .exe installer.
  2. Double-click the installer, Python-2.xxx.yyy.exe. The name will depend on the version of Python available when you read this.
  3. Make sure that you have the administrative rights on the machine that you are working on.
  4. Step through the installer program.
  5. Some of the features can be removed from the installation in case you do not have enough space on your hard disk.
  6. Python will be installed with all the proper files and extensions.
  7. After the installation is complete, close the installer and select Start->Programs->Python 2.7->IDLE (Python GUI). You'll see something like the following, which is a sample shot from my machine:
   Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec 5 2015, 20:32:19) [MSC v.1500 32 bit (Intel)] on win32
   Type "copyright", "credits" or "license ()" for more information.
   >>> 

I am assuming that you have the standard Python interpreter on the machine that you are currently planning to work on with.

2.2. The Interactive Shell

edit

Once you have Python installed, there are different ways to run Python programs. On the shell interpreter, you can run commands and test how the flow of code goes. You can save the code that you have written and then run the file to obtain the output for the code that you have written.

Here, I will explain how to run simple code snippets on the Python interpreter.

You can fire up the Python shell interpreter in any of the following ways.

2.2.1. Method 1:

edit

Run the command interpreter by pressing the windows and the letter R. You will see a window open up like this.

Type in the letters cmd in the text box and you will see a screen like the one shown below in Figure 2.

Once at the command prompt, change into the directory where the Python executable resides. For example, on my local machine, I have the Python executable residing on C:\Python27\. So, I will go ahead and change into that particular folder by typing “cd C:\Python27” on the command prompt. Here’s what I see on my local machine. Once inside the folder Python27, type in python at the command prompt and voila! You’ll see the Python shell interpreter as illustrated in Figure 3.

2.2.2. Method 2:

edit

Now there is a second way on firing the Python shell interpreter and the following will explain how to do the same. Click on Start-> All Programs->Python2.7->Python IDLE and you’ll see the following shell interpreter as shown in Figure. 4.

Feel comfortable with these methods of firing up Python shell interpreter, I will talk about different other methods of firing the shell interpreter as and when the situation rises for the occasion. Here IDE stands for Integrated Development Editor, this is the Graphical User Interface (GUI) of the Python programming paradigm.

Apparently, there are users who use the Python shell interpreter like a calculator, but I still stick my own calculator on my local Windows machine.

Just type in the following command in the Python shell interpreter and you will see the following result. For sake of understanding, the input commands to the shell interpreter are preceded with >>>, while the output from the shell interpreter is in “Burnt Sienna” color. 
   >>> print "Hello World!!!!"
   Hello World!!!!
   >>>

There is one important operator that anyone needs to know which is the “=” operator also called the assignment operator. Variables can be assigned values using the assignment operator and Python will dynamically typecast the variable based on the value assigned to the variable.

Launch the Python interactive shell in whatever way works on your platform, and let's dive in with the steps shown here:

Example 1. First Steps in the Interactive Shell

   >>> print "Hello World, my first Python Program"
   Hello World, my first Python Program
   >>> 6+7
   13 
   >>> x = 4               
   >>> y = 6
   >>> x + y
   10

Welcome to the Python party! Lets Python like we have never Python”ed before.

2.3. Kick starting with Python

edit

There is another way to achieve printing “Hello World” like shown in the earlier sections. Store the value “Hello World” in a variable and then print the variable. The output of the print statement are followed by the outputs that they provide just below them

   >>> s="Hello World!"
   >>> s
   'Hello World!'
   >>> print (s)
   Hello World!
   >>> print s
   Hello World!

To write multiple lines, just add ‘\n’ character where you want to introduce the new line. Just follow the following example.

   >>> print "This is the first line\nAnd this is the second line"
   This is the first line
   And this is the second line
   >>> mystring="This is the first line\nAnd this is the second line"
   >>> mystring
   'This is the first line\nAnd this is the second line'
   >>> print mystring
   This is the first line
   And this is the second line

2.3.1. Printing variables

edit

Variables can be printed in their forms and also the types they are cast into can also be printed using the print command. To print variables,

   >>> x=3
   >>> x
   3
   >>> print x
   3
   >>> print (x)
   3
   >>> print (str(x))
   3
   >>> print type(x)
   <type 'int'>

The above print statements focused on printing a single variable. However, a combination of different variables can be printed using a same single print command.

   >>> x=5
   >>> y=7
   >>> print x
   5
   >>> print y
   7
   >>> print x,y
   5 7
   >>> print (x,y)
   (5, 7)

Before printing integer variables, they have to be converted to string type using the str when printing along with string variables.

   >>> print str(x) + str(y)
   57
   >>> print str(x) + " " + str(y)
   5 7
   >>> print x+7
   12
   >>> print x+y
   12
   >>> print (x+y)
   12
   >>> print "the sum of %d and %d is %d" %(x,y,x+y)
   the sum of 5 and 7 is 12

2.3.2. Getting text inputs

edit

Obtaining input from the interpreter into the shell is done using the input command available. The syntax and the following statements highlight the use of input commands. Strings inputs have to be enclosed within quotes. Errors are highlighted in red.

   >>> input("what is the number")
   what is the number3
   3
   >>> input("whats my name!:")
   whats my name!:Some
   Traceback (most recent call last):
     File "<pyshell#52>", line 1, in <module>
       input("whats my name!:")
     File "<string>", line 1, in <module>
   NameError: name 'Some' is not defined
   >>> input("whats my name!:")
   whats my name!:"New Name"
   'New Name'
   >>> name=input("whats my name!:")
   whats my name!:"New Name"
   >>> print name
   New Name
   >>> print (name)
   New Name
   >>> print "My name is " + name
   My name is New Name
   Python typecasts the variable based on the input you provide the Python interpreter. 
   >>> print "My name is " + name
   My name is New Name
   >>> age=input("whats my name!:")
   whats my name!:34
   >>> print age
   34
   >>> print type(name)
   <type 'str'>
   >>> print type(age)
   <type 'int'>
   >>> print type(name), type(age)
   <type 'str'> <type 'int'>
   >>> print "The data type of "+ name +" and "+str(age)+"is "+ str(type(name)) + "and " + str(type(age))
   The data type of New Name and 34is <type 'str'>and <type 'int'>

However for ease of use, you can specifically instruct the Python interpreter to typecast the input into the type of variable you want to use the variable in. if you don’t specifically provide the typecast, Python interpreter will do the job of typecasting the variable.

   >>> x=int(input("enter a integer number"))
   enter a integer number3
   >>> x
   3
   >>> x=float(input("enter a integer number"))
   enter a integer number4
   >>> print x
   4.0
   >>> x
   4.0
   >>> x=str(input("enter a integer number"))
   enter a integer number4
   >>> x
   '4'
   >>> x=input("enter a integer number")
   enter a integer number5
   >>> x
   5
   >>> print type(x)
   <type 'int'>

Now that we have a fair idea of the print and the input commands, let’s write a simple Python program that will ask your name, age, location, and your job details and prints the same. On the Python interpreter, I opened a new file and wrote the following piece of code and saved it as file.py.

   name=input("Enter your name: ")
   age=input("Enter a two digit number as age: ")
   location=input("Enter your current location: ")
   job=input("Enter your job details: ")
   print "Hi There!!! \nMy name is  %s,\nMy age is %d,\nI am located at %s and currently working as %s" %(name,age,location,job)

Further, I ran the program on the Python shell and obtained my first output for the same.

   >>>  RESTART:C:/Users/Desktop/nameagelocation.py 
   Enter your name: "Suzaane"
   Enter a two digit number as age: 37
   Enter your current location: "Buffalo"
   Enter your job details: "Research Programmer"
   Hi There!!! 
   My name is  Suzaane,
   My age is 37,
   I am located at Buffalo and currently working as Research Programmer
   >>>

There you go, you just wrote your first Python program and ran it on the Python interpreter.