Dictionaries.   Review on for loops – nested for loops  Dictionaries (p.79 Learning Python)  Sys Module for system arguments  Reverse complementing.

Slides:



Advertisements
Similar presentations
ResponseCard XR Creating Tests and Homework ®. Navigating the Menu Press the MENU button to bring up the Main Menu. Press the Down Arrow twice to select.
Advertisements

Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Title An Introduction to Python. What is Python exactly? Python is a modern rapid development language. Code is very clean and easy to read. Emphasizes.
Sequences A sequence is a list of elements Lists and tuples
An Introduction to Python – Part II Dr. Nancy Warter-Perez April 21, 2005.
Chapter 2 Writing Simple Programs
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
Python Control of Flow.
Python programs How can I run a program? Input and output.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
The if statement and files. The if statement Do a code block only when something is True if test: print "The expression is true"
Python Data Structures
Data Structures in Python By: Christopher Todd. Lists in Python A list is a group of comma-separated values between square brackets. A list is a group.
Handling Lists F. Duveau 16/12/11 Chapter 9.2. Objectives of the session: Tools: Everything will be done with the Python interpreter in the Terminal Learning.
CS 127 Writing Simple Programs.  Stages involved  Analyze the problem  Understand as much as possible what is trying to be solved  Determine Specifications.
Sorting and Modules. Sorting Lists have a sort method >>> L1 = ["this", "is", "a", "list", "of", "words"] >>> print L1 ['this', 'is', 'a', 'list', 'of',
1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.
Python Lists and Such CS 4320, SPRING List Functions len(s) is the length of list s s + t is the concatenation of lists s and t s.append(x) adds.
Test Automation For Web-Based Applications Portnov Computer School Presenter: Ellie Skobel.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Intro Python: Variables, Indexing, Numbers, Strings.
Built-in Data Structures in Python An Introduction.
An Introduction to Python – Part II Dr. Nancy Warter-Perez.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
9/23/2015BCHB Edwards Advanced Python Data Structures BCHB Lecture 7.
9/28/2015BCHB Edwards Basic Python Review BCHB Lecture 8.
Jim Havrilla. Invoking Python Just type “python –m script.py [arg]” or “python –c command [arg]” To exit, quit() or Control-D is used To just use the.
GE3M25: Computer Programming for Biologists Python, Class 5
2. WRITING SIMPLE PROGRAMS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
Perl for Bioinformatics Part 2 Stuart Brown NYU School of Medicine.
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
Introduction to Programming Oliver Hawkins. BACKGROUND TO PROGRAMMING LANGUAGES Introduction to Programming.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Tips. Iteration On a list: o group = ["Paul","Duncan","Jessica"] for person in group: print(group) On a dictionary: o stock = {'eggs':15, 'milk':3, 'sugar':28}
Lists/Dictionaries. What we are covering Data structure basics Lists Dictionaries Json.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
DAY 3. ADVANCED PYTHON PRACTICE SANGREA SHIM TAEYOUNG LEE.
Python Fundamentals: Complex Data Structures Eric Shook Department of Geography Kent State University.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
Lecture III Syntax ● Statements ● Output ● Variables ● Conditions ● Loops ● List Comprehension ● Function Calls ● Modules.
Chapter 2 Writing Simple Programs
String and Lists Dr. José M. Reyes Álamo.
Python: Experiencing IDLE, writing simple programs
Module 5 Working with Data
Introduction to Python
Advanced Python Data Structures
Topics Introduction to Repetition Structures
Advanced Python Data Structures
Psuedo Code.
Topics Introduction to File Input and Output
An Introduction to Python
Basic Python Review BCHB524 Lecture 8 BCHB524 - Edwards.
CS190/295 Programming in Python for Life Sciences: Lecture 6
Data Structures – 1D Lists
String and Lists Dr. José M. Reyes Álamo.
Python programming exercise
Advanced Python Data Structures
Topics Introduction to File Input and Output
Python Basics with Jupyter Notebook
Basic Python Review BCHB524 Lecture 8 BCHB524 - Edwards.
Chopin’s Nocturne.
Topic: Loops Loops Idea While Loop Introduction to ranges For Loop
Topics Introduction to File Input and Output
Lists Like tuples, but mutable. Formed with brackets: Assignment: >>> a = [1,2,3] Or with a constructor function: a = list(some_other_container) Subscription.
Presentation transcript:

Dictionaries

  Review on for loops – nested for loops  Dictionaries (p.79 Learning Python)  Sys Module for system arguments  Reverse complementing a DNA sequence  Using the range function for for loops Agenda

  For loops allow us to iterate through ordered sequence objects like strings and lists.  Nested for loops can contain if statements and other for loops. You must indent in order to nest.  Type and press enter:  list1 = [‘a’, ‘b’, ‘c’, ‘d’]  list2 = [‘1’,’2’]  for letter in list1:  for number in list2:  print(letter+number) Review

 What’s happening in our nested for loop? Output: ‘a1’ ‘a’ ‘b’ ‘c’ ‘d’ ‘1’ ‘2’ list1 = [‘a’, ‘b’, ‘c’, ‘d’] list2 = [‘1’,’2’] for letter in list1: for number in list2: print(letter+number)

 What’s happening in our nested for loop? Output: ‘a2’ ‘a’ ‘b’ ‘c’ ‘d’ ‘1’ ‘2’ list1 = [‘a’, ‘b’, ‘c’, ‘d’] list2 = [‘1’,’2’] for letter in list1: for number in list2: print(letter+number)

 What’s happening in our nested for loop? Output: ‘b1’ ‘a’ ‘b’ ‘c’ ‘d’ ‘1’ ‘2’ list1 = [‘a’, ‘b’, ‘c’, ‘d’] list2 = [‘1’,’2’] for letter in list1: for number in list2: print(letter+number)

 What’s happening in our nested for loop? Output: ‘b2’ ‘a’ ‘b’ ‘c’ ‘d’ ‘1’ ‘2’ list1 = [‘a’, ‘b’, ‘c’, ‘d’] list2 = [‘1’,’2’] for letter in list1: for number in list2: print(letter+number)

 What’s happening in our nested for loop? Output: ‘c1’ ‘a’ ‘b’ ‘c’ ‘d’ ‘1’ ‘2’ list1 = [‘a’, ‘b’, ‘c’, ‘d’] list2 = [‘1’,’2’] for letter in list1: for number in list2: print(letter+number)

 What’s happening in our nested for loop? Output: ‘c2’ ‘a’ ‘b’ ‘c’ ‘d’ ‘1’ ‘2’ list1 = [‘a’, ‘b’, ‘c’, ‘d’] list2 = [‘1’,’2’] for letter in list1: for number in list2: print(letter+number)

 What’s happening in our nested for loop? Output: ‘d1’ ‘a’ ‘b’ ‘c’ ‘d’ ‘1’ ‘2’ list1 = [‘a’, ‘b’, ‘c’, ‘d’] list2 = [‘1’,’2’] for letter in list1: for number in list2: print(letter+number)

 What’s happening in our nested for loop? Output: ‘d2’ ‘a’ ‘b’ ‘c’ ‘d’ ‘1’ ‘2’ list1 = [‘a’, ‘b’, ‘c’, ‘d’] list2 = [‘1’,’2’] for letter in list1: for number in list2: print(letter+number)

  A quick way to iterate without using a for loop  Type and press enter:  newlist = []  for letter in list1:  newlist.append(letter*2)  newlist2 = [letter*2 for letter in list1]  The contents of newlist and newlist2 should be the same  Type and press enter:  combine = []  for letter in list1:  for number in list2:  combine.append(letter+number)  combine2 = [letter+number for number in list2 for letter in list1] List Comprehensions

  Note the curly brackets for dictionaries  Unordered collection of objects (unlike lists which are ordered) comprised of key:value pairs  Dictionaries unlike lists are indexed by key and not by numeric position  Like a list, a dictionary can shrink and grow  Like a list, the contents of a dictionary can be modified  Like a list, a dictionary can be nested  The keys of a dictionary can only be objects that cannot grow and shrink, so keys can’t be lists or dictionaries  The vlaues of a dictionary can be any object {Dictionaries}

  Type and press enter:  D = {'food': 'Spam', 'quantity': 4, 'color': 'pink'}  D['food’]  D[1]  Why does D[1] give an error? Indexing Dictionaries D = {'food': 'Spam', 'quantity': 4, 'color': 'pink'} key value keyvalue key value

  Type and press enter:  newD = {}  newD  newD['name'] = 'Bob’  newD['job'] = 'dev’  newD['age'] = 40  newD  newD['name’]  Notice that newD now contains key:value assignments We can also create empty dictionaries and fill them one by one

  for item in D.keys():  print item  What is this printing, the keys or values?  for item in D.keys():  print D[item]  What is this printing, the keys or values?  for key,value in D.iteritems():  print(key, value)  Trick question: What is this printing, the keys or values? How to iterate through dictionaries

  We have a DNA sequence where we want to find the reverse complement. Not just that, but we want a script that we can reuse for all DNA sequences without having to modify the script every time.  First, we need to make a script that can accept system arguments (arguments you give on the command line when you run the script)  Behold, the python module sys to give input to a script  Modules or packages provide extra functionality to python’s pre-built functions  modules must be imported in each python session in order to access their functions Reverse Complementing DNA

 Create a python script that looks like this:  Run your script by typing into the command line:  python system_input.py hello world  If you are using spyder type in the console:  %run system_input.py hello world  Modify your script so that the output is hello world  Index sys.argv to get just hello world  Convert your resulting list to a string (google list to string python)  (this is the ‘’.join() method)  import sys  repeat_after_me = sys.argv[:]  print(repeat_after_me) system_input.py

  1. Now that we know how to give a script system arguments, we need to convert our DNA sequence into it’s complement:  a. use a practice sequence within your script: ie sequence = 'atcggacttca’  b. We should store the DNA sequence as a variable  c. We should reverse the sequence of the dna  d. We should create a dictionary of complementary bases  e. We should iterate through the dna sequence. Within our for loop, we need to index the dictionary with our dna nucleotides and pull out the value for the keys.  f. We should store these values from the dictionary to an empty list  2. We need to convert the list to a string  3. Erase your practice sequence and give your sequence as a system command  The answer we should get if our script worked is: tgaagtccgat Breaking our problem down

  import sys  sequence = str(sys.argv[1])  newseq = sequence[::-1]  dnadict = {'a':'t', 't':'a', 'c':'g', 'g':'c'}  compseq = []  for item in newseq:  compseq.append(dnadict[item])  newseq2 = ''.join(compseq)  print(newseq2) The Answer

  You don’t have to go through every item. You can write a for loop to iterate by numeric position. Remember your indexing (start, end, step)  Type and press enter:  list1 = [‘a’, ‘b’, ‘c’, ‘d’]  len(list1)  range(0, len(list1))  range(0, len(list1), 2) One other thing about iterating….

  This for loop will iterate through range of list1 or the index of list1, type and press enter:  for i in range(0, len(list1), 2):  print(i)  This for loop will iterate through list1 by indexing list1 using i, type and press enter:  for i in range(0, len(list1), 2):  print(list1[i])  This for loop will iterate through list1 but pull out multiple indices, type and press enter:  for i in range(0, len(list1),2):  list1[i:i+2] One other thing about iterating….

  You have a long nucleotide sequence. You want to translate that sequence into amino acids. And, you want your script to work for any nucleotide sequence you give it in the future. I, being the benevolent teacher, will give you the code for the nucleotide and amino acids. However, you must make the dictionary!  You can use this as your practice sequence which you will give as a system argument once you are finished, while building your script write this sequence into the script saved as a variable so that you can make sure your code works: atcggacttcat  Your answer should be: IGLH Homework

  bases = ['t', 'c', 'a', 'g']  codons = [a+b+c for a in bases for b in bases for c in bases]  amino_acids = 'FFLLSSSSYY**CC*WLLLLPPPPHHQQRRRRIIIMTTTTNNKKSSRRVVVVAAAADDEEGGGG’  #* this means stop codon Code to get you started

  1. Create a dictionary from the important lists given in the code provided. I have not taught you how to do that. You must figure out which lists from the previous code are important for your dictionary and how to convert these two lists into a dictionary.  Hint: Google “create dictionary from two lists python” – usually stack overflow will give the most understandable answers to google questions.  Hint2: It’s probably easier if your nucleotide list comprises your dictionary keys and the amino acid list will be values for your keys. Steps to break down the problem

  2. Create a for loop that iterates by position so that it iterates in steps or intervals of 3 (you are using this to break your DNA string into codons)  3. Within your for loop assign each group of 3 nucleotides to a variable (remember how we indexed using range method?)  4. Use the variable that you created in step 3 to index your dictionary within your for loop  5. You can save the results of your for loop as a list  6. Use the join method to join your list into a string and print this as output  7. Delete your example sequence variable from your script and give to your script as a system argument Steps to break down the problem

 Your Answer Should Look Like: