Advanced scripting programming - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Advanced scripting programming

Description:

Advanced scripting programming lectures 3 and 4. MSc Bioinformatics ... M = ['X', L, 'Y'] # embed a reference to L M ['X', [1, 2, 3], 'Y' ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 15
Provided by: sgam1
Category:

less

Transcript and Presenter's Notes

Title: Advanced scripting programming


1
Advanced scripting programming
  • jesus.ibanez_at_upf.edu

2
Built-in data types
3
Generality
  • In general
  • Lists, dictionaries, and tuples can hold any kind
    of object.
  • Lists, dictionaries, and tuples can be
    arbitrarily nested.
  • Lists and dictionaries can dynamically grow and
    shrink.

4
Generality
  • gtgtgt L 'abc', (1, 2), (3, 4), 5
  • gtgtgt L1
  • (1, 2), (3, 4)
  • gtgtgt L11
  • (3, 4)
  • gtgtgt L110
  • 3
  • gtgtgt L1100
  • 3

5
Shared references
  • Assignments store references to objects, not
    copies.
  • gtgtgt X 1, 2, 3
  • gtgtgt L 'a', X, 'b'
  • gtgtgt D 'x'X, 'y'2

6
Shared references
  • Since lists are mutable...
  • gtgtgt X1 'surprise' changes all three
    references!
  • gtgtgt L
  • 'a', 1, 'surprise', 3, 'b'
  • gtgtgt D
  • 'x' 1, 'surprise', 3, 'y' 2

7
Comparisons, Equality, and Truth
  • gtgtgt L1 1, ('a', 3) same value,
    unique objects
  • gtgtgt L2 1, ('a', 3)
  • gtgtgt L1 L2, L1 is L2 equivalent?,
    same object?
  • (1, 0)
  • The operator tests value equivalence
  • The is operator tests object identity

8
Comparisons, Equality, and Truth
  • gtgtgt L1 1, ('a', 3)
  • gtgtgt L2 1, ('a', 2)
  • gtgtgt L1 lt L2, L1 L2, L1 gt L2 less, equal,
    greater a tuple of results
  • (0, 0, 1)

9
Comparisons, Equality, and Truth
  • In general, Python compares the types we've seen
    as follows
  • Numbers are compared by relative magnitude.
  • Strings are compared lexicographically,
    character-by-character ("abc" lt "ac").
  • Lists and tuples are compared by comparing each
    component, from left to right.

10
Special cases
  • Assignment creates references, not copies
  • gtgtgt L 1, 2, 3
  • gtgtgt M 'X', L, 'Y' embed a reference
    to L
  • gtgtgt M
  • 'X', 1, 2, 3, 'Y'
  • gtgtgt L1 0 changes M too
  • gtgtgt M
  • 'X', 1, 0, 3, 'Y'

11
Special cases
  • solution
  • gtgtgt L 1, 2, 3
  • gtgtgt M 'X', L, 'Y' embed a copy of L
  • gtgtgt L1 0 only changes L,
    not M
  • gtgtgt L
  • 1, 0, 3
  • gtgtgt M
  • 'X', 1, 2, 3, 'Y'

12
Special cases
  • Repetition
  • gtgtgt L 4, 5, 6
  • gtgtgt X L 4 like 4, 5, 6 4, 5,
    6 ...
  • gtgtgt Y L 4 L L ... L,
    L,...
  • gtgtgt X
  • 4, 5, 6, 4, 5, 6, 4, 5, 6, 4, 5, 6
  • gtgtgt Y
  • 4, 5, 6, 4, 5, 6, 4, 5, 6, 4, 5, 6

13
Special cases
  • gtgtgt L1 0 impacts Y but not X
  • gtgtgt X
  • 4, 5, 6, 4, 5, 6, 4, 5, 6, 4, 5, 6
  • gtgtgt Y
  • 4, 0, 6, 4, 0, 6, 4, 0, 6, 4, 0, 6
  • solution
  • gtgtgt Y L 4
  • instead of
  • gtgtgt Y L 4

14
Special cases
  • Immutable types can't be changed in place
  • T (1, 2, 3)
  • T2 4 error!
  • T T2 (4,) okay (1, 2, 4)
Write a Comment
User Comments (0)
About PowerShow.com