Advanced scripting programming - PowerPoint PPT Presentation

1 / 45
About This Presentation
Title:

Advanced scripting programming

Description:

MSc Bioinformatics. Assignment. spam = 'Spam' Basic form ... MSc Bioinformatics. Input. If int or float desired, simply convert string: ... – PowerPoint PPT presentation

Number of Views:56
Avg rating:3.0/5.0
Slides: 46
Provided by: sgam1
Category:

less

Transcript and Presenter's Notes

Title: Advanced scripting programming


1
Advanced scripting programming
  • jesus.ibanez_at_upf.edu

2
Assignment
  • spam 'Spam' Basic form
  • spam, ham 'yum', 'YUM' Tuple assignment
    (positional)
  • spam, ham 'yum', 'YUM' List assignment
    (positional)
  • spam ham 'lunch' Multiple-target

3
Assignment
  • nudge 1
  • wink 2
  • A, B nudge, wink tuples
  • A, B
  • (1, 2)
  • C, D nudge, wink lists
  • C, D
  • (1, 2)
  • nudge, wink wink, nudge tuples
    swaps values
  • nudge, wink
  • (2, 1)

4
Variable name rules
  • Syntax
  • (underscore or letter) (any number of letters,
    digits, or underscores)
  • Case matters
  • SPAM is not the same as spam
  • Reserved words are off limits
  • print, and, while, etc

5
Input
  • Function raw_input() designed to read a line of
    input from the user. 1 optional argument string
    to prompt user
  • mystr raw_input("Enter a string")
  • Enter a stringHello World!
  • mystr
  • 'Hello World!'

6
Input
  • If int or float desired, simply convert string
  • int(mystring)-convert to int (if possible)
  • float(mystring)-convert to float (if possible)
  • a int(raw_input("Enter an integer "))
  • Enter an integer 123
  • a
  • 123

7
Output
  • print spam, ham Print objects to sys.stdout, add
    a space between
  • print spam, ham, Same, but don't add newline at
    end
  • print "a", "b"
  • a b
  • print "a" "b"
  • ab
  • print "s...s" ("a", "b")
  • a...b

8
Output
  • print 'hello world' print a
    string object
  • hello world
  • 'hello world'
    interactive prints
  • 'hello world'
  • import sys
    printing the hard way
  • sys.stdout.write('hello world\n')
  • hello world

9
Relational and logical operators
  • Relational operators
  •  
  • equal
  • !, not equal
  • greater than
  • greater than or equal
  • Logical operators
  • and
  • or
  • not
  • True means any nonzero number or nonempty object.
  • False means not true a zero number, empty
    object, or None.

10
Control of flow
  • Sequential
  • Conditional if
  • Iterative while, for

11
Decision making statements
if
12
Decision making statements
if elif
else

13
Decision making statements
  • example
  • if 1
  • ... print 'true'
  • ...
  • true
  • if not 1
  • ... print 'true'
  • ... else
  • ... print 'false'
  • ...
  • false

14
Decision making statements
  • example
  • x 'killer rabbit'
  • if x 'roger'
  • ... print "how's jessica?"
  • ... elif x 'bugs'
  • ... print "what's up doc?"
  • ... else
  • ... print 'Run away! Run away!'
  • ...
  • Run away! Run away!

15
Decision making statements
  • example
  • if choice 'spam'
  • ... print 1.25
  • ... elif choice 'ham'
  • ... print 1.99
  • ... elif choice 'eggs'
  • ... print 0.99
  • ... elif choice 'bacon'
  • ... print 1.10
  • ... else
  • ... print 'Bad choice'
  • ...
  • 1.99

16
Decision making statements
  • choice 'ham'
  • print 'spam' 1.25,
  • ... 'ham' 1.99,
  • ... 'eggs' 0.99,
  • ... 'bacon' 1.10choice
  • 1.99
  • a dictionary-based 'switch',
  • use has_key() test for default case
  • example
  • if choice 'spam'
  • ... print 1.25
  • ... elif choice 'ham'
  • ... print 1.99
  • ... elif choice 'eggs'
  • ... print 0.99
  • ... elif choice 'bacon'
  • ... print 1.10
  • ... else
  • ... print 'Bad choice'
  • ...
  • 1.99

17
Decision making statements
18
Decision making statements
  • x 1
  • if x
  • y 2
  • if y
  • print 'block2'
  • print 'block1'
  • print 'block0'

