Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Python: Day Two

Similar presentations


Presentation on theme: "Introduction to Python: Day Two"— Presentation transcript:

1 Introduction to Python: Day Two
Stephanie J Spielman, PhD Big data in biology summer school, 2018 Center for computational biology and bioinformatics University of Texas at austin

2 Recap: we have learned…
Working with several variable types Integer, float, boolean, string, list Printing our code Evaluating with if statements

3 iteration is our other control flow tool
Iteration performs the same code repeatedly Two flavors: For-loops iterate a pre-specified number of times While-loops iterate while a logical condition remains True

4 iterating with for-loops
Two basic uses: Perform a task on each item in a list, dictionary, etc. Perform a task a certain number of times

5 Iterating over lists item is the loop variable.
# Generic list some_list = [...items...] # Loop over the list for item in some_list: ... ... Do these commands for each item ... Back to the rest of the script item is the loop variable. Takes on a new value for each iteration The name is arbitrary

6 Iterating over lists item is the loop variable.
# Generic list some_list = [...items...] # Loop over the list for blahblahblah in some_list: ... ... Do these commands for each item ... Back to the rest of the script item is the loop variable. Takes on a new value for each iteration The name is arbitrary

7 Iterating over lists item is the loop variable.
# Generic list some_list = [...items...] # Loop over the list for x in some_list: ... ... Do these commands for each item ... Back to the rest of the script item is the loop variable. Takes on a new value for each iteration The name is arbitrary

8 iterating over lists Example: curving grades
# List of grades grades = [88, 71, 74, 83, 57, 79, 66] # Loop over the grades for grade in grades: print(grade * 1.1) 96.8 78.1 81.4 91.3 62.7 86.9 72.6

9 iterating over lists Example: curving grades
# List of grades grades = [88, 71, 74, 83, 57, 79, 66] # Define an empty list of new grades to populate new_grades = [] # Loop over the grades and save curved grade for grade in grades: new = grade * 1.1 new_grades.append(new) print(new_grades) [96.8, 78.1, 81.4, 91.3, 62.7, 86.9, 72.6]

10 but no hard-coding!!! # List of grades
# Define an empty list of new grades to populate new_grades = [] curve = 1.1 # Loop over the grades and save curved grade for grade in grades: new = grade * curve new_grades.append(new) print(new_grades) [96.8, 78.1, 81.4, 91.3, 62.7, 86.9, 72.6]

11 use a counter variable to keep track of loop
curve = 1.1 for grade in grades: print("Iteration", i) print(grade * curve) i += 1 Iteration 0 96.8 Iteration 1 78.1 Iteration 2 81.4 Iteration 3 91.3 Iteration 4 62.7 ...

12 iterating a certain number of times
Use the range() function This function takes the same arguments as indexing with [ ] for x in range(5): print(x) 1 2 3 4 for x in range(2,8): print(x, "squared is", x**2) 2 squared is 4 3 squared is 9 4 squared is 16 5 squared is 25 6 squared is 36 7 squared is 49

13 iterating a certain number of times
curve = 1.1 for grade in grades: print(grade * curve) 96.8 78.1 81.4 91.3 62.7 86.9 72.6 for i in range(len(grades)): print(grades[i] * curve) 96.8 78.1 81.4 91.3 62.7 86.9 72.6

14 looping over strings for s in "python": print(s) p y t h o n

15 exercise break

16 another data type: dictionaries
Dictionaries are defined with braces: {} Contain key:value pairs Known in other contexts as "associative arrays" # Define a dictionary of names names = {"Stephanie": "Spielman", "Dariya": "Sydykova", "Claus": "Wilke"} # Each key:value pair is a single item print(len(names)) 3 # Index dictionaries via *keys* (not position!!) print(names["Claus"]) "Wilke"

17 another data type: dictionaries
names = {"Stephanie": "Spielman", "Dariya": "Sydykova", "Claus": "Wilke"} # Add a key:value pair to a dictionary and print to confirm names["Bob"] = "Smith" print(names) {'Claus': 'Wilke', 'Bob': 'Smith', Stephanie':'Spielman', 'Dariya': 'Sydykova'} Did you expect this output?

18 dictionaries are unordered
Unique key:value pairs are *always* preserved, but their order is not One of many reasons why we index with keys, not positions

19 dictionary Keys must be unique
Keys must be unique, but values may be repeated: Adding an existing key will overwrite the original: acceptable_dict = {"a": 5, "b": 3, "c": 5} acceptable_dict["a"] = 7 print(acceptable_dict) {"a": 7, "c": 5, "b": 3}

