Compsci 101.2, Fall 2015 12.1 Plan for FFWOO l Programming in the small and in the large  Making one function work correctly, especially in the context.

Slides:



Advertisements
Similar presentations
ECS 15 if and random. Topic  Testing user input using if statements  Truth and falsehood in Python  Getting random numbers.
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.
Top-Down Design CSC 161: The Art of Programming Prof. Henry Kautz 9/16/2009.
James Tam Loops In Python In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
Python (yay!) November 16, Unit 7. Recap We can store values in variables using an assignment statement >>>x = We can get input from the user using.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Geography 465 Assignments, Conditionals, and Loops.
Loops and Iteration Chapter 5 Python for Informatics: Exploring Information
Python Control of Flow.
Python quick start guide
4. Python - Basic Operators
1 Python Control of Flow and Defining Classes LING 5200 Computational Corpus Linguistics Martha Palmer.
Programming for Linguists An Introduction to Python 24/11/2011.
CPTR 124 Review for Test 1. Development Tools Editor Similar to a word processor Allows programmer to compose/save/edit source code Compiler/interpreter.
By the end of this session you should be able to...
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.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
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 Arrays  Aggregate data type  Deal with items of same type  Lists of words  Numbers  Analogies  Mailboxes in post office  CD racks.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Python 101 Dr. Bernard Chen University of Central Arkansas PyArkansas.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Count Controlled Loops Look at the little children … Why is the sun’s face features orange …
More Python!. Lists, Variables with more than one value Variables can point to more than one value at a time. The simplest way to do this is with a List.
Lecture 15 – more on lists and for…in loops, the split() function COMPSCI 1 1 Principles of Programming.
Compsci 101.2, Fall Plan for TDAFB (after fall break) l Hear via about plans to catch-up and stay-ahead  Profs. Rodger and Astrachan to.
Python Let’s get started!.
Compsci 101.2, Fall Plan for FWON l Review current assignments and APTs  Review Dictionaries and how to use them  Code and APT walk-through.
Compsci 101.2, Fall Plan for October 29 l Review dictionaries and their use  Very efficient, easy to use  Efficiency doesn't matter much for.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
Iteration. Iteration: Review  If you wanted to display all the numbers from 1 to 1000, you wouldn’t want to do this, would you? Start display 1 display.
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
ITERATION. Iteration Computers are often used to automate repetitive tasks. Repeating identical or similar tasks without making errors is something that.
The If Statement There are no switch statements in Python. You need to use just if statements. There are no switch statements in Python. You need to use.
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.
CompSci 101 Introduction to Computer Science February 4, 2016 Prof. Rodger compsci101 spring161.
Compsci 06/101, Spring This week in Compsci 6/101 l From sets to dictionaries  Incredibly powerful in solving problems  Power in Perl, Python,
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
CONDITIONALS CITS1001. Scope of this lecture if statements switch statements Source ppts: Objects First with Java - A Practical Introduction using BlueJ,
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.
Functions. What is a Function?  We have already used a few functions. Can you give some examples?  Some functions take a comma-separated list of arguments.
7 - Programming 7J, K, L, M, N, O – Handling Data.
CMSC201 Computer Science I for Majors Lecture 07 – While Loops
String and Lists Dr. José M. Reyes Álamo.
CprE 185: Intro to Problem Solving (using C)
Topic: Iterative Statements – Part 1 -> for loop
Compsci 6/101: I Python Techniques for looping
Lecture 6 Repetition Richard Gesick.
Selection and Python Syntax
Python: Control Structures
Lesson 05: Iterations Class Chat: Attendance: Participation
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
CompSci 101 Introduction to Computer Science
Engineering Innovation Center
Lecture 4A Repetition Richard Gesick.
Introduction to Programming
CompSci 101 Introduction to Computer Science
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
IST256 : Applications Programming for Information Systems
String and Lists Dr. José M. Reyes Álamo.
Python programming exercise
(more) Python.
PFtTBSB Files and formats: how to read and store data
Python Basics with Jupyter Notebook
Backtracking, Search, Heuristics
Presentation transcript:

Compsci 101.2, Fall Plan for FFWOO l Programming in the small and in the large  Making one function work correctly, especially in the context of an interactive game – in the small  Creating multiple functions in a module that communicate with each other and "between" function calls or with other modules – in the medium  Creating an API that other programmers can use to accomplish tasks, facilitating multi-module interactions – toward the large

