Module 4 –Python and Regular Expressions

[Pages:22]Module 4 ? Python and Regular Expressions

? Module 4 contains only an individual assignment

? Due Wednesday Oct 2nd

? Do not wait until the last minute to start on this module

? Read the WIKI before starting along with a few Python tutorials

? Portions of today's slides came from

? Marc Conrad ? University of Luton

? Paul Prescod ? Vancouver Python Users' Group

? James Casey ? Opscode

? Tim Finin ? Univeristy of Maryland

1E- xCStEen33s0ib?leCreNaetitvwe PororkgrinamgmPilnagtfaondrmRapid Prototyping

1

What is Python?

? Python is an easy to learn, powerful programming language ? Efficient high-level data structures ? Simple approach to object-oriented programming. ? Elegant syntax and dynamic typing ? Up-and-coming language in the open source world

? We are using Python version 3.4 or later in this course

2E- xCStEen33s0ib?leCreNaetitvwe PororkgrinamgmPilnagtfaondrmRapid Prototyping

2

Usability Features

? Very clear syntax ? Obvious way to do most things ? Huge amount of free code and libraries ? Interactive ? Only innovative where innovation is really

necessary

? Better to steal a good idea than invent a bad one!

3E- xCStEen33s0ib?leCreNaetitvwe PororkgrinamgmPilnagtfaondrmRapid Prototyping

3

Python Hello world"

print ("Hello, World")

4E- xCStEen33s0ib?leCreNaetitvwe PororkgrinamgmPilnagtfaondrmRapid Prototyping

4

Python Interpreter

? Just type:

? Todds-MacBook-Air:~ todd$ python3 ? Python 3.6.1 (default, Apr 4 2017, 09:40:21) ? [GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.38)]

on darwin ? Type "help", "copyright", "credits" or "license" for more

information.

5E- xCStEen33s0ib?leCreNaetitvwe PororkgrinamgmPilnagtfaondrmRapid Prototyping

5

Features of the Interpreter

? Lines start with >>>. You can recognize Python interpreter transcripts anywhere you see them.

? Expressions that return a value display the value. >>> 5+3*4 17

? This saves you from excessive printing

6E- xCStEen33s0ib?leCreNaetitvwe PororkgrinamgmPilnagtfaondrmRapid Prototyping

6

Interactive Interpreters

? Windows command line ? OS X ? Linux/Unix ? Graphical command lines: IDLE, PythonWin,

MacPython, ... ? Jython ? And many more...

7E- xCStEen33s0ib?leCreNaetitvwe PororkgrinamgmPilnagtfaondrmRapid Prototyping

7

Python scripts

? Sometimes you want to run the same program more than once!

? Make a file with Python statements in it: foo.py:

print (hello world)

todd$ python3 foo.py hello world todd$ python3 foo.py hello world

8E- xCStEen33s0ib?leCreNaetitvwe PororkgrinamgmPilnagtfaondrmRapid Prototyping

8

Python is dynamically typed

width = 20 print (width) height = 5 * 9 print (height) print (width * height) width = "really wide" print (width)

9E- xCStEen33s0ib?leCreNaetitvwe PororkgrinamgmPilnagtfaondrmRapid Prototyping

9

Experiment in the Interpreter

? Any Python variable can hold any value.

>>> width = 20 >>> height = 5 * 9 >>> print (width * height) 900 >>> width = "really wide" >>> print (width) really wide

10E-xCteSEn3s3ib0 l?eCNreeattiwveoPrrkoginragmPmlaintgfoanrmd Rapid Prototyping

10

Dynamic Type Checking

test_sqrt.py:

import math

def square_root(num): return math.sqrt(num)

def goodfunc(): print (square_root(10))

def badfunc(): print (square_root("10"))

goodfunc() badfunc()

11E-xCteSEn3s3ib0 l?eCNreeattiwveoPrrkoginragmPmlaintgfoanrmd Rapid Prototyping

11

Multiple statements on a line

? You can combine multiple simple statements on a line:

>>> a = 5;print (a); a = 6; print (a) 5 6

12E-xCteSEn3s3ib0 l?eCNreeattiwveoPrrkoginragmPmlaintgfoanrmd Rapid Prototyping

12

Indentation

? Python uses indentation for scoping:

if this_function(that_variable): do_something()

else: do_something_else()

13E-xCteSEn3s3ib0 l?eCNreeattiwveoPrrkoginragmPmlaintgfoanrmd Rapid Prototyping

13

Indentation

? Tabs and spaces look the same in most editors.

? If your editor uses a different conversion rate between tabs and spaces than standard, your Python code may not parse properly.

? Three easy solutions:

1. Only use tabs or spaces in a file: dont mix them. 2. Use an editor that knows about Python. 3. Configure editor to use the same tab/space rules as Python, vi, emacs,

notepad, edit, etc. : 8 spaces per tab

14E-xCteSEn3s3ib0 l?eCNreeattiwveoPrrkoginragmPmlaintgfoanrmd Rapid Prototyping

14

Compared to PHP/Javascript

? Excellent for Web apps (PHP on server, Javascript on client) but not much else.

? Python can be used for your Web apps, your complicated algorithms, your GUIs, your COM components, an extension language for Java programs

? Even in Web apps, Python handles complexity better.

15E-xCteSEn3s3ib0 l?eCNreeattiwveoPrrkoginragmPmlaintgfoanrmd Rapid Prototyping

15

Compared to Java

? Java is more difficult for amateur programmers.

? Static type checking can be inconvenient and inflexible.

? Bottom line: Java can make projects harder than they need to be.

16E-xCteSEn3s3ib0 l?eCNreeattiwveoPrrkoginragmPmlaintgfoanrmd Rapid Prototyping

16

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download