20 Common dictionary methods
medals = {"gold": "first", "silver": "second", "bronze": "third"} # The .keys() method returns an iterator* of dictionary keys print(medals.keys()) dict_keys(['gold', 'silver', 'bronze']) print(medals.keys()[0]) TypeError: 'dict_keys' object does not support indexing print(list(medals.keys())[0]) 'gold' print(list(medals.values()) ##.values() -> the values ['third', 'second', 'first'] print(list(medals.items()) ##.items() -> tuples of (key,value) [('gold', 'first'), ('silver', 'second'), ('bronze', 'third')]

21 looping over dictionaries
price = {"banana" : 0.79, "apple": 1.02, "bell pepper": 2.39} for item in price: print(item) bell pepper banana apple What are we actually looping over?

22 looping over dictionaries
price = {"banana" : 0.79, "apple": 1.02, "bell pepper": 2.39} for item in price: # Print the key *and* value print( item, price[item] ) bell pepper 2.39 banana 0.79 apple 1.02 # Getting fancier for x in price.items(): print( x[0], x[1] ) # Even fancier! for (x, y) in price.items(): print( x, y ) bell pepper 2.39 banana 0.79 apple 1.02

23 exercise break

24 controlling the loops even more
Two statements change loop flow: continue immediately start the next iteration and skip remaining loop statements break immediately exit out of loop entirely

25 the continue statement
codons = ["ATT", "GAT", "NNA", "ANG", "NTT", "ATG"] # Print unambiguous codons only i = 0 for seq in codons: i += 1 if "N" in seq: continue # Immediately start next iteration print("loop iteration count:", i) print("The sequence is ", seq) print() # Prints a newline, for clarity loop iteration count: 1 The sequence is ATT loop iteration count: 2 The sequence is GAT loop iteration count: 6 The sequence is ATG

26 the break statement NB: these are essentially required for while-loops
codons = ["ATT", "GAT", "NNA", "ANG", "NTT", "ATG"] # Print unambiguous codons only i = 0 for seq in codons: i += 1 if "N" in seq: print("Oh no, ambiguities! I'm gonna stop.") break # Immediately exit print("loop iteration count:", i) print("The sequence is ", seq) print() # Prints a newline, for clarity print("Outside of the loop now.") loop iteration count: 1 The sequence is ATT loop iteration count: 2 The sequence is GAT "Oh no, ambiguities! I'm gonna stop." Outside of the loop now. NB: these are essentially required for while-loops

27 using if and for together
# List of grades grades = [88, 71, 74, 83, 57, 79, 66] # Empty list of letter letter_grades = [] # Determine the letter grade for grade in grades: if grade >= 90: letter_grades.append("A") elif grade >= 80: letter_grades.append("B") elif grade >= 70: letter_grades.append("C") elif grade >= 60: letter_grades.append("D") else: letter_grades.append("F") print(letter_grades) ['B', 'C', 'C', 'B', 'F', 'C', 'D'] This code WORKS, but I see a problem. What is it?

28 This is complex! Don't freak out
# List of grades grades = [88, 71, 74, 83, 57, 79, 66] # Dictionary of grade BOUNDARIES as letter: [lower inclusive, upper exclusive] grade_bounds = {"A": [90, 101], "B": [80, 90], "C": [70, 80], "D": [60, 70], "F":[0,60] } # Empty list of letter letter_grades = [] # Determine the letter grade for grade in grades: for bound in grade_bounds: if grade >= grade_bounds[bound][0]] and grade < grade_bounds[bound][1]]: letter_grades.append(bound) print(letter_grades) ['B', 'C', 'C', 'B', 'F', 'C', 'D']

29 A handy dictionary technique
The task: Count the number of each character in a string, by creating a dictionary, key:value::character:count. The catch: You don't know beforehand what characters are there! string = "supercalifragilisticexpialidocious" string_counts = {} for item in string: if item in string_counts: string_counts[item] += 1 else: string_counts[item] = 1 print(string_counts) {'s': 3, 'u': 2, 'p': 2, 'e': 2, 'r': 2, 'c': 3, 'a': 3, 'l': 3, 'i': 7, 'f': 1, 'g': 1, 't': 1, 'x': 1, 'd': 1, 'o': 2}

30 exercise break


Download ppt "Introduction to Python: Day Two"

Similar presentations


Ads by Google