Unit I
Unit I
Welcome to:
Introduction to Python
9.1
Unit objectives IBM ICE (Innovation Centre for Education)
IBM Power Systems
Introduction
• building video games ,popular in IoT, Data Science ,Big Data,AI and so on….
Source https://stackoverflow.blog/
Strength of Python IBM ICE (Innovation Centre for Education)
IBM Power Systems
Source https://stackoverflow.blog
Python use cases IBM ICE (Innovation Centre for Education)
IBM Power Systems
• Python is a scripting language like PHP, Perl, Ruby
• web programming (django, Zope, Google App Engine..)
• desktop applications (Blender 3D)
• Even for games (Pygames)
• Applications:Health,Finanace,Mathematical&Scientific,Vision,Database,Data science,Machine Learning
Flavors of Python
IBM ICE (Innovation Centre for Education)
IBM Power Systems
Python 2 vs Python 3 IBM ICE (Innovation Centre for Education)
IBM Power Systems
When would one choose 2.7 ? IBM ICE (Innovation Centre for Education)
IBM Power Systems
• Many organisations who store Python Installers for releases in their repo’s have not adopted
to v3.6 so this may be a bottleneck for developers who want to explore the new features and
libraries in v3.6
.
• The user would choose earlier version if they are going to deploy apps on an environment
that they do not have control over - and since assuming the remote deployment location still
has 2.7 - then it is understandable to use 2.7.
• If the third party releases for the product does not support 3.6 - then it is apt to use the
earlier version.
Introduction to Python IBM ICE (Innovation Centre for Education)
IBM Power Systems
• Python is Open Source and is GPL compatible
• Python as it provides a web development framework for faster development and deployments
Introduction to Python IBM ICE (Innovation Centre for Education)
IBM Power Systems
Installation of Python IBM ICE (Innovation Centre for Education)
IBM Power Systems
Introduction to Python Versions of Python
Installation IBM ICE (Innovation Centre for Education)
IBM Power Systems
Introduction to Python Versions of Python
Installation IBM ICE (Innovation Centre for Education)
IBM Power Systems
• Before you install, check if python is already installed ! ( command -v python3 on MacOS, or
type in python3 at the command prompt.)
• Python comes preinstalled on CentOS, RHEL lately and hence not required to be installed. If it is
not installed then follow the instructions in the download link provided.
• Here, you will install the python 3.6 on a Linux/UNIX server. Since there are variants to the UNIX
flavours - only DEBIAN is described here in this slide
Installation IBM ICE (Innovation Centre for Education)
IBM Power Systems
• User would use the same Download URL, and choose the v2.7 and install it. Both versions
can co-exist.
Introduction to Python Versions of Python
Installation IBM ICE (Innovation Centre for Education)
IBM Power Systems
• One has the option of installing Python in different ways, It can be either in gzip form,
Installer, or a web based installer
• Installing the gzip or source code and compiling is only if you want to install it locally.
• However the easier way is to install using the Windows Installer.
• Scroll down to the section in this link/URL where the “Files” are listed, and you will see
different flavours of installers, and for 32 bit and 64 bit processors as well.
• Choose the appropriate package for your platform - download and install.
Testing and Validating Installation IBM ICE (Innovation Centre for Education)
IBM Power Systems
• Validating the installation of both Python and pip.
• pip3 - the tool to support in installation of additional libraries for Python gets automatically installed when
user installs the v3.6
• To check if this is installed - open a command prompt and type in the command pip3 ( the related
command to check in version 2.7 is pip )
• pip3 —V ( double dash )
• This indicates the version of pip installed and at the end it will say the matching python version that this
is compatible with.
• Additional information on installing pip can be obtained from
https://pip.pypa.io/en/stable/installing/
altgraph (0.10.2)
wscli (1.14.28)
bdist-mpkg (0.5.0)
bonjour-py (0.3)
botocore (1.8.32)
certifi (2018.1.18)
chardet (3.0.4)
click (6.7)
cloudant (2.3.1)
• next, explore the pip command by typing in
• pip3 —-help
PIP3 LIST 1 of 2 IBM ICE (Innovation Centre for Education)
IBM Power Systems
• Also, explore
• pip show command to list the package and it’s attributes
pip3 show python ( This is only a sample, Your output can vary )
• Name: Python
• Version: 2.7.10
• Summary: A high-level object-oriented programming language
• Home-page: http://www.python.org/2.7
• Author: Guido van Rossum and the Python community
• Author-email: [email protected]
• License: PSF license
• Location: /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-
dynload
• Requires:
Installation IBM ICE (Innovation Centre for Education)
IBM Power Systems
• Let’s just install a sample package called Cloudant ( this is a NoSQL DB)
$ pip3 install cloudant
• Collecting cloudant
• Downloading cloudant-2.8.1.tar.gz (54kB)
• 100% |████████████████████████████████| 61kB 205kB/s
• Collecting requests<3.0.0,>=2.7.0 (from cloudant)
• Using cached requests-2.18.4-py2.py3-none-any.whl
• Collecting certifi>=2017.4.17 (from requests<3.0.0,>=2.7.0->cloudant)
• Using cached certifi-2018.1.18-py2.py3-none-any.whl
• Collecting chardet<3.1.0,>=3.0.2 (from requests<3.0.0,>=2.7.0->cloudant)
• Using cached chardet-3.0.4-py2.py3-none-any.whl
• Collecting urllib3<1.23,>=1.21.1 (from requests<3.0.0,>=2.7.0->cloudant)
• Using cached urllib3-1.22-py2.py3-none-any.whl
• Collecting idna<2.7,>=2.5 (from requests<3.0.0,>=2.7.0->cloudant)
• Using cached idna-2.6-py2.py3-none-any.whl
• Installing collected packages: certifi, chardet, urllib3, idna, requests, cloudant
• Running setup.py install for cloudant ... done
• Successfully installed certifi-2018.1.18 chardet-3.0.4 cloudant-2.8.1 idna-2.6 requests-2.18.4 urllib3-1.22
• Now, run the pip3 list command and validate that the Cloudant is installed.
Checkpoint (1 of 2) IBM ICE (Innovation Centre for Education)
IBM Power Systems
1 MARK QUESTION
1.Is it a good practice to write python programs in file names that ends with a .py ?
– Yes
– No.
2.‘pip’ and ‘setuptools.py’ are supporting 3rd party tools that aid in installing Python modules and other
libraries.
– Yes
– No
3.Basic Programs written for version 2.7 can be run on version 3.6 as well in general ?
– Yes
– No
4.python —version command will indicate the version of the Python executable
– Yes
– No
5.‘python’ and ‘python3’ are the executable names for python and python3 programming Language
– Yes
– No
6. Python is an interpreter and does require code to be compiled before executing.
– True
– False
Checkpoint solutions (1 of 2) IBM ICE (Innovation Centre for Education)
IBM Power Systems
• 1. Yes - all programs need a suffix and in the case of Python programs - they end with .py. It
helps in identifying that they are Python scripts.
• 2. Yes, pip and setuptools.py are 3rd party tools that aid in installation of libraries and
modules. They are installed as part of the v3.6 by default.
• 3. Yes, Generally, all code written for v2.7 will and should work for v3.6.
• 4. Yes, python —version will print the v2.7 and python3 —version will print the v3.6
• 5. python is the name of the v2.7 executable and python3 is the name of the v3.6 executable.
• 6. False, Python is an interpreter and does not need to be compiled for it to be run.
Checkpoint (2 of 2) IBM ICE (Innovation Centre for Education)
IBM Power Systems
7.Data Types in Python are similar to Fortran Programming Language
– True
– False
8. Python syntax is indent based, and English like
– True
– False
9. Invoking the python interpreter command line can be terminated by
– Ctrl-D
– Ctrl-C
10. Python code can be executed by mentioning the location of the executable in first line of the script
– True
– False
11. Python lets integration of different components seamlessly into it’s code.
– True
– False
12. Python is fast in execution as it is a interpreter, since compilation is not done.
– True
– False
• 9. Ctrl-D
• 11. True
• 12. False, Python tends to be slow in execution compared to compiled languages like Java
and others as it is an Interpreter.
• 13. quit() - this is the method to get out of the Interactive command interpreter.
Question bank IBM ICE (Innovation Centre for Education)
IBM Power Systems
2 MARKS QUESTIONS
• 1. Comments in Python is indicated by a #, or triple quotes, or triple double quotes sign in the beginning
of the line.
– True
– False
• 2. Data Type of ‘x’ is obtained by “ print type(x) “
– True
– False
• 3. Python language programming is not Case-Sensitive
– True
– False
• 4. Multi Line statements are indicated by a ‘*’ at end of the line.
– True
– False
• 5. Multiple statements are allowed in a single line if they are separated by a semicolon ‘;’.
– True
– False
• 6. The “python -v” flag reports the verbose debug output when program is run.
– True
– False
Question bank IBM ICE (Innovation Centre for Education)
IBM Power Systems
• 4 MARKS QUESTION
• 1. Write a program that will read the number of arguments and print the arguments ? (
http://www.pythonforbeginners.com/system/python-sys-argv)
• 6. Import ‘os’ package in your sample program, and print the os.name.
import os
print os.name
Question bank IBM ICE (Innovation Centre for Education)
IBM Power Systems
• 8 MARKS QUESTION
• 1. Write a program that will import os, and print os.getcwd() and os.uname().
Refer to this link for additional help. https://docs.python.org/2/library/os.html
• 3. Write a program that will print the arguments passed to the program. e.g python.
print_args.py. one two three
Hint : use sys.argv method.
• 4.Write a program, that will print today’s date using ‘from datetime import date’ and print
date.today()
• 5.Write a program and include help(), and when executed will print the help menu ( e.g:
help() )
• 6. Write a program a) import os, and then b) print dir (os) and check results.
Unit summary IBM ICE (Innovation Centre for Education)
IBM Power Systems