Compsci 06/101, Fall 2010 4.1 What will you do in Compsci 6 Today? l Learn about selection and if/else statements  In context of solving problems  Understanding.

Slides:



Advertisements
Similar presentations
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
Advertisements

CS0007: Introduction to Computer Programming
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
Decision Making George Mason University. Today’s topics 2 Review of Chapter 2: Decision Making Go over exercises Decision making in Python.
Games and Simulations O-O Programming in Java The Walker School
Lecture 11 – if … else, if... elif statements, nested ifs COMPSCI 1 1 Principles of Programming.
Python Control of Flow.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
An Introduction to Textual Programming
CS 102 Computers In Context (Multimedia)‏ 01 / 28 / 2009 Instructor: Michael Eckmann.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);
Programming for Linguists An Introduction to Python 24/11/2011.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
CS1022 Computer Programming & Principles Lecture 1.2 A brief introduction to Python.
1 CSC 221: Computer Programming I Spring 2010 interaction & design  modular design: roulette game  constants, static fields  % operator, string equals.
Compsci 06/101, Fall Compsci 6/101: Sept 6-10 l We've looked at the language Python  Names and types: int, float, long, String, file, …  Operations:
Introduction. 2COMPSCI Computer Science Fundamentals.
Compsci 101.2, Fall PFTT (plan for this Thursday) l What is a Python program?  In the context of what we do in Compsci 101  In a neuroscience.
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
Fall Week 4 CSCI-141 Scott C. Johnson.  Computers can process text as well as numbers ◦ Example: a news agency might want to find all the articles.
Compsci 101.2, Fall PFThursday l Review Organization and Problem-Solving  Defining functions, calling functions  Return types, print, None l.
I Power Int 2 Computing Software Development High Level Language Constructs.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Compsci 6/101, Spring More on Python, Tools, Compsci 101 l APTs, Assignments, Tools  APT: Algorithmic Problem-solving and Testing  How to get.
Compsci 101.2, Fall PFTheWeek 9/21-9/25 l Potpourri, Hodgepodge of Review Concepts  File reading and writing  Boolean expressions  Arithmetic.
Conditional Execution Chapter 3 Python for Informatics: Exploring Information
Compsci 101.2, Fall PFTWeek l Introduce new Python concepts  Control: if, elif, else, for  Data: Strings, Lists Operators on data: slicing,
I Power Higher Computing Software Development High Level Language Constructs.
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.
Java Basics Hussein Suleman March 2007 UCT Department of Computer Science Computer Science 1015F.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Decision Structures, String Comparison, Nested Structures
CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May.
Logical Operators, Boolean Variables, Random Numbers This template was just too good to let go in one day!
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
Compsci 101.2, Fall Plan for eleven-four l Thinking about APTs and test problems  How do you choose: list, string, set, dictionary  Experience?
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.
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
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.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
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.
Python Programing: An Introduction to Computer Science
CompSci 101 Introduction to Computer Science February 4, 2016 Prof. Rodger compsci101 spring161.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Genome Revolution: COMPSCI 004G 5.1 Searching l Why does SSAHA compute locations of all n-mers?  agttc occurs at (1,3) (1,18) (2,6) (3,13), …  What is.
Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
CompSci 101 Introduction to Computer Science March 8, 2016 Prof. Rodger.
Loop Structures and Booleans Zelle - Chapter 8 Charles Severance - Textbook: Python Programming: An Introduction to Computer Science,
CS1022 Computer Programming & Principles
Compsci 6/101: I Python Techniques for looping
Introduction to Python
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
Introduction to Programming
Loop Structures and Booleans Zelle - Chapter 8
CS190/295 Programming in Python for Life Sciences: Lecture 6
Introduction to Programming
Logical Operations In Matlab.
Compsci 6/101: PFTW Review how APTs and Python work, run
Thinking about programming
Introduction to Programming
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
3.0 - Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
CompSci 101 Introduction to Computer Science
Presentation transcript:

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 how to alter flow-of-control in Python  Vastly increasing the kinds of problems we can solve l Learn about looping over sequences  See above! Sequences will be strings, lists, images l Learn about lists  Humongously powerful structure for storing data

Compsci 06/101, Fall Language and Problems in Context l Convert Romeo and Juliet to Piglatin  What else could we do with Shakespeare's plays?  How do we convert HTML to text?  Remove all $$ from salary data in a file l How do we make an image larger, more red, …  What is an image? How do read it? Convert it? Access it? l How do get the parts of an IP address  What are the parts? How do we access them?  How would a router use these parts?