Compsci 101.2, Fall PFFWOO continued l Python idioms – in the small programming  List Comprehensions [x for x in range(100) if x % 2 == 0]  Sets as simple way to structure data Similar to list, but no duplicate elements l Speaking "in the vernacular" helps in communicating with other programmers  Who might help you in many ways

Compsci 101.2, Fall Loops in programs We've seen for X in Y: loops  What type of thing has Y been?  What type of thing has X been?  Deep discussion for Y that we gloss over (iterator, iterable, and more) l Sometimes you can't get a "next" item, but still need a loop  Looping to keep a program running, e.g., when interacting with user like in a game or drawing or reacting to mouse clicks or …

Compsci 101.2, Fall SmartGuessing.py low, high = 1, 100 while True: guess = (low + high)/2 print "I guess",guess response = raw_input("high/low/correct ") if response[0] == 'c': print "I guessed your number!" break elif response[0] == 'h': high = guess-1 else: low = guess + 1

Compsci 101.2, Fall Thinking about guessing numbers

Compsci 101.2, Fall Refactoring Game-playing Program while True: take_turn() update_state() if game_over(): update() break l Determine state  Local variables  Parameters to functions  Initialize appropriately

Compsci 101.2, Fall SmartGuessing.py low, high = 1,100 while True: guess = (low + high)/2 print "I guess",guess response = raw_input("high/low/correct ") if response[0] == 'c': print "I guessed your number!" break elif response[0] == 'h': high = guess-1 else: low = guess + 1 Update State Take Turn Game Over

Compsci 101.2, Fall Anita Borg “Dr. Anita Borg tenaciously envisioned and set about to change the world for women and for technology. … she fought tirelessly for the development technology with positive social and human impact.” l “Anita Borg sought to revolutionize the world and the way we think about technology and its impact on our lives.” l ch?v=1yPxd5jqz_Q ch?v=1yPxd5jqz_Q

Compsci 101.2, Fall From Numbers to Words l Would you like to play a game?  Words with Friends  Hanging with Friends  Jotto by yourself l l l

Compsci 101.2, Fall Problem Solving: Common APT l count(“smart”, “beast”) is 3 l count(“smart”, “seats”) is 3 l count(“seems”, “eases”) is ? l General ideas:  We need a loop, over what?  We need to mark a letter as used, how?

Compsci 101.2, Fall Jotto.py def play(words): print “Jotto: I guess your word” while True: guess = random.choice(words) print "my guess:",guess same = raw_input("how many in common? ") sameInt = int(same) if sameInt == 6: print "I win!!" break # conceptually what do we do here?

Compsci 101.2, Fall New Idiom: List Comprehension l Given a list of strings  New list of just those that are “special”  Remove non-special strings? Create new list? l Given a list of numbers  New list of just the positive numbers  Remove negative numbers? Create new list?

Compsci 101.2, Fall Be Positive! def onlyPos(nums): ret = [] for n in nums: if n > 0: ret.append(n) return ret print onlyPos([1,2,3,-1,-2,-3])

Compsci 101.2, Fall Don’t be Negative! def removeNegs(nums): for n in nums: if n < 0: nums.remove(n) return nums x = [1,2,3,-1,-2,-3] y = removeNegs(x) print x,y

Compsci 101.2, Fall List Comprehension x = [1,2,-1,-2,3,4,-3,-4] y = [n for n in x if n > 0] l See onlyPos for comparison def onlyPos(nums): ret = [] for n in nums: if n > 0: ret.append(n) return ret

Compsci 101.2, Fall General format for list comprehension l Creates a new list, based on existing list l [v_expression for v in list]  v is a variable that iterates over list  v_expression is any expression, could use v s = [‘a’, ‘b’, ‘c’] t = [1,2,3] x = [v*2 for v in s] y = [v*2 for v in t]

Compsci 101.2, Fall Filtered list comprehension l Only selects certain elements from list l [v_exp for v in list if bool_v]  v is a variable that iterates over list  v_expr is any expression, could use v  bool_v is boolean expression, could use v s = [‘a’, ‘b’, ‘c’] t = [1,2,3] x = [v*2 for v in s if v > ‘a’] y = [v*2 for v in t if v % 2 == 1]

Compsci 101.2, Fall Questions

Compsci 101.2, Fall Return to Jotto l How can we select only the words with the same number of letters in common with guess?  If guess is “stick” and count is 2  What about “stand”, “thick”, “check” l How do we use a list comprehension?