Presentation is loading. Please wait.

Presentation is loading. Please wait.

Nate Brunelle Today: Slicing, Debugging, Style

Similar presentations


Presentation on theme: "Nate Brunelle Today: Slicing, Debugging, Style"— Presentation transcript:

1 Nate Brunelle Today: Slicing, Debugging, Style
CS1110 Nate Brunelle Today: Slicing, Debugging, Style

2 Questions?

3 Last Time While loops For loops

4 Collections Order doesn’t matter, No Repetition
Order Matters, Repetition ok Examples: String List Tuple Range Counting starts at 0 collection[index] gives a specific value in the collection Order doesn’t matter, No Repetition Examples: Set Dict

5 Range range(x) range(x,y)
Gives all integers from 0 (inclusive) to x (exclusive) range(5) -> 0,1,2,3,4 range(x,y) Gives all integers from x (inclusive) to y (exclusive) Range (2, 6) -> 2,3,4,5 [lower, upper)

6 hello Strings Collection of characters Order Matters Repetition ok
X = “hello” X[0] -> h hello 1 2 3 4 Indices Indexing

7 More on Indexing s = ‘bananarama’ Indices are always int
S[1.0] -> error Start counting indices at 0: S[0] -> ‘b’ S[1] -> a S[2] -> n …. len(s) gives the number of things in the collection len(s) -> 10 The last thing in s can be identified by: s[len(s)-1] Use negative indices to index from the end S[-1] is the last thing S[-2] is the second from last thing S[-x] is the same as s[len(s)-x]

8 Slicing s = ‘bananaarama’ Allows you to refer to a range of indices
Give a start index and an end index Evaluates to a new collection from start (inclusive) to end (exclusive) s[start:end] s[2:5] -> ‘nan’ S[2:len(s)] -> ‘nanarama’ s[start:] Goes from start index (inclusive) to the end of the collection s[2:] -> ‘nanarama’ S[x:] the same as s[x:len(s)-1] s[:end] Goes from the beginning of the collection to the end index (exclusive) S[:5] -> ‘banan’ S[:x] the same as s[0:x] s[start:end:step] Extended slice Goes from the start index (incluvie) to the end index(exclusive), but keeping only every stepth thing

9 Debugging “bug”- an error in your code

10 Test Case Selection Exceptional Cases: Equivalence Classes: Edge Cases
What happens when we get bad arguments? Not important yet Equivalence Classes: Groups of things such that if our code behaves correctly on one of them, it will behave correctly on the whole group Edge Cases Boundary between Equivalence Classes Tip: Make sure you know the answer! Pow(1.479, )

11 Equivalence Classes for Absolute Value
1.0 8.5 8 13.2 Positive 13 0.0 -8 -8.5 Negative -13 -13.2 int float -1.0

12 Equivalence Classes for Median?
median(a,b,c) Equivalence Classes: Positive values, negative values a>b>c vs b>a>c .. A == b == c

13 Which thing in the middle?
b smaller a smaller a bigger a b bigger b a bigger a smaller c

14 Style You should write code to be read by humans Docstrings Names Pep8
Have information at beginning of functions to describe what it does Names A name should describe the meaning Can be hard Make sure there is one meaning per variable Pep8 Other Python style guidelines Talks about things that aren’t names or docstrings, but make code ugly


Download ppt "Nate Brunelle Today: Slicing, Debugging, Style"

Similar presentations


Ads by Google