Lec 5
Lec 5
Lecture – 05
Tutorial 1 Part 2 Python
Hi everyone. In the last video we learnt how to install Linux on our systems. We installed
Ubuntu 14.04 desktop edition in a virtual environment. So, now in this video, we will
start off from where we left in the last part.
In this video, we will learn how to use the terminal, and see how to run python on
Ubuntu. So, after you have completed the installation process of Ubuntu, and restarted
your system, you will reach this login screen. This login screen will have a user account
that you created, and a guest session. So, just put in the password that you created at the
time of installing Ubuntu, and press enter to login. Now, since it is the first time that you
are logging into your new virtual machine, it will take a few seconds before the desktop
loads. So, this is how the Ubuntu desktop looks like.
(Refer Slide Time: 01:06)
You can see a bunch of icons in the left hand side column. They are some applications
that are preinstalled in Ubuntu. So, first, we will see how to reach the terminal. Take the
mouse pointer to the left top, the first icon in the bunch of icons in the left column, and
click on the first icon.
Now, if you see such replies, you know that, the internet is working. Press control c to
stop pinging. If you see some other error messages, like request timed out, or destination
host unreachable, that means that, the internet is not working on this virtual machine.
Please refer to the previous video, to check the settings of this virtual machine, and make
sure that, you have configured the virtual machine in a way that the internet works. Now,
an important thing to do with a fresh installation of Ubuntu is to update the list of
packages that are available for installation on this operating system.
This can be done by running the sudo apt get update command on the terminal. Now, this
is a system level operation; so, only a super user, or a user with administrative rights, can
execute this command. This is why we add sudo before executing most apt get
commands. Sudo is short for super user do. Now, it will ask you for your account
password. So, apt get is a command line package management tool for Ubuntu, which is
used to install, remove, and manage software packages.
When you type update with apt get, it instructs this package manager to download a list
of all the latest software packages, and their latest available versions, from the internet.
You can see a list of URLs here on the screen, from where the apt get is downloading the
latest list of packages. These URLs are online software repositories, which maintain the
list of latest software packages available for various versions of Ubuntu. Now, we do not
need to go into more details for the purpose of this course; once the apt get update
operation is complete, let us get started with python. Now, python comes preinstalled
with most versions of Ubuntu.
You can enter the python shell by simply typing in python in the terminal, and pressing
enter. So, we can see here that, python version 2.7.6 is installed. So, before we start
working with python, let us install a package manager for python, so that, we can install
and manage new python packages easily.
(Refer Slide Time: 04:10)
Exit the python shell, and type ‘sudo apt get install python hyphen pip’. Now, this
command instructs the apt get package manager to download and install the package
name python hyphen pip.
And, this is one of the prime reasons that we are using Ubuntu. You do not have to worry
about any version mismatches, or any unmet dependencies, or any missing modules,
etcetera. It also tells you the amount of additional disk space that will be used, after the
installation is complete. Now, press y and enter to continue.
Now, just like apt get is used to install and manage packages for Ubuntu, pip is used to
install and manage packages for python. So, just like we used apt get install to install
packages for Ubuntu, we can use pip install to install packages for python.
(Refer Slide Time: 05:27)
Now, let us try to install a package using pip. Now, similar to apt get, we type sudo pip
install package name; let us say, requests. Now, a request is http library for python. We
will use this package to send get requests to some of the social network APIs in the next
tutorial. So, it looks like, it is already installed here. So, let us just go to the python shell,
and try out this library. Type python in the terminal and press enter. Now, to use any
library, we will have to import it first. So, we write import requests, and press enter. This
is very similar to the hash include statements in C or C++ programming.
Now, let us start by a sending a get request to Google dot com. This is essentially the
same thing that your internet browser does, when you open Google, or any other website,
in your browser. It sends a get request to the website, receives a response, and displays it.
Now, we are doing the same thing in python. So, we write response equal to requests dot
get Google dot com, and press enter. Now, you should type response and press enter; you
see this response object with the code 200. Now, 200 is the http code for an OK
response. It means that, we were successfully able to send the request and get a response.
(Refer Slide Time: 06:50)
Now, to see the text part of this response, we type response dot text, and press enter. This
is the HTML source code of google.com. You get the same response when you open
Google in your browser; but, the browser is capable of understanding this HTML code,
and showing it as a pretty looking web page that you see, OK.
To execute the python script, just write python space hello dot py, and press enter, and
there you have it; you have the first hello world program in python. Now, this is all for
this tutorial. This tutorial was just a getting started kit for Ubuntu and python.
In the next tutorial, we learn how to collect data from Facebook, using the graph API. If
you have any questions, or comments, please post them on the NPTEL discussion forum.
We will be happy to assist you with any doubts or clarification or help.
Thanks.