Sorting. Lists have a sort method Strings are sorted alphabetically, except... Uppercase is sorted before lowercase (yes, strange)



Similar documents
Exercise 1: Python Language Basics

CS106A, Stanford Handout #38. Strings and Chars

Python Lists and Loops

Introduction to Python

Objects and classes. Objects and classes. Jarkko Toivonen (CS Department) Programming in Python 1

Chapter 4: Computer Codes

Python Loops and String Manipulation

CS177 MIDTERM 2 PRACTICE EXAM SOLUTION. Name: Student ID:

Time Clock Import Setup & Use

PYTHON Basics

Programming Exercises

LINEAR INEQUALITIES. less than, < 2x + 5 x 3 less than or equal to, greater than, > 3x 2 x 6 greater than or equal to,

Interactive Applications (CLI) and Math

Exercise 4 Learning Python language fundamentals

WBSCM. Web-Based Supply Chain Management. RA Training Guide has been developed to assist our recipients in utilizing USDA s online ordering program

Python Programming: An Introduction to Computer Science

University of Hull Department of Computer Science. Wrestling with Python Week 01 Playing with Python

NLP Lab Session Week 3 Bigram Frequencies and Mutual Information Scores in NLTK September 16, 2015

.NET Standard DateTime Format Strings

NetSupport DNA Configuration of Microsoft SQL Server Express

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

Arduino Lesson 5. The Serial Monitor

Lecture 2, Introduction to Python. Python Programming Language

COMSC 100 Programming Exercises, For SP15

FTP client Selection and Programming

Basic Object-Oriented Programming in Java

InfiniteInsight 6.5 sp4

Unit 10: Microsoft Access Queries

JMETER - MONITOR TEST PLAN

Making the MDM file. cannot

JavaScript Introduction

Data Intensive Computing Handout 5 Hadoop

Searching Guide Version 8.0 December 11, 2013

Stanford Math Circle: Sunday, May 9, 2010 Square-Triangular Numbers, Pell s Equation, and Continued Fractions

Introduction to Programming with Python. A Useful Reference.

MS Access: Advanced Tables and Queries. Lesson Notes Author: Pamela Schmidt

Python Standard Library: Data Storage Clemens Szyperski, in Component Software

>

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

1.3 Conditionals and Loops

SAP InfiniteInsight Explorer Analytical Data Management v7.0

File Transfer Protocol (FTP) Instructions

Regular Expression Syntax

How do sort this list using one line? a_list.sort()

Secure Retrieval (First Time)

College Tuition: Data mining and analysis

How-To Set Custom Scale Format for FedEx Ship Manager

Access 2010: Creating Queries Table of Contents INTRODUCTION TO QUERIES... 2 QUERY JOINS... 2 INNER JOINS... 3 OUTER JOINS...

Compsci 101: Test 1. October 2, 2013

Introduction to Python for Text Analysis

Programming a mathematical formula. INF1100 Lectures, Chapter 1: Computing with Formulas. How to write and run the program.

Data Intensive Computing Handout 6 Hadoop

ADP Workforce Now V3.0

FARHAT GUITAR CHORD FONTS User's Manual (English)

Cognos Event Studio. Deliver By : Amit Sharma Presented By : Amit Sharma

! 2!!$ ,)!$- %$0. Baskı-2 ! "! #$ % #$#!&'! '! (&&)!! &!! #.! &)!$#$! /&)!!! 0! &)!$!.!! 0$! #! &)!$ &.!!#$!! 3!&!#!!3! #&!'! &! 4!!

ASCII Code. Numerous codes were invented, including Émile Baudot's code (known as Baudot

Using This Reference Manual Chapter 1 to Issue ACL Commands

MEDIAplus administration interface

Setting Up Database Security with Access 97

Best Practices for... "Setting up notifications for alerts"

Writing Simple Programs

Computer Science for San Francisco Youth

MIS Export via the FEM transfer software

Introduction to: Computers & Programming: Input and Output (IO)

Admin Guide for Lead-Xpress

Linear Equations and Inequalities

Python for Rookies. Example Examination Paper

From Data Modeling to Data Dictionary Written Date : January 20, 2014

