Introduction to Python: Day Two

Slides:



Advertisements
Similar presentations
INTRODUCTION TO PYTHON PART 3 - LOOPS AND CONDITIONAL LOGIC CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
Advertisements

Python Control of Flow.
Python Control Flow statements There are three control flow statements in Python - if, for and while.
Test Automation For Web-Based Applications Portnov Computer School Presenter: Ellie Skobel.
University of Palestine software engineering department Introduction to data structures Control Statements: Part 1 instructor: Tasneem Darwish.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
Ch. 10 For Statement Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
9/14/2015BCHB Edwards Introduction to Python BCHB Lecture 4.
Loops & List Intro2CS – week 3 1. Loops -- Motivation Sometimes we want to repeat a certain set of instructions more than once. The number of repetitions.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
ITERATION. Iteration Computers are often used to automate repetitive tasks. Repeating identical or similar tasks without making errors is something that.
Introduction to Computing Using Python Repetition: the for loop  Execution control structures  for loop – iterating over a sequence  range() function.
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.
Introduction to python programming
String and Lists Dr. José M. Reyes Álamo.
Intro to CS Nov 21, 2016.
Introduction to Python
G. Pullaiah College of Engineering and Technology
Chapter 6: Loops.
REPETITION CONTROL STRUCTURE
Python Loops and Iteration
Computer Programming Fundamentals
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
When to use Tuples instead of Lists
CMSC201 Computer Science I for Majors Lecture 22 – Searching
Python: Control Structures
loops for loops iterate over a given sequence.
Control Statements: Part 2
Basic operators - strings
Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during.
Repetition: the for loop
While Loops in Python.
JavaScript: Control Statements.
JavaScript: Control Statements I
Agenda Control Flow Statements Purpose test statement
Lecture 4B More Repetition Richard Gesick
MSIS 655 Advanced Business Applications Programming
COSC 1323 – Computer Science Concepts I
Arithmetic operations, decisions and looping
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Python Primer 2: Functions and Control Flow
CMSC 120: Visualizing Information 3/25/08
Chapter 8 JavaScript: Control Statements, Part 2
Structured Program Development in C
CS190/295 Programming in Python for Life Sciences: Lecture 6
Types, Truth, and Expressions (Part 2)
Coding Concepts (Basics)
Test Automation For Web-Based Applications
String and Lists Dr. José M. Reyes Álamo.
Chapter 6 Control Statements: Part 2
Introduction to Python: Day Three
CMSC201 Computer Science I for Majors Lecture 09 – While Loops
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
Introduction to Python
Introduction to Repetition Structures
2.6 The if/else Selection Structure
functions: argument, return value
CHAPTER 6: Control Flow Tools (for and while loops)
For loops Taken from notes by Dr. Neil Moore
Introduction to Python
The structure of programming
CHAPTER 4: Lists, Tuples and Dictionaries
Repetition: the for loop
Introduction to Programming
Another Example Problem
“Everything Else”.
Chapter 8 JavaScript: Control Statements, Part 2
Introduction to Python
Selamat Datang di “Programming Essentials in Python”
Presentation transcript:

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

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

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

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

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

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

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

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

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]

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]

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 ...

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

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

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

exercise break

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"

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?

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

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}

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')]

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?

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

exercise break

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

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

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

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?

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']

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}

exercise break