CompSci 101 Introduction to Computer Science February 4, 2016 Prof. Rodger compsci101 spring161.

Slides:



Advertisements
Similar presentations
CompSci 101 Introduction to Computer Science February 3, 2015 Prof. Rodger Lecture given by Elizabeth Dowd.
Advertisements

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.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
CompSci 101 Introduction to Computer Science January 20, 2015 Prof. Rodger compsci 101, spring
October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 1 Introduction to Computer Programming Chapter 4: For Loops, Strings, and Tuples Michael.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
Compsci 101.2, Fall PFThursday l Review Organization and Problem-Solving  Defining functions, calling functions  Return types, print, None l.
Compsci 06/101, Fall What will you do in Compsci 6 Today? l Learn about selection and if/else statements  In context of solving problems  Understanding.
CompSci 101 Introduction to Computer Science September 23, 2014 Prof. Rodger.
Compsci 06/101, Fall How to build a program l Create a module  Can be used by other modules via import  Collection of functions, later collection.
CompSci 101 Introduction to Computer Science Sept. 9, 2014 Prof. Rodger President Brodhead speech graduation 2010 CompSci 101 Fall
Compsci 6/101, Spring More on Python, Tools, Compsci 101 l APTs, Assignments, Tools  APT: Algorithmic Problem-solving and Testing  How to get.
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
Compsci 6/101, Spring PFTW: Sequences aka Strings&Lists l From Return values to Random-ness [aka two R's]  What power does random provide? 
Compsci 101.2, Fall PFTheWeek 9/21-9/25 l Potpourri, Hodgepodge of Review Concepts  File reading and writing  Boolean expressions  Arithmetic.
Compsci 101.2, Fall PFTWeek l Introduce new Python concepts  Control: if, elif, else, for  Data: Strings, Lists Operators on data: slicing,
Python 101 Dr. Bernard Chen University of Central Arkansas PyArkansas.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
CompSci 6 Introduction to Computer Science September 13, 2011 Prof. Rodger.
Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 11 Using strings and sequences 5/02/09 Python Mini-Course: Day 3 – Lesson.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
CompSci 101 Introduction to Computer Science January 28, 2016 Prof. Rodger compsci101 spring161.
Compsci 6/101, Spring PFTW: Functions, Control, Python/Tools l How do functions work and why do we use them?  Functions we call (APIs), Functions.
Compsci 06/101, Spring Compsci 6: PFTW l Problem solving and (Python) programming  What are the steps in solving an APT?  How do you get better.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
Python Programing: An Introduction to Computer Science
CompSci 6 Introduction to Computer Science Sept. 20, 2011 Prof. Rodger CompSci 6 Fall
CompSci 101 Introduction to Computer Science February 11, 2016 Prof. Rodger.
CompSci 101 Introduction to Computer Science January 26, 2016 Prof. Rodger compsci 101, spring
CompSci 101 Introduction to Computer Science February 25, 2016 Prof. Rodger compsci101 spring20161.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
CompSci 6 Introduction to Computer Science September 27, 2011 Prof. Rodger CompSci 6 Fall
CompSci 101 Introduction to Computer Science February 16, 2016 Prof. Rodger “All your troubles are due to those ‘ifs’,” declared the Wizard. If you were.
CompSci 101 Introduction to Computer Science March 24, 2016 Prof. Rodger compsci 101, spring
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.
CompSci 101 Introduction to Computer Science March 8, 2016 Prof. Rodger.
CompSci 101 Introduction to Computer Science February 5, 2015 Prof. Rodger Lecture given by Elizabeth Dowd compsci101 spring151.
CPS 100, Spring Interlude for trees l Joyce Kilmer Joyce Kilmer l Balanced Trees  Splay  Red-Black  AVL  B-tree.
String and Lists Dr. José M. Reyes Álamo.
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
Python: Control Structures
CompSci 101 Introduction to Computer Science
Compsci 6/101: PFTW What is Python? What is a programming language?
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
Compsci 101, APTs and Sequences and Selection
CompSci 101 Introduction to Computer Science
Introduction to Computer Science
CS190/295 Programming in Python for Life Sciences: Lecture 6
CEV208 Computer Programming
String and Lists Dr. José M. Reyes Álamo.
Compsci 6/101: PFTW Review how APTs and Python work, run
Basic String Operations
The structure of programming
Topics Basic String Operations String Slicing
Michele Weigle - COMP 14 - Spr 04 Catie Welsh February 14, 2011
COMPUTER PROGRAMMING SKILLS
Introduction to Strings
Topics Basic String Operations String Slicing
Class code for pythonroom.com cchsp2cs
Introduction to Strings
Topics Basic String Operations String Slicing
CompSci 101 Introduction to Computer Science
Presentation transcript:

CompSci 101 Introduction to Computer Science February 4, 2016 Prof. Rodger compsci101 spring161

Announcements Reading and RQ7 due next time Assignment 3 due Tuesday APT 2 due today, APT 3 out APT Quiz 1 – can practice –Get two hours from 6pm Sun – 10pm Tues Today: –Designing programs to draw with turtles –Functions, if, strings, lists compsci101 spring162

Problem Solving to Code 7 Step Process 1.Work small example by hand 2.Write down what you did in words (algorithm) 3.Find Patterns (generalize algorithm) 4.Work another example by hand (does your algorithm work? If not, go back to 2) 5.Translate to code 6.Test several cases 7.Debug failed test cases 3compsci101 spring16

Turtles: bit.ly/101sp Run in eclipse Make square with different sizes? Make a rectangle? Where is the repetition? New commands: up(), down(), position(), goto() compsci101 spring164

Assignment 3 Turtles –Creative Earthquakes –Data from last 30 days around the world –Example - Find the largest earthquake compsci101 spring165

Python if statements and Booleans In python we have if: else: elif: –Used to guard or select block of code –If guard is True then code block, else other What type of expression used in if/elif tests? –==,, >=, !=, and, or, not, in –Value of expression must be either True or False –Type is bool - George Boole, Boolean, Examples with if –String starts with vowel (useful for APT Emphasize) 6

Four versions of isVowel? bit.ly/101sp def isVowel(ch): if ch =='e': return True if ch == 'a': return True if ch == 'i': return True if ch == 'o': return True if ch == 'u': return True return False def isVowel(ch): c = "aeiou".count(ch) if c > 0: return True def isVowel(ch): return "aeiou".count(ch) > 0 def isVowel(ch): if ch in "aeiou": return True else: return False A B D C compsci101 spring167

Anatomy of a Python String String is a sequence of characters –Functions we can apply to sequences: len, slice [:], others –Methods applied to strings [specific to strings] st.split(), st.startswith(), st.strip(), st.lower(), … st.find(), st.count() Strings are immutable sequences –Characters are actually length-one strings –Cannot change a string, can only create new one What does upper do? –See resources for functions/methods on strings Iterable: Can loop over it, Indexable: can slice it compsci101 spring168

Lynn Conway See Wikipedia and lynnconway.com Joined Xerox Parc in 1973 –Revolutionized VLSI design with Carver Mead Joined U. Michigan 1985 –Professor and Dean, retired '98 NAE '89, IEEE Pioneer '09 Helped invent dynamic scheduling early '60s IBM Transgender, fired in '68

Incremental + : numbers and strings Wtht vwls cn y stll rd ths sntnc? –Create a no-vowel version of word –Examine each character, if it's not a vowel … –Pattern of building a string def noVowels(word): ret = "" for ch in word: if ! isVowel(ch): ret = ret + ch return ret compsci101 spring1611

Counting vowels in a string Accumulating a count in an int is similar to accumulating characters in a string Alternative version of adding: value += 1 def vowelCount(word): value = 0 for ch in word: if isVowel(ch): value = value + 1 return value compsci101 spring1612

What does this function do? bit.ly/101sp def mystery(s): r = "" for ch in s: r = ch + r return r compsci101 spring1613

From high- to low-level Python def mystery(s): r = "" for ch in s: r = ch + r return r Create version on the right using disassembler import dis # mystery here dis.dis(mystery) 7 0 LOAD_CONST 1 ('') 3 STORE_FAST 1 (r) 8 6 SETUP_LOOP 24 (to 33) 9 LOAD_FAST 0 (s) 12 GET_ITER >> 13 FOR_ITER 16 (to 32) 16 STORE_FAST 2 (ch) 9 19 LOAD_FAST 2 (ch) 22 LOAD_FAST 1 (r) 25 BINARY_ADD 26 STORE_FAST 1 (r) 29 JUMP_ABSOLUTE 13 >> 32 POP_BLOCK 10 >> 33 LOAD_FAST 1 (r) 36 RETURN_VALUE compsci101 spring1614

Bug and Debug software 'bug' Start small –Easier to cope –Simplest input? Judicious 'print' –Debugger too Python tutor –Visualizes data –step through Verify the approach being taken, test small, test frequently –How do you 'prove' your code works? 15

APT Emphasize compsci101 spring1616

String Functions – What is output? bit.ly/101sp

Lists A list is a collection of objects scores = [99, 78, 91, 84] allAboutMe = [“Mo”,25, “ ”] club=[‘Mo’,‘Jo’,‘Po’, ‘Flo’, ‘Bo’] Lists are mutable – use [num] to change a value Lists are indexed starting at 0, or -1 from the end Functions: max, min, len, sum Slice lists [:] compsci101 spring1618

List Examples scores = [10, 8, 10, 9] print scores scores[2] = 5 print scores print max(scores) print len(scores) print sum(scores) print scores[1:] print scores[1] compsci101 spring1619

List before/after modification score = [10,8,10,9] score [2] = compsci101 spring1620

Processing List Items Process all the items in a list, one item at a time Format: for variable in list: block Example: sum = 0 nums = [6, 7, 3, 1, 2] for value in nums: sum = sum + value print sum compsci101 spring1621