19
Program looping
while
20
Program looping
while else

21
Program looping
  • example
  • while 1
  • ... print 'Type Ctrl-C to stop me!'

22
Program looping
  • example
  • x 'spam'
  • while x
  • ... print x,
  • ... x x1 strip first character
    off x
  • ...
  • spam pam am m

23
Program looping
  • example
  • a0 b10
  • while a loops
  • ... print a,
  • ... a a1
  • ...
  • 0 1 2 3 4 5 6 7 8 9

24
Program looping
  • break
  • Jumps out of the closest enclosing loop (past the
    entire loop statement).
  • continue
  • Jumps to the top of the closest enclosing loop
    (to the loop's header line).
  • pass
  • Does nothing at all it's an empty statement
    placeholder.
  • loop else block
  • Run if and only if the loop is exited
    normallyi.e., without hitting a break.

25
Program looping
while if
break if continue else

26
Program looping
  • example
  • while 1 pass type Ctrl-C to stop me!

27
Program looping
  • example
  • x 10
  • while x
  • x x-1
  • if x 2 ! 0 continue odd?--skip print
  • print x,

28
Program looping
  • example
  • x y / 2
  • while x 1
  • if y x 0 remainder
  • print y, 'has factor', x
  • break skip else
  • x x-1
  • else normal exit
  • print y, 'is prime'

29
Program looping
for in
30
Program looping
for in else

31
Program looping
for in
if break if continue else

32
Program looping
  • example
  • for x in "spam", "eggs", "ham"
  • ... print x,
  • ...
  • spam eggs ham

33
Program looping
  • example
  • sum 0
  • for x in 1, 2, 3, 4
  • ... sum sum x
  • ...
  • sum
  • 10
  • prod 1
  • for item in 1, 2, 3, 4 prod prod item
  • ...
  • prod
  • 24

34
Program looping
  • example
  • S, T "lumberjack", ("and", "I'm", "okay")
  • for x in S print x,
  • ...
  • l u m b e r j a c k
  • for x in T print x,
  • ...
  • and I'm okay
  • T (1, 2), (3, 4), (5, 6)
  • for (a, b) in T
    tuple assignment at work
  • ... print a, b
  • ...
  • 1 2
  • 3 4
  • 5 6

35
Program looping
  • example
  • items "aaa", 111, (4, 5), 2.01
    a set of objects
  • tests (4, 5), 3.14
    keys to search for
  • for key in tests
    for all keys
  • ... for item in items
    for all items
  • ... if item key
    check for match
  • ... print key, "was found"
  • ... break
  • ... else
  • ... print key, "not found!"
  • ...
  • (4, 5) was found
  • 3.14 not found!

36
Program looping
  • example
  • for key in tests for all keys
  • ... if key in items let Python
    check for a match
  • ... print key, "was found"
  • ... else
  • ... print key, "not found!"
  • ...
  • (4, 5) was found
  • 3.14 not found!

37
Program looping
  • example
  • seq1 "spam"
  • seq2 "scam"
  • res start empty
  • for x in seq1 scan first
    sequence
  • ... if x in seq2 common item?
  • ... res.append(x) add to result
    end
  • ...
  • res
  • 's', 'a', 'm'

38
Program looping
  • example
  • file open("name", "r")
  • while 1
  • line file.readline() fetch next line,
    if any
  • if not line break exit loop on
    end-of-file
  • Process line here

39
Program looping
  • example
  • file open("name", "r")
  • for line in file.readlines() read into a
    lines list
  • Process line here

40
Range and counter loops
  • example
  • range(5), range(2, 5), range(0, 10, 2)
  • (0, 1, 2, 3, 4, 2, 3, 4, 0, 2, 4, 6, 8)

41
Range and counter loops
  • example
  • X 'spam'
  • for item in X print item,
    simple iteration
  • ...
  • s p a m

42
Range and counter loops
  • example
  • i 0
  • while i iteration
  • ... print Xi, i i1
  • ...
  • s p a m

43
Range and counter loops
  • example
  • for i in range(len(X)) print Xi,
    manual indexing
  • ...
  • s p a m

44
Range and counter loops
  • example
  • for i in range(3) print i, 'Pythons'
  • ...
  • 0 Pythons
  • 1 Pythons
  • 2 Pythons

45
Exercises
  • Try to translate some of your C or Perl programs
    into Python ones.
Write a Comment
User Comments (0)
About PowerShow.com