Binary Heap Algorithms

JavaScript: Introduction to Scripting Pearson Education, Inc. All rights reserved.

Using Microsoft Access

Conditionals (with solutions)

Infinite Campus Ad Hoc Reporting Basics

Chapter 2 Writing Simple Programs

Interspire Website Publisher Developer Documentation. Template Customization Guide

SECURED PDF DOWNLOADABLE PRODUCTS BY TANGKOKO

Informatica e Sistemi in Tempo Reale

1st Grade Math Standard I Rubric. Number Sense. Score 4 Students show proficiency with numbers beyond 100.

Search help. More on Office.com: images templates. Here are some basic tasks that you can do in Microsoft Excel 2010.

Bulk Downloader. Call Recording: Bulk Downloader

Smart Card Authentication Client. Administrator's Guide

PaymentNet Federal Card Solutions Cardholder FAQs

ezbusiness Expense Management

Version August 2016

CHAPTER 11 LEGAL ACCOUNTING MODULE 11.0 OVERVIEW 11.1 REQUIREMENTS AND INSTALLATION Special Requirements

Analysis of Algorithms I: Optimal Binary Search Trees

IP-PBX Quick Start Guide

The main program should be added to the menu system (e.g ). Menu security should be set up to restrict user access. Frame in ASCII - Version

SPSS for Windows importing and exporting data

XSLT Mapping in SAP PI 7.1

FWG Management System Manual

I. Pointwise convergence

VIP Quick Reference Card

Microsoft Access 2010 Tables & Field Properties

LAB #3 VHDL RECOGNITION AND GAL IC PROGRAMMING USING ALL-11 UNIVERSAL PROGRAMMER

Transcription:

Sorting and Modules

Sorting Lists have a sort method Strings are sorted alphabetically, except... L1 = ["this", "is", "a", "list", "of", "words"] print L1 ['this', 'is', 'a', 'list', 'of', 'words'] L1.sort() print L1 ['a', 'is', 'list', 'of', 'this', 'words'] Uppercase is sorted before lowercase (yes, strange) L1 = ["this", "is", "a", "list", "Of", "Words"] print L1 ['this', 'is', 'a', 'list', 'Of', 'Words'] L1.sort() print L1 ['Of', 'Words', 'a', 'is', 'list', 'this']

for i in range(32, 127):... print i, "=", chr(i)... 32 = 33 =! 34 = " 35 = # 36 = $ 37 = % 38 = & 39 = ' 40 = ( 41 = ) 42 = * 43 = + 44 =, 45 = - 46 =. 47 = / 48 = 0 49 = 1 50 = 2 51 = 3 52 = 4 53 = 5 54 = 6 55 = 7 56 = 8 57 = 9 58 = : 59 = ; 60 = < 61 = = 62 = > 63 =? 64 = @ 65 = A 66 = B 67 = C 68 = D 69 = E 70 = F 71 = G 72 = H 73 = I 74 = J 75 = K 76 = L 77 = M 78 = N 79 = O 80 = P 81 = Q 82 = R 83 = S 84 = T 85 = U 86 = V 87 = W 88 = X 89 = Y 90 = Z 91 = [ 92 = \ 93 = ] 94 = ^ 95 = _ 96 = ` 97 = a 98 = b 99 = c 100 = d 101 = e 102 = f 103 = g 104 = h 105 = i 106 = j 107 = k 108 = l 109 = m 110 = n 111 = o 112 = p 113 = q 114 = r 115 = s 116 = t 117 = u 118 = v 119 = w 120 = x 121 = y 122 = z 123 = { 124 = 125 = } 126 = ~ ASCII order for letter in "Hello":... print ord(letter)... 72 101 108 108 111 10

Sorting Numbers Numbers are sorted numerically L3 = [5, 2, 7, 8] L3.sort() print L3 [2, 5, 7, 8] L4 = [-7.0, 6, 3.5, -2] L4.sort() print L4 [-7.0, -2, 3.5, 6]

Sorting Both You can sort with both numbers and strings L5 = [1, "two", 9.8, "fem"] L5.sort() print L5 [1, 9.8000000000000007, 'fem', 'two'] If you do, it usually means you ve designed your program poorly.