Compsci 06/101, Fall How do you solve a problem like …? l Solution from lab last week always returns -1? def firstVowelIndex(word): ai = word.find('a') ei = word.find('e') ii = word.find('i') oi = word.find('o') ui = word.find('u') return min(ai,ei,ii,oi,ui ) l There is more than one way to skin a cat, but we need at least one way

Compsci 06/101, Fall Python if statements and Booleans l In python we have if: else: elif:  Used to guard or select block of code  If guard is True then, else other l What type of expression used in if/elif tests?  ==,, >=, !=, and, or, not, in  Value of expression must be either True or False  Type == bool, George Boole, Boolean, l Examples with if  String starts with vowel  Rock, paper, scissors (!aka Rochambeau) winner

Compsci 06/101, Fall Pragmatics of using if, else, elif l Indentation in Python determines how blocks of code execute  What's in a function, what's not  What's guarded by if/else statement  Preview: what's in control of for loop l Nested blocks are hard to develop and reason about, but powerful  Sometimes factor nested blocks into other functions, for example see Uppity.py l Complex boolean expressions difficult to deal with  When is a year a leap year? When divisible by 4, unless divisible by 100 except when divisible by 400

Compsci 06/101, Fall Grace Murray Hopper ( ) l “third programmer on world’s first large-scale digital computer”  US Navy: Admiral “It’s better to show that something can be done and apologize for not asking permission, than to try to persuade the powers that be at the beginning” l ACM Hopper award given for contributions before : Jennifer Rexford 2009: Tim Roughgarden

Compsci 06/101, Fall READ THIS NOW! l How do we convert Romeo and Juliet to all uppercase: WHEREFORE ART THOU ROMEO?  See Uppity.py for details, note how many functions have loops l We start with loops over sequences for element in sequence: #Process element  Sequences: Strings, files, lists, (more in future)  Sequences queryable with boolean in operator

Compsci 06/101, Fall Sequences: Strings, Files, Lists l String is a sequence of characters which are essentially strings of length 1  Python does not have a character type  In Python type("rocket"[0]) is ? l File is a sequence of lines, each line is a string  Can process files in other ways, e.g., read entire file or process a fixed number of bytes/chars at a time l List is a sequence of elements  For example, returned by line.split() in Uppity.py  Lists are powerful abstractions and they are mutable!

Compsci 06/101, Fall Anatomy of a Python List l Lists are sequences and indexable/subscriptable  What does this mean in Python? Lists created by using list(..) as a function, similar to str(..) and int(..)  What is list("apple") and what is list(123)? Lists created and returned by other functions, e.g., str.split() or range(.. )  We'll see range later, not a list in Python 3 l See list idiom in Uppity.py, create [], then append  Grow the list when appropriate

Compsci 06/101, Fall Python loops and strings l Smelly code and how to fix it: firstVowelIndex  If you're duplicating code it's smelly  If you think there's a better way, it could be smelly  Smelly code should NOT always be fixed, sometimes you can simply call it odorific! Running right is most important! def firstVowelIndex(word): mi = len(word)+1 for v in "aeiou": if v in word: mi = min(mi,word.find(v)) return mi

Compsci 06/101, Fall Looping and other idioms l Initialize a variable before a loop, update in loop  See vowel indexing code on previous slide: min What about maximal element in a list?  Create list of "long" words (from list, file, other source) l This is filtering: selecting according to some criteria  Could also remove elements from list, changing list  Create new list v. alter existing list: benefits, tradeoffs? l Can we add up the digits of a number?  How can we index elements of a number? l Can we add up divisors of a number?  How do we loop over possibilities?

Compsci 06/101, Fall Donald Knuth (b. 1938, Hopper '71) l Scholar, practitioner, artisan  Art of Computer Programming  Began effort in 1962 to survey entire field, still going l Writes beautiful code  Developed TeX to help typeset his books, widely used scientific document processing program l Many, many publications  First was in Mad Magazine  On the Complexity of Songs  Surreal Numbers

Compsci 06/101, Fall It’s all relative and it depends I make the best bread in the city I make the best bread in the world I make the best bread in the universe I make the best bread on the block

Compsci 06/101, Fall Richard Stallman (b.1953, Hopper '90) l "World's Best Programmer"  Gnu/Linux: g++  Believes all software should be free, but like “free speech”, not “free beer”  Won MacArthur award for his efforts and contributions  League for Programming Freedom It's about free, not open