Python, Lecture notes of Advanced Computer Programming

Python can be converted to a “C” program and compiled. ... Python statements that start a block of code end with a colon (:) ... made me love Python.

Typology: Lecture notes

2021/2022
On special offer
30 Points
Discount

Limited-time offer


Uploaded on 08/05/2022

char_s67
char_s67 🇱🇺

4.5

(116)

1.9K documents

1 / 111

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Python
How You Can Use and Write Python Programs
(Part I)
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64
Discount

On special offer

Partial preview of the text

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...