Sort returns nothing! Sort modifies the list in-place L1 = "this is a list of words".split() print L1 ['this', 'is', 'a', 'list', 'of', 'words'] x = L1.sort() print x None print L1 ['a', 'is', 'list', 'of', 'this', 'words']

Three steps for sorting L1 = "this is a list of words".split() print L1 ['this', 'is', 'a', 'list', 'of', 'words'] L1.sort() #1 - Get the list #2 - Sort it #3 - Use the sorted list print L1 ['a', 'is', 'list', 'of', 'this', 'words']

Sorting Dictionaries Dictionary keys are unsorted D = {"ATA": 6, "TGG": 8, "AAA": 1} print D {'AAA': 1, 'TGG': 8, 'ATA': 6}

Sorting Dictionaries #1 - Get the list D = {"ATA": 6, "TGG": 8, "AAA": 1} print D {'AAA': 1, 'TGG': 8, 'ATA': 6} keys = D.keys() print keys ['AAA', 'TGG', 'ATA']

#2 - Sort the list D = {"ATA": 6, "TGG": 8, "AAA": 1} print D {'AAA': 1, 'TGG': 8, 'ATA': 6} keys = D.keys() print keys ['AAA', 'TGG', 'ATA'] keys.sort() print keys ['AAA', 'ATA', 'TGG'] for k in keys:... print k, D[k]... AAA 1 ATA 6 TGG 8

#3 - Use the sorted list D = {"ATA": 6, "TGG": 8, "AAA": 1} print D {'AAA': 1, 'TGG': 8, 'ATA': 6} keys = D.keys() print keys ['AAA', 'TGG', 'ATA'] keys.sort() print keys ['AAA', 'ATA', 'TGG'] for k in keys:... print k, D[k]... AAA 1 ATA 6 TGG 8

More info There is a how-to on sorting at http://www.amk.ca/python/howto/sorting/sorting.html

Modules Modules are collections of objects (like strings, numbers, functions, lists, and dictionaries) You ve seen the math module import math math.cos(0) 1.0 math.cos(math.radians(45)) 0.70710678118654746 math.sqrt(2) / 2 0.70710678118654757 math.hypot(5, 12) 13.0

Importing a module The import statement tells Python to find module with the given name. import math This says to import the module named math.

Using the new module Objects in the math module are accessed with the dot notation import math math.pi 3.1415926535897931 This says to get the variable named pi from the math module.

Attributes The dot notation is used for attributes, which are also called properties. import math math.pi 3.1415926535897931 math.degrees(math.pi) 180.0 pi and degrees are attributes (or properties) of the math module.

Make a module First, create a new file In IDLE, click on File then select New Window. This creates a new window. In that window, save it to the file name seq_functions.py At this point the file is empty.

Add Python code In the file seq_functions.py add the following BASES = "ATCG" def GC_content(s): return (s.count("g") + s.count("c")) / float(len(s)) Next, save this file (again).

Test it interactively import seq_functions seq_functions.bases 'ATCG' seq_functions.gc_content("atcg") 0.5

Using it from a program Create a new file called main.py Add the following code import seq_functions print "%GC content: ", seq_functions.gc_content(seq_functions.bases) Run this program. You should see 0.5 printed out.

Making changes If you edit seq_functions.py then you must tell Python to reread the statements from the module. This does not happen automatically. We have configured IDLE to reread all the modules when Python runs. If you edit a file in IDLE, you must do Run Module for Python to see the changes.

Assignment 30 Make a new program which asks for a DNA sequence as input and prints the GC content as output. It must use the seq_functions.py module to get the GC_content function. Example output Enter DNA sequence: AATC %GC content: 25.0

Assignment 31 Take the count_bases function from yesterday. Put it in the seq_functions.py module. Modify your main program so it also prints the number of bases. Enter DNA sequence: AATC %GC content: 25.0 A: 2 T: 1 C: 1

Assignment 32 Start with the program you have to count the number of sequences which have a given property. From yesterday s exercise, these are individual functions. Move those functions into the seq_functions.py module. The program output should be unchanged.