Presentation is loading. Please wait.

Presentation is loading. Please wait.

Complex Data Types and Flow Control

Similar presentations


Presentation on theme: "Complex Data Types and Flow Control"— Presentation transcript:

1 Complex Data Types and Flow Control
June 25, 2015

2 Part 1: Complex data types

3 Part 1 Overview Ordered types Unordered types Array/list String Set
Hash/map/dictionary

4 Arrays, vectors, and lists
Ordered collection of values or variables Mutable: you can (generally) change the values in the array after you have created it Various implementations All one type or mixed type (lists typically mixed, others not) Fixed length or variable length Indexed from 0 (Python) or 1 (R)

5 What’s the difference? Sometimes these terms will be used interchangeably, but often: An array will have a fixed number of values all of the same type A vector will have a variable number of values all of the same type A list will have a variable number of values of any type (possibly mixed)

6 Creating a vector/list
Initialize with values my_list = [1, 2, 3, 4] my_list = c( 1, 2, 3, 4 ) Initialize without values and append my_empty_list = [] Functions that create lists “Hello world”.split() ->[ “Hello”, “world” ] range(5) -> [0, 1, 2, 3, 4] x = c( 1, 2) rep( x, length.out=4 ) -> [1, 2, 1, 2] “Slicing” an existing list my_list[0:2] -> [1, 2] (EXCLUDES LAST ELEMENT) my_list[1:2] -> [1, 2] (INCLUDES LAST ELEMENT)

7 Basic array operations
Accessing data: Use brackets or parentheses (language-dependent) and the index number of the element you want to access >>myArray = [2, 4, 6, 8, 10] >>myArray[3] 8 Setting values: Same as accessing data, but set it equal to a value >>myArray[3] = 100 >>print myArray [2, 4, 6, 100, 10] Other operations vary (appending, deleting entries, etc.)

8 Arrays of Arrays (of arrays . . . )
Two-dimensional (and higher) data structures can be created by nesting data structures Elements can be accessed by indexing the outer array and then the inner array Example: my_matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] my_matrix[0][1] -> 2

9 Strings Sequence of characters enclosed in single or double quotation marks (language-dependent) Python: similar operations to arrays Indexed from 0 Access individual characters by indexing, substrings by slicing In many languages, strings are immutable, meaning they can’t be changed in place (i.e. myString[3] = ‘a’ won’t work). Concatenation: “race” + “car” = “racecar” Spaces aren’t added automatically—you have to tell the computer where they go! R: String-specific operations Indexed from 1 Access individual characters or substrings using the substr() function substr(“string”, 1, 1) -> “s” substr(“string”, 2, 4) -> “tri” Concatenation: paste( “race”, “car”, sep=“” ) -> “racecar” By default, a space would be added between the words! paste(“race”, “car” ) -> “race car”

10 Sets Values stored and accessed in no particular order
Each value appears only once in the set Very fast to check whether a value is in the set Objects can be added or removed, but are not placed at a particular index Common set operations (for sets A and B) Union: items found in A, B, or both ( A or B) Intersect: items found in both A and B ( A and B ) Difference: Items in one set but not the other B – A = set of items in B but not in A Symmetric difference = set of items in A or B but not both (A – B) + (B – A)

11 Set Operations Explained
Set A Set B A - B B - A Intersect UNION Symmetric Difference = (A – B) + (B – A)

12 Hash tables, maps, dictionaries
Set of keys (constant value, like a word in a dictionary) paired with values Keys must be immutable Values can be any type including numeric types, strings, arrays, or even other dictionaries Keys stored in no particular order

13 Using dictionaries Values are indexed by their keys for accessing/setting >>myDictionary = {‘first’:1, ‘second’:2, ‘third’:3} >>myDictionary[‘first’] 1 >>myDictionary[‘first’] = 25 >>print myDictionary Note that they aren’t in {'second': 2, 'third': 3, 'first': 25} their original order For a dictionary of lists, you can index sequentially >>> myDictionary = {'first':[1,2,3], 'second':[10,20,30], 'third':[100,200,300]} >>> myDictionary['first'][2] 3 **This also works for lists of lists, dictionaries of dictionaries, etc.

14 When will I use unordered structures?
Sets Dictionaries Keep track of what and how many unique sequences are in a file (but not # of replicates) mySet = set() for sequence in file: mySet.insert(sequence) #If sequence is already in #mySet, nothing happens For two files, quickly find which sequences appear in both: inBoth = mySet1.intersection(mySet2) Map codons to amino acids for mRNA translation myCodons = {“AUG”:”M’, “AAA”:”K”, “UUU”:F”} Keep running counts of how often each unique sequence appears in a file You could even store lists of the line numbers where they appear: myDict[sequence].append(lineNumber)

15 What if I want something more complicated/specific?
Some languages allow you to define structures (struct) which store some set of pre-specified variables Object-oriented languages (like Python) let you define classes, which contain both data specific to the class and special functions (methods) that belong to that class

16 Part 2: Control structures

17 Flow control topics Logic and logical order of operations
If–else if (or elif, or elsif)–else Loops For While (pre-test) Do-while (post-test) Statements to affect flow control Break Continue Pass Functions Try/Except

18 Basic Logical Comparisons
Mathematical comparisons <, >, <=, >=, ==, != **Don’t confuse equality (==) and assignment (=)** Logical operations x OR y (x||y, x | y, x or y): Inclusive or (x is true or y is true or both) x AND y (x && y, x & y, x and y): x and y are both true NOT x (!x, not x): x is not true

19 Logical order of operations
Not has higher priority than And which has higher priority than Or Parentheses can be used to give a grouping priority (like in mathematical order of operations) Examples x and y or y and z: Either both x and y are true OR both y and z are true (or they are all true); (x and y) or (y and z) not x and y or y and z: Either 1) x is false and y is true or 2) y and z are both true (or both); ((not x) and y) or (y and z) not (x or y) = not x and not y not (x and y) = not x or not y

