Top Banner
INF1204-FUNDAMENTALS OF PROGRAMMING II - 15 Credits - 15 weeks - 4 hours a week
111

INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Dec 23, 2015

Download

Documents

Amber Fields
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

INF1204-FUNDAMENTALS OF PROGRAMMING II

- 15 Credits - 15 weeks- 4 hours a week

Page 2: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Course Description

• This course is an introduction to computer programming using the Python programming language.

• This course covers basic procedural techniques such as variables, data types, selection, iteration, and functions.

Page 3: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Learning Outcomes

• write computer programs using the Python programming language

• manipulate different types of data types found in Python

• write and understand basic computer algorithms • mentally execute and trace the execution of

computer programs • find and correct simple computer program bugs • create functions and use them in computer programs

Page 4: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Introduction • Python is a high-level general purpose programming language.• Because code is automatically compiled to byte code and executed,

Python is suitable for use as a scripting language, Web application implementation language, etc.

• Because Python can be extended in C and C++, Python can provide the speed needed for even compute intensive tasks.

• Because of its strong structuring constructs (nested code blocks, functions, classes, modules, and packages) and its consistent use of objects and objectoriented programming, Python enables us to write clear, logical applications for small and large tasks.

Page 5: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Important Features of Python• Builti n high level data types: strings, lists, dictionaries, etc.

The usual control structures: if, if else, if elif else, while, plus a powerful collection iterator (for).

• Multiple levels of organizational structure: functions, classes, modules, and packages. These assist in organizing code. An excellent and large example is the Python standard library.

• Compile on the fly to byte code Source code is compiled to byte code without a separate compile step. Source code modules can also be "pre compiled" to byte code files.

• Object oriented Python provides a consistent way to use objects: everything is an object. And, in Python it is easy to implement new object types (called classes in object oriented programming).

• Extensions in C and C++ Extension modules and extension types can be written by hand. There are also tools that help with this, for example, SWIG, sip, Pyrex.

• Jython is a version of Python that "plays well with" Java.

Page 6: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Some things you will need to know:

• Python uses indentation to show block structure. Indent one level to show the beginning of a block. Out dent one level to show the end of a block. As an example, the following C style code:

• if (x) { • if (y)• { • f1()• }• f2() • } • in Python would be: • if x:• if y: • f1()• f2() And, the convention is to use four spaces (and no hard tabs) for each level of indentation.

Actually, it's more than a convention; it's practically a requirement. Following that "convention" will make it so much easier to merge your Python code with code from other sources.

Page 7: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Week 2-3: MODULES and PACKAGES

o MODULESo What is a module in Python?o You can use a module to organize a number of

Python definitions in a single file. o A

definition can be a function, a class, or a variable containing any Python object.

o Here is an example: python_101_module_simple.py

Page 8: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Modules

• Comments: The string definitions at the beginning of each of the module, class definitions, and function definitions serve as documentation for these items. You can show this documentation with the following from the command line:

• $ pydoc python_101_module_simple

Page 9: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Modules

• Or this, from the Python interactive prompt:• >>> import python_101_module_simple

>>> help(python_101_module_simple) • It is common and it is a good practice to includ

e a test harness for the module at the end of the source file. Note that the test:

• if __name__ == '__main__':

Page 10: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Modules

• will be true only when the file is run (e.g. from the command line with something like:

• "$ python python_101_module_simple.py• but not when the module is imported. • Remember that the code in a module is only -

evaluated the first time it is imported in a pr-ogram.

Page 11: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Modules

• So, for example, change the value of a global variable in a module might cause behavior that users of the module might not expect.

• Constants, on the other hand, are safe. A constant, in Python, is a variable whose value is initialized but not changed. An example is LABEL, above.

Page 12: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Packages

• A package is a way to organize a number of modules together as a unit.

• Python packages can also contain other packages.

• To give us an example to talk about, consider the follow package structure:

Page 13: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Packages

• package_example/ package_example/__init__.py package_example/module1.py package_example/module2.py package_example/A.py package_example/B.py

Page 14: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Packages

• And, here are the contents: • __init__.py:• # __init__.py# Expose definitions from modules in this package. from module1 import class1 from module2 import class2

Page 15: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Packages

• module1.py:#module1.py:class class1: def __init__(self): self.description = 'class #1‘ def show(self): print self.description

Page 16: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Packages

• module2.py:# module2.pyclass class2: def __init__(self): self.description = 'class #2‘ def show(self): print self.description

Page 17: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Packages

• A.py:# A.pyimport B• B.py:# B.pydef function_b(): print 'Hello from function_b'

Page 18: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Packages

• In order to be used as a Python package (e.g. so that modules can be imported from it) a directory must contain a file whose name is __init__.py.

• The code in this module is evaluated the first time a module is imported from the package.

• In order to import modules from a package, you may either add the package directory to sys.path or, if the parent directory is on sys.path, use dot notation to explicitly specify the path.

• In our example, you might use: "import package_example.module1".

Page 19: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Packages

• A module in a package can import another module from the same package directly without using the path to the package. For example, the module A in our sample package package_example can import module B in the same package with "import B". Module A does not need to use "import package_example.B".

Page 20: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Packages

• In the __init__.py file, import and make available objects defined in modules in the package. Our sample package package_example does this. Then, you can use from package_example import * to import the package and its contents. For example:

Page 21: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Packages

• >>> from package_example import *• >>> dir() ['__builtins__', '__doc__', '__file__', '__name__',

'atexit', 'class1', 'class2', 'module1', 'module2', 'readline', 'rlcompleter', 'sl', 'sys']

• >>>• >>> c1 = class1()• >>> c2 = class2()• >>> c1.show()• class #1• >>> c2.show()• class #2

Page 22: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Packages

• With Python 2.3, you can collect the modules in a package into a Zip file by using PyZipFile from the Python standard library. See http://www.python.org/doc/current/lib/pyzipfile objects.html.

Page 23: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Packages

• >>> import zipfile >>> a = zipfile.PyZipFile('mypackage.zip', 'w', zipfile.ZIP_DEFLATED) >>> a.writepy('Examples')

>>> a.close()

Page 24: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Packages

• Then you can import and use this archive by inserting its path in sys.path. In the following example, class_basic_1 is a module within package mypackage:

Page 25: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Packages

• Then you can import and use this archive by inserting its path in sys.path. In the following example, class_basic_1 is a module within package mypackage:

Page 26: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Packages

• >>> import sys >>> sys.path.insert(0, '/w2/Txt/Training/mypackage.zip')

>>> import class_basic_1 Basic name: Apricot >>> obj = class_basic_1.Basic('Wilma') >>> obj.show() Basic name: Wilma

Page 27: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Implementing Python Packages

• In order to be able to import individual modules from a directory, the directory must contain a file named __init__.py.

• (Note that requirement does not apply to directories that are listed in PYTHONPATH.)

• The __init__.py serves several purposes:

Page 28: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Cont

• The presence of the file __init__.py in a directory marks the directory as a Python package, which enables importing modules from the directory.

• The first time an application imports any module from the directory/package, the code in the module __init__ is evaluated.

• If the package itself is imported (as opposed to an individual module within the directory/package), then it is the __init__ that is imported (and evaluated).

Page 29: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Using Packages

• One simple way to enable the user to import and use a package is to instruct the use to import individual modules from the package. A second, slightly more advanced way to enable the user to import the package is to expose those features of the package in the __init__ module. Suppose that module mod1 contains functions fun1a and fun1b and suppose that module mod2 contains functions fun2a and fun2b. Then file __init__.py might contain the following:

Page 30: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

cont

• from mod1 import fun1a, fun1b from mod2 import fun2a, fun2b

• Then, if the following is evaluated in the user's code:

import testpackages

Page 31: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

cont

• Then testpackages will contain fun1a, fun1b, fun2a, and fun2b. For example, here is an interactive session that demostrates importing the package:

• >>> import testpackages >>> print dir(testpackages) [`__builtins__', `__doc__', `__file__', `__name__', `__path__', `fun1a', `fun1b', `fun2a', `fun2b', `mod1', `mod2']

Page 32: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Distributing and Installing Packages

• Distutils (Python Distribution Utilities) has special support for distrubuting and installing packages. Learn more here: Distributing Python Modules http://docs.python.org/distutils/index.html. As our example, imagine that we have a directory containing the following:

Page 33: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

cont

TestpackagesTestpackages/README Testpackages/MANIFEST.in Testpackages/setup.py Testpackages/testpackages/__init__.pyTestpackages/testpackages/mod1.py Testpackages/testpackages/mod2.py

Page 34: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

cont

• Notice the sub directory Testpackages/testpackages containing the file __init__.py. This is the Python package that we will install.

• We'll describe how to configure the above files so that they can be packaged as a single distribution file and so that the Python package they contain can be installed as a package by Distutils.

Page 35: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

cont

• The MANIFEST.in file lists the files that we want included in our distribution. Here is the contents of our MANIFEST.in file:

• include README MANIFEST MANIFEST.in include setup.py

include testpackages/*.py

Page 36: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

cont

• The setup.py file describes to Distutils (1) how to package the distribution file and (2) how to install the distribution.

• Here is the contents of our sample setup.py:

Page 37: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

cont#!/usr/bin/env pythonfrom distutils.core import setup # [1]long_description = 'Tests for installing and distributing Python packages'setup(name = 'testpackages', # [2] version = '1.0a', description = 'Tests for Python packages', maintainer = 'Dave Kuhlman', maintainer_email = '[email protected]', url = 'http://www.rexx.com/~dkuhlman', long_description = long_description, packages = ['testpackages'] # [3] )

Page 38: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

cont

• Explanation: 1. We import the necessary component from Distutils. 2. We describe the package and its developer/maintainer. 3. We specify the directory that is to be installed as a package. When the user installs our distribution, this directory and all the modules in it will be installed as a package. Now, to create a distribution file, we run the following:

Page 39: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

cont

• python setup.py sdist formats=gztar• which will create a file testpackages

1.0a.tar.gz under the directory dist. Then, you can give this distribution file to a potential user, who can install it by doing the following:

Page 40: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

cont

• $ tar xvzf testpackages 1.0a.tar.gz $ cd testpackages 1.0a $ python setup.py build $ python setup.py install # as root

Page 41: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Overview of Python Standard Library

• The “Python library” contains several different kinds of components.

• It contains data types that would normally be considered part of the “core” of a language, such as numbers and lists. For these types, the Python language core defines the form of literals and places some constraints on their semantics, but does not fully define the semantics.

• On the other hand, the language core does define syntactic properties like the spelling and priorities of operators.

Page 42: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Cont

• The library also contains built-in functions and exceptions — objects that can be used by all Python code without the need of an import statement. Some of these are defined by the core language, but many are not essential for the core semantics and are only described here.

Page 43: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Cont

• The bulk of the library, however, consists of a collection of modules. There are many ways to dissect this collection. Some modules are written in C and built in to the Python interpreter; others are written in Python and imported in source form. Some modules provide interfaces that are highly specific to Python, like printing a stack trace; some provide interfaces that are specific to particular operating systems, such as access to specific hardware; others provide interfaces that are specific to a particular application domain, like the World Wide Web.

Page 44: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

cont

• Some modules are available in all versions and ports of Python; others are only available when the underlying system supports or requires them; yet others are available only when a particular configuration option was chosen at the time when Python was compiled and installed.

Page 45: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

String Handling

• What are strings?• Strings in programming are simply text, either

individual characters, words, phrases, or complete sentences.

• They are one of the most common elements to use when programming, at least when it comes to interacting with the user.

Page 46: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

String

• Because they are so common, they are a native data type within Python, meaning they have many powerful capabilities built-in.

• In Python, strings are immutable sequences of characters.

• They are immutable in that in order to modify a string, you must produce a new string.

Page 47: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

When to use strings

• Any text information.• How to use strings • Create a new string from a constant:• s1 = 'abce‘• s2 = "xyz“• s3 = ""“A multi line string. """

Page 48: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Cont

• Use any of the string methods, for example:• >>> 'The happy cat ran home.'.upper()

'THE HAPPY CAT RAN HOME.' >>> 'The happy cat ran home.'.find('cat')

10 >>> 'The happy cat ran home.'.find('kitten') - 1 >>> 'The happy cat ran home.'.replace('cat', 'dog') 'The happy dog ran home.'

Page 49: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Cont

• Type "help(str)" or see http://www.python.org/doc/current/lib/string methods.html for more information on string methods.

Page 50: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Cont

• You can also use the equivalent functions from the string module. For example:

• >>> import string >>> s1 = 'The happy cat ran home.' >>> string.find(s1, 'happy')

4

Page 51: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Cont

• There is also a string formatting operator: "%". For example:• >>> state = 'California‘• >>> 'It never rains in sunny %s.' % state

'It never rains in sunny California.‘ >>> >>> width = 24 >>> height = 32 >>> depth = 8 >>> print 'The box is %d by %d by %d.' % (width, height, depth, ) The box is 24 by 32 by 8.

Page 52: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Cont

• Things to know:• Format specifiers consist of a percent sign follo

wed by flags, length, and a type character. • The number of format specifiers in the target st

ring (to the left of the "%" operator) must be the same as the number of values on the right.

• When there are more than one value (on the right), they must be provided in a tuple.

Page 53: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Cont

• You can also write strings to a file and read them from a file. Here are some examples. Writing For example:

• >>> outfile = open('tmp.txt', 'w') >>> outfile.write('This is line #1\n') >>> outfile.write('This is line #2\n') >>> outfile.write('This is line #3\n') >>> outfile.close()

Page 54: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Cont

• Notes: • Note the end of

line character at the end of each string. • The open() built

in function creates a file object. It takes as arguments (1) the file name and (2) a mode. Commonly used modes are "r" (read), "w" (write), and "a" (append).

Page 55: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Cont

• Reading an entire file example:• >>> infile = file('tmp.txt', 'r')

>>> content = infile.read()• >>> print content• This is line #1• This is line #2• This is line #3• >>> infile.close()

Page 56: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Cont

• Notes: Also consider using something like content.splitlines(), if you want to divide content in lines (split on newline characters). Reading a file one line at a time example:

Page 57: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Cont

• >>> infile = file('tmp.txt', 'r') >>> for line in infile:

• ... print 'Line:', line• ...• Line: This is line #1• Line: This is line #2• Line: This is line #3• >>> infile.close()

Page 58: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Cont

• Notes: • Learn more about the for: statement in section for: statem

ent. • "infile.readlines()" returns a list of lines in the file. For large

files use the file object itself or "infile.xreadlines()", both of which are iterators for the lines in the file.

• In older versions of Python, a file object is not itself an iterator. In those older versions of Python, you may need to use infile.readlines() or a while loop containing infile.readline() For example:

Page 59: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Cont

• >>> infile = file('tmp.txt', 'r') >>> for line in infile.readlines(): ... print 'Line:', line ...

• A few additional comments about strings: • A string is a special kind of sequence. So, you c

an index into the characters of a string and you can iterate over the characters in a string. For example:

Page 60: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Cont• >>> s1 = 'abcd‘• >>> s1[1]• 'b‘• >>> s1[2]• 'c‘• >>> for ch in s1:• ... print ch• ...• a• b• c• d

Page 61: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Cont

• An interesting feature of string formatting is the ability to use dictionaries to supply the values that are inserted. Here is an example:

• names = {'tree': 'sycamore', 'flower': 'poppy', 'herb': 'arugula'}

• print 'The tree is %(tree)s' % names print 'The flower is %(flower)s' % names print 'The herb is %(herb)s' % names

Page 62: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Basic String Operations

• The "+" and "*" operators are overloaded in Python, letting you concatenate and repeat string objects, respectively.

• Overloading is just using the same operator to do multiple things, based on the situation where it’s used.

• For example, the “+” symbol can mean addition when two numbers are involved or, as in this case, combining strings.

Page 63: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Cont

• Concatenation combines two (or more) strings into a new string object whereas repeat simply repeats a given string a given number of times.

• Here are some examples: • >>> len( ’abc ’) #length : number items • 3

Page 64: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Cont

• >>> ’abc ’ + ’def ’ #concatenation : a new string ’abcdef ’

• >>> ’Ni! ’ 4 #multiple concatentation : "Ni!" ∗+ "Ni!" + ...

’Ni!Ni!Ni!Ni! ’

Page 65: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Cont

• You need to be aware that Python doesn’t automatically change a number to a string, so writing “spam” + 3 will give you an error.

• To explicitly tell Python that a number should be a string, simply tell it.

• >>>str (3) #converts number to string • Just remember that you can no longer perform

mathematical functions with it; it’s strictly text.

Page 66: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Cont

• Iteration in strings is a little different than in other languages. Rather than creating a loop to continually go through the string and print out each character, Python has a built-in type for iteration. Here’s an example followed by an explanation:

Page 67: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Cont

• >>> myjob = "lumberjack“• >>> for c in myjob: print c , #step through

items ...• l u m b e r j a c k • >>> "k" in myjob #1 means true 1

Page 68: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Cont

• Essentially what is happening is that Python is sequentially going through the variable “myjob” and printing each character that exists in the string.

Page 69: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Combining and Separating Strings

• Strings can be combined (joined) and separated (split) quite easily.

• Tokenization is the process of splitting something up into individual tokens; in this case, a sentence is split into individual words.

Page 70: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Cont

• The Python interpreter tokenizes the source code and identifies the parts that are part of the actual programming language and the parts that are data.

• The individual tokens are separated by delimiters, characters that actually separate one token from another.

Page 71: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Cont

• In strings, the main delimiter is a whitespace character, such as a tab, a newline, or an actual space.

• These delimiters mark off indi- vidual characters or words, sentences, and paragraphs.

Page 72: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Cont

• Joining strings combines the separate strings into one string.

• Because string operations always create a new string, you don’t have to worry about the original strings being overwritten.

• The catch is that it doesn’t concatenate the strings, i.e. joining doesn’t combine them like you would expect.

• Here’s an example:

Page 73: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Cont

• >>>string1 = "1 2 3" • >>>string2= "A B C" • >>>string3 = string2 . join ( string1 ) • >>>print string3 • 1A B C A B C2A B C A B C3

Page 74: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Times and Dates

• A Python program can handle date & time in several ways.

• Python's time and calendar modules help track dates and times.

Page 75: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

How to Measure Time

• Time intervals are floating-point numbers in units of seconds.

• Particular instants in time are expressed in seconds since 12:00am, January 1, 1970(epoch).

Page 76: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Tick

• There is a popular time module available in Python which provides functions for working with times, and for converting between representations.

• The function time.time() returns the current system time in ticks since 12:00am, January 1, 1970(epoch).

Page 77: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Cont

• >>> import time; # This is required to include time module.

• >>> ticks = time.time() • >>> print "Number of ticks since 12:00am,

January 1, 1970:", ticksNumber of ticks since 12:00am, January 1, 1970: xxxxxxx.xxx

Page 78: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Date Arithmetic

• Date arithmetic is easy to do with ticks. However, dates before the epoch cannot be represented in this form. Dates in the far future also cannot be represented this way - the cutoff point is sometime in 2038 for UNIX and Windows.

Page 79: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Time Tuple

• What is TimeTuple?• Many of Python's time functions handle time

as a tuple of 9 numbers, as shown below:

Page 80: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Time TupleIndex Field Values

0 4-digit year 2014

1 Month 1 to 12

2 Day 1 to 31

3 Hour 0 to 23

4 Minute 0 to 59

5 Second 0 to 61 (60 or 61 is a leap second)

6 Day of the week 0 to 6

7 Day of the year 1 to 366

8 Daylight savings -1, 0, 1, -1 means library determined DST

Page 81: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Struct_time

• The above tuple is equivalent to struct_time structure. This structure has following attributes:

Page 82: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

struct-timeIndex Attributes Values

0 tm_year 2014

1 tm_mon 1 to 12

2 tm_mday 1 to 31

3 tm_hour 0 to 23

4 tm_min 0 to 59

5 tm_sec 0 to 61 (60 and 61 are the leap second)

6 tm_wday 0 to 6

7 tm_yday 1 to 366 (Julian Day)

8 tm_isdst -1,0,1, -1 means library determined DST

Page 83: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Getting current time

• To translate a time instant from a seconds since the epoch floating-point value into a time-tuple, pass the floating- point value to a function (e.g., localtime) that returns a time-tuple with all nine items valid.

Page 84: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

cont

• >>> import time;• >>> localtime = time.localtime(time.time())

>>> print "Local current time :", localtime

Page 85: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Getting formatted time

• You can format any time as per your requirement, but simple method to get time in readable format is asctime():

• >>> import time;• >>> localtime =

time.asctime( time.localtime(time.time()) ) • >>> print "Local current time :", localtime

Page 86: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Getting calendar for a month

• The calendar module gives a wide range of methods to play with yearly and monthly calendars. Here, we print a calendar for a given month ( Jan 2008 ):

• >>> import calendar• >>> cal = calendar.month(2008, 1) • >>> print "Here is the calendar:" • >>> print cal

Page 87: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

time module

• There is a popular time module available in Python which provides functions for working with times and for converting between representations.

Page 88: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

calendar module

• The calendar module supplies calendar-related functions, including functions to print a text calendar for a given month or year.

Page 89: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Mathematics and Numbers

• Number data types store numeric values. They are immutable data types, which means that changing the value of a number data type results in a newly allocated object.

• Number objects are created when you assign a value to them. For example:

• var1 = 1 • var2 = 10

Page 90: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Delete a number object

• You can also delete the reference to a number object by using the del statement. The syntax of the del statement is:

• del var1[,var2[,var3[....,varN]]]]• You can delete a single object or multiple

objects by using the del statement. For example:

• del var • del var_a, var_b

Page 91: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Numerical Types

• Python supports four different numerical types:

• int (signed integers): often called just integers or ints, are positive or negative whole numbers with no decimal point.

• long (long integers ): or longs, are integers of unlimited size, written like integers and followed by an uppercase or lowercase L.

Page 92: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

cont

• float (floating point real values) : or floats, represent real numbers and are written with a decimal point dividing the integer and fractional parts. Floats may also be in scientific notation, with E or e indicating the power of 10 (2.5e2 = 2.5 x 102 = 250).

Page 93: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

cont

• complex (complex numbers) : are of the form a + bJ, where a and b are floats and J (or j) represents the square root of -1 (which is an imaginary number). a is the real part of the number, and b is the imaginary part. Complex numbers are not used much in Python programming.

Page 94: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Examplesint long float complex

10 51924361L 0.0 3.14j

100 -0x19323L 15.20 45.j

Page 95: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Number Type Conversion:

• Python converts numbers internally in an expression containing mixed types to a common type for evaluation.

• But sometimes, you'll need to coerce a number explicitly from one type to another to satisfy the requirements of an operator or function parameter.

Page 96: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

cont

• Type int(x)to convert x to a plain integer.• Type long(x) to convert x to a long integer.• Type float(x) to convert x to a floating-point

number.• Type complex(x) to convert x to a complex

number with real part x and imaginary part zero.• Type complex(x, y) to convert x and y to a

complex number with real part x and imaginary part y. x and y are numeric expressions

Page 97: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Mathematical Functions:

• Python includes following functions that perform mathematical calculations.

Page 98: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

contFunction Returns ( description )

abs(x) The absolute value of x: the (positive) distance between x and zero.

ceil(x) The ceiling of x: the smallest integer not less than x

cmp(x,y) -1 if x < y, 0 if x == y, or 1 if x > y

exp(x) The exponential of x: ex

fabs(x) The absolute value of x.

floor(x) The floor of x: the largest integer not greater than x

log(x) The natural logarithm of x, for x> 0

log10(x) The base-10 logarithm of x for x> 0 .

max(x1,x2,…) The largest of its arguments: the value closest to positive infinity

Page 99: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

contFunction Returns (description)

min(x1,x2,….) The smallest of its arguments: the value closest to negative infinity

modf(x) The fractional and integer parts of x in a two-item tuple. Both parts have the same sign as x. The integer part is returned as a float.

pow(x,y) The value of x**y.

round(x,[.n]) x rounded to n digits from the decimal point. Python rounds away from zero as a tie-breaker: round(0.5) is 1.0 and round(-0.5) is -1.0.

sqrt(x) The square root of x for x > 0

Page 100: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Trigonometric Functions

Function Description

acos(x) Return the arc cosine of x, in radians.

asin(x) Return the arc sine of x, in radians.

atan(x) Return the arc tangent of x, in radians.

atan2(x,y) Return atan(y / x), in radians.

cos(x) Return the cosine of x radians.

hypot(x,y) Return the Euclidean norm, sqrt(x*x + y*y).

sin(x) Return the sine of x radians.

tan(x) Return the tangent of x radians.

degrees(x) Converts angle x from radians to degrees.

radians(x) Converts angle x from degrees to radians.

Page 101: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Mathematical ConstantsConstant Description

pi The mathematical constant pi.

e The mathematical constant e.

Page 102: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Command-Line Programming

• A command is used to invocate a script• One liners -> shell scripts -> applications • A lot of interfaces:• Files• Pipes• User’s input/output• Networking

Page 103: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

cont

• The Unix model:• Lots of small tools that can be combined in

lots of useful ways.

Page 104: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Why Python for command-line programming

• It is available in a wide range of platforms• Readable, consistent syntax• Easy to write and maintain• Scale well with large apps and libraries• Lots of modules (operating system functions,

networking, file systems)

Page 105: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

if _name_==‘__main__’

• For any Python script break it up into: • Functions• A main function, called from command line• Make it easy to:• Test functionality• Reuse the functions

Page 106: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Anatomy of a command line

Page 107: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Files

• Reading, writing, appending to files• Text or binary format• Example:file1.py• Example:file2.py

Page 108: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Pipes

• Instead of a filename, pipe input/output• Create chains of tools• Standard pipes:• Input: stdin• Output: stdout & stderr• The sys module has support for these• The fileinput module support reading from

stdin and files

Page 109: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Passing of an argument/arguments

• Allow user to specify argument• (edit script, modify a file)• Need to handle (flags, strings, pipes, invalid

number of commands, type checking, range checking)

Page 110: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Thoughts

• Always provide help at the command line• Be consistent (short/long flag, dangerous flag,

sensible abbreviation for flags)

Page 111: INF1204-FUNDAMENTALS OF PROGRAMMING II -15 Credits -15 weeks -4 hours a week.

Calling Command

• Python can execute other applications• Example: subprocess module is the best of STD

library modules.• The envoy module is a whole lot easier and

more Pythonic.