Download Python and more Lecture notes Advanced Computer Programming in PDF only on Docsity!
Python
How You Can Use and Write Python Programs
(Part I)
Intro
I’m a Systems Programmer with IT/NSS.
● I’ve been programming over 30 years
● Recent convert to Python
● Needed to use Python because Google APIs are written
for Python
● I’m new to Python. You can probably stump me
● …. unless I can use Google.
What is Python?
When Python modules are converted, the results are stored as.
pyc files.
Modules are an advanced topic. I mention it in case you install a
package. You will want write access so that the .pyc files can be
created.
Python will still run if the .pyc files can not be created. Just a little
more slowly.
What is Python?
So, it is a little hard to put Python in a box. It can be used for
many things.
Its role in high performance computing is to allow simple use of
optimized libraries that implement mathematical routines in an
efficient manner.
Python is reasonably fast, but it is not a number crunching
language by itself. Extra packages are required such as NumPy.
Python Resources:
● Python - http://python.org/
● PEPs - https://www.python.org/dev/peps/
(PEPs are Python Enhancement Proposals but are often used to document
various Python issues.)
PEP 8 is required reading if you plan to write Python programs. Less so if you just
need to read or make minor changes.
● The last slide contains more on help. Left to end so you will remember!
Python Resources: Zen
import this
The Zen of Python, by Tim Peters
● Beautiful is better than ugly.
● Explicit is better than implicit.
● Simple is better than complex.
● Complex is better than complicated.
● Flat is better than nested.
● Sparse is better than dense.
● Readability counts.
● Special cases aren't special enough to
break the rules.
● Although practicality beats purity.
● Errors should never pass silently.
● Unless explicitly silenced.
● In the face of ambiguity, refuse the
temptation to guess.
● There should be one-- and preferably only
one --obvious way to do it.
● Although that way may not be obvious at
first unless you're Dutch.
● Now is better than never.
● Although never is often better than right
now.
● If the implementation is hard to explain, it's
a bad idea.
● If the implementation is easy to explain, it
may be a good idea.
● Namespaces are one honking great idea --
let's do more of those!
In (Modern) C, the classic “Hello, World!” Program is:
#include <stdio.h> int main( int argc, char ** argv ) { int i; printf( "Hello, World!\n" ); for( i = 0; i < argc; i++ ) { printf( " Arg %d: %s\n", i, argv[i] ); }
return( 0 ); } doc => gcc a.c doc => ./a.out a Hello, World! Arg 0: ./a.out Arg 1: a
In Python:
#!/usr/bin/python import sys
print( "Hello World" ) for (i, arg ) in enumerate( sys.argv ): print( " Arg %d: %s" % (i, arg) ) doc => ./a.py 12 bc Hello World Arg 0: ./a.py Arg 1: 12 Arg 2: bc
Python Source Code
In Python, a “main” routine is not needed. Python executes your script top to
bottom.
Python Source Code
for (i, arg ) in enumerate( sys.argv ): print( " Arg %d: %s" % (i, arg) )
The indentation is super important.
● In C, I could have written the entire program on one line.
● In Python, I could too. But, no one does that.
● In Python, the indent prior to print is what associates the print with the for.
Most (all?) Python statements that start a block of code end with a colon (:)
Python Source Code
● So, indenting is important.
● Line ends matter
Remember, you can always just add in parens if you need to continue the line
VS
if (ThisLongVariable > SomeOtherLongVariable &&
AndThisOneToo < TheLastOne):
print( “It’s good.” )
if ThisLongVariable > SomeOtherLongVariable && AndThisOneToo < TheLastOne:
print( “Yuck!” )
Python Source Code
● This simple fact that lines can be continued when any brace like pattern is open is what finally
made me love Python.
● Once you get used to not typing braces and semicolons, you will not want to go back!
● And very, very rarely should you use the line continuation character “\” In over 50,000 lines, I have
never used it.
Python Editing
Because editing in Python is based on indents
… and indents of level 8 - a full tab stop - are rather long
… and indents of level 2 are horrid to read
… and only certain people use indents of five (you know who you are!)
Setting up your editor to convert a tab key to four spaces is quite useful.
Python Editing
In my ~/.exrc file for vi, I have
set shiftwidth= set tabstop= set expandtab set softtabstop=
This causes any tab key press to be converted to align to column divisible by 4, and to use
spaces. This will not convert existing tabs to align on four, though. Converting code in that
manner, unless it is yours, can cause anger!
There are many clever ways to do this so it only works for Python files, for instance. Google
your options...