LIS 559: Programming for Library and Information Science Applications - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

LIS 559: Programming for Library and Information Science Applications

Description:

print has a carriage return at the end, to remove this, add a , print 'Hello, World! ... and, assert, break, class, continue, def, del, elif, else, except, exec, ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 19
Provided by: publi2
Category:

less

Transcript and Presenter's Notes

Title: LIS 559: Programming for Library and Information Science Applications


1
LIS 559 Programming for Library and Information
Science Applications
  • Summer 2006
  • Margaret Kipp
  • mkipp_at_uwo.ca
  • x88687
  • NCB 235

2
Basic Concepts
  • Getting Started
  • First Programme
  • Types
  • Strings
  • Variables
  • Conditionals
  • Flow Control Structures
  • Namespaces
  • Input and Output

3
Getting Started
  • Programming Language Python
  • ActivePython
  • Start -- Programs -- ActiveState ActivePython
  • Pythonwin IDE (Integrated Development
    Environment)
  • Interactive Python session
  • Text editor
  • http//www.activestate.com/Products/ActivePython/

4
First Programme
  • Type print "Hello, World!"
  • This is an extremely simple programme
  • print is a builtin Python command
  • print has a carriage return at the end, to remove
    this, add a ,
  • print "Hello, World!",
  • print "Hello, World!"

5
Reserved Words (builtin commands)
  • and, assert, break, class, continue, def, del,
    elif, else, except, exec, finally, for, from,
    global, if, import, in, is, lambda, not, or,
    pass, print, raise, return, try, while

6
Variables
  • A variable stores programme data.
  • A variable has a type. It can be a number, a
    string or something more complex.
  • e.g.
  • hello "Hello, World!"
  • print hello
  • hello is a variable of type string

7
Variables (cont.)
  • Variables cannot have the same name as a builtin
    command in Python
  • Variable names should tell others what the
    variable does
  • e.g. 'name' for a person's name, rather than
    'column1'

8
Types
  • String
  • "python", "Hello, World!"
  • Number
  • 1, 1.5, 1000
  • List
  • "apple", "orange", "banana"
  • 1,2,3
  • Tuple
  • (1, 2, 4)
  • Dictionary
  • "author" "Jane Doe", "title" "This is not a
    book"

9
Numbers
  • integers (1, 2, 3), floats (e.g. 1.56)
  • basic arithmetic - /
  • is the modulo operator or remainder of
    division, e.g. 50 5 0 because 50 is divisible
    by 5
  • if a b is not zero, then a is not divisible by
    b
  • tests
  • lt (less than) gt (greater than)
  • (equals) ! (is not equal to)
  • gt (greater than or equal to) lt (less than or
    equal to)

10
Strings
  • Always start/end with quotes " or ', either 1, 2
    or 3 (3 quotes allow strings to span multiple
    lines)
  • a "Hello, World!"
  • b "This is a sample programme."
  • a0 H
  • a1 e
  • a2 l
  • ...
  • a-1 ! (can be used to pick letters from the
    end)

11
Strings (cont.)
  • concatenation sticking strings together
  • just like adding numbers a b
  • a "Hello, World!"
  • b "This is a sample programme."
  • print a b
  • result "Hello, World! This is a sample
    programme."
  • length number of letters/numbers in a string
  • len()
  • print len(a)
  • result 13
  • print len(b)
  • result 27

12
Strings (cont.)
  • strip() removes spaces and returns from the end
    of the string
  • s "Hello, World! "
  • s s.strip()
  • print s
  • result "Hello, World!"
  • split(" ") splits a string
  • s "Hello, World!"
  • s s.split(" ")
  • print s
  • result "Hello,", "World!"

13
Strings (cont.)
  • find(substring) finds a substring inside the
    string
  • s "Hello, World!"
  • s s.find("lo")
  • print s
  • result 3
  • replace(substring, new) replaces a substring
    with something new
  • s "Hello, World!"
  • s s.replace("World","Class")
  • result "Hello, Class!"

14
Conditionals
  • If-Then
  • Used to determine which path the programme will
    take
  • a 7
  • b 5
  • if a lt b
  • print str(a) " is less than " str(b)
  • elif a gt b
  • print str(a) " is greater than " str(b)
  • else
  • print str(a) " equals " str(b)

15
Flow Control Structures (Loops)
  • while loop
  • used when the number of times the loop will run
    is not known
  • syntax
  • i 20
  • while i gt 5
  • i i / 2
  • print i
  • result 5
  • Note that any lines that are part of the while
    loop must be indented

16
Loops (cont.)
  • for loop
  • useful where the number of times the loop will
    run is known
  • syntax
  • j 0
  • for i in range(1,5)
  • j j 1
  • print j
  • result 4
  • range command
  • this command creates a list of numbers, in this
    case 1, 2, 3, 4, 5, it's extremely useful in
    for loops
  • or
  • for item in allDatabaseEntries
  • print item

17
Input and Output
  • Output
  • print
  • e.g.
  • print "Hello, World!"
  • print 54
  • Input
  • raw_input()
  • e.g.
  • name raw_input("What is your name?")

18
Saved Programmes (import)
  • import
  • used to run programmes typed in a text editor at
    the command line
  • can also be used to add other programmes to yours
  • import helloworld.py
Write a Comment
User Comments (0)
About PowerShow.com