20 If statements if( x ): some code more code else: some different code end x can be a variable, comparison, or any combination of variables/comparisons with and/or/not The truth value of x is evaluated If x is true, the code in red is executed, and the code in blue is skipped If x is false, the code in red is skipped, and the code in blue is executed

21 Else-if You can use this structure to test multiple cases:
if( x ): some code more code else if (y): some different code else if (z): still more code else if (x and z): this code will never run else: code if all else fails Code in red runs if x is true All subsequent “else” and “else if” blocks are skipped—the conditions are never tested Code in blue runs if x is false and y is true Code in green runs if x and y are false and z is true Code in purple will never run Code in orange runs if x, y, and z are false

22 If statements don’t have to have an else
if (x): code if (x or z): else if (y): code that always runs Code in red runs if x is true Code in blue runs if x is true OR z is true Note this is independent of the first test Code in green runs if y is true and x and z are false This else if belongs to the second if statement

23 Loops A loop is a section of code that will run repeatedly until some condition is met Types of loops you may see Pre-test loops (“while” or “until”) Post-test loops(“do … while” or “do … until”) For loops

24 Pre-test loops while(x): code more code code that affects x code that runs once x is no longer true x is tested before the loop runs If x is false to begin with, the loop never executes Each time the loop completes, x is tested again to determine whether the loop will run again What about until(x)? Same as while(not x) What if x never changes? Infinite loop!

25 Post-test loops (Not available in some languages)
do: code more code code that affects x while(x) code that runs once x is no longer true x is tested after the loop executes What difference does this make? Both can accomplish the same thing If x is false the first time you reach the loop, a while loop will not run at all, but a do-while loop will run at least once Pre-test loops are much more common

26 For Loops x = [1, 2, 3, 4, 5, 6, 7, 8] For loop version: for value in x: print value While loop version: index = 0 while(index < length(x)) print x[index] index = index + 1 For loops are essentially specialized while loops designed to loop through a data structure (lists, strings, etc.) Number of iterations generally known before the loop begins Much harder to write an infinite for loop (but still possible)

27 Infinite loops while(true): This code will run forever Forever and ever Over and over again Until you manually kill it (Control-C) Unless you have another way out code that will never run

28 Break and Continue statements
Use break when you want to completely leave (‘break out’) of a loop while (True): x = x + 1 if (x > 6): break y = y / 2 x = x + y code that runs once x > 6 Use continue when you want to skip to the next iteration of a loop Always tests the condition before continuing while (x > 0): if (y > 5): x = x – 1 continue x = x – 2 #This only executes if #y <= 5

29 Exception handling (sneak preview)
try: some code except <optional type>: some different code runs if & only if red code throws an exception finally: code that runs no matter what (even if you quit with an exception) additional code Example: x = 0 y = 2 try: z = y/x except ZeroDivisionError: print “You can’t do that!” raise except: print “Something went wrong!” finally: print “Goodbye!”

30 Using exception handling correctly
If your code throws an exception, you often don’t want to continue Use raise to throw an exception and exit the program Don’t use try…except when another flow control method would be more appropriate! x = [1, 2, 3] index=0 while True: try: print x[index] except IndexError: break x = [1, 2, 3] for value in x: print value

31 Functions (preview) A function lets you store a block of code that you’ll use repeatedly (but not necessarily in a loop) Often take in values and return (output) a different value (like a mathematical function) Some functions take no input and/or produce no output

32 Functions function addTheseNumbers(x, y, z): return x + y + z function printMessage(): print “This function”, print “returns nothing.” function appendToList(list) list.append(‘more’) a = 2 #We actually start here!! b = 7 c = 1000 myList = [‘f’, ‘g’, ‘h’] z = addTheseNumbers(a, b, c) print z #This should print 1009 printMessage() appendToList(myList) #myList now contains [‘f’, ‘g’, ‘h’, ‘more’] When it reaches the function call (in the code below), the program gets instructions from the function definition (above) x, y, and z are the parameters of the function a, b, and c are the arguments The contents of the arguments are assigned to the parameters The function ends when it reaches the return statement

33 What will be the output? What is the first thing this program does?
def translate(mySequence): #Assume this contains all the codons myCodons = {“AAA”:”K”, “AAC”:”N”, “UUU”:”F”} myProtein = “” start = mySequence.find(“AUG”) for i in range(start, len(mySequence)-2, 3): #HINT: range(start, stop, step) makes a list of numbers #from start to (stop – 1) in increments of step if myCodons[ mySequence[ i:i+3] ] == “STOP”: break myProtein += myCodons[ mySequence[ i:i+3 ] ] return myProtein transcript_1 = “AUGAUCCCUUUAUAGAG” transcript_2 = “ACUUAUGCAUGATCAUUGACAAAAAA” print “Transcript 1 translates to”, translate(transcript_1) print “Transcript 2 translates to”, translate(transcript_2) What is the first thing this program does? What does the function do? What is myCodons and what is it used for? What is the purpose of the break statement? What happens if the sequence contains a stop codon? If it doesn’t contain a stop codon?


Download ppt "Complex Data Types and Flow Control"

Similar presentations


Ads by Google