Python For Programmers
Python For Programmers
Large-scale programming
Python
4/25/16
4/25/16
debug, modify
Variables
variable assigned to any type
lists can include different data type
*From Ceder book, page 19 (see refs)
4/25/16
and more
Example: write web server to share files in a directory
import http.server
http.server.test(Handlerclass=http.server. SimpleHTTPRequest Handler)
Cross Platform
Windows, Mac, Linux,
Interpreted, so same code runs on all
Can run on Java (Jython) and .Net (IronPython)
4/25/16
Python is readable
#Perl version
sub pairwise_sum {
my($arg1, $arg2) = @_;
my(@result)=();
@list1 = @$arg1;
@list2 = @$arg2;
for ($i=0;$i < length(@list1); $i++ {
push(@result, $list1[$i] + $list2[$i];
}
return(\@result);
}
4/25/16
Python is readable
#Python version for the same task
def pairwise_sum(list1, list2):
result=[]
for i in range(len(list1)):
result.append(list1[i] + list2[i])
return result
4/25/16
Things to consider
often outweigh
Easy to integrate with C/C++ modules for
CPU intensive components
*From Ceder book, page 19 (see refs)
4/25/16
Things to consider
Speed
byte-code Python interpreter
But not always, e.g., regular expressions
as fast or faster than C programs
This may all be ok, since
with fast computers, development costs often outweigh
Easy to integrate with C/C++ modules for CPU
intensive components
*From Ceder book, page 19 (see refs)
4/25/16
Python 2.7
existing code to use, maintain
need certain libraries
tools available to help transition to 3.x
Python 2.x
Python
Print hello
Print(hello)
4/25/16
10
Python 3
is 0
is 0.5
1//2 is 0
4/25/16
11
Overview for
programmers
4/25/16
12
Python Synopsis*
Python has a number of built-in data types such as integers,
floats, complex numbers, strings, lists, tuples, dictionaries, and
file objects. These can be manipulated using language
operators, built-in functions, library functions, or a data types
own methods.
[ object stuff omitted, but its in the language]
Python provides conditional and iterative control flow through
an if-elif-else construct along with while and for loops. It
allows function definition with flexible argument-passing
options. Exceptions (errors) can be raised using the raise
statement and caught and handled using the try-except-else
construct.
Variables dont have to be declared and can have any built-in
data type, user defined object, function, or module assigned to
them.
*From Ceder book, page 19 (see refs)
4/25/16
13
Built-in datatypes
Numbers
Integers
Floats
Complex Numbers (3 + 2j)
Booleans (True, False)
behave like 1 and 0
Numeric Operators
+, -, *, /, %, ** (exponentiation)
built in operators and libraries
import math
Complex numbers
x = (3 + 2j) * (4 + 9j)
is (-6 + 35j)
x.real
is -6
x.imag is 35j
*From Ceder book, chapter 3f
4/25/16
14
Other datatypes
more datatypes
strings
lists
dictionaries
tuples
sets
4/25/16
15
Strings
delimiting
Can contain single quotes here
Can contain single quotes here
''' \t starts with tab, end with newline char\n ''
""triple quoted can contain real newlines, like in a
file??? ""
*From Ceder book, page 19 (see refs)
4/25/16
16
Lists
library includes
append, count, pop, remove, sort, reverse
x = [1, (2,3,)piggie]
x[2] is piggie
x[1] is (2,3)
x[-1]is piggie
x[0:3] is [1, (2, 3), 'piggie']
x[-2:] is [(2, 3), 'piggie']
4/25/16
17
List comprehensions
>>> [n*n for n in range(1,11)]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
>>> [c.upper() for c in 'piggie']
['P', 'I', 'G', 'G', 'I', 'E']
>>> nums=[2,-4,13,-17, 2]
>>> result=[n for n in nums if n>0]
>>> result
[2, 13, 2]
*From Ceder book, page 19 (see refs)
4/25/16
18
4/25/16
19
if elif
for i in range (10,0,-1)print(i)
see airfare.py
while loop
x=2
y=4
while y > x:
y = y-1
print y
for loop
includes continue and break statements (as in C)
*From Ceder book, page 19 (see refs)
4/25/16
20
More stuff.
functions
flexible. simple example:
funct1(u,z=v,y=2) # returns 23
see demofunctions.py
exceptions
try-except-finally-else (for 3, maybe for 2)
File handling
basic open, close, reading & writing
sys library for access to stdin, stdout, stderr
Pickle to easily read/write Python data types
struct library to read/write files for use with C programs
4/25/16
21
Resources
Python.org
Good series (both 2.7 and 3 versions)
for example, from the series by Derek Banas
Tkinter GUIs
4/25/16
22