Compsci 101.2, Fall 2015 5.1 PFTWeek l Introduce new Python concepts  Control: if, elif, else, for  Data: Strings, Lists Operators on data: slicing,

Slides:



Advertisements
Similar presentations
Chapter 4 Computation Bjarne Stroustrup
Advertisements

CS 100: Roadmap to Computing Fall 2014 Lecture 0.
ECS 15 if and random. Topic  Testing user input using if statements  Truth and falsehood in Python  Getting random numbers.
Types and Arithmetic Operators
Lecture 03 – Sequences of data.  At the end of this lecture, students should be able to:  Define and use functions  Import functions from modules 
Chapter 2: Algorithm Discovery and Design
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Chapter 2: Algorithm Discovery and Design
Chapter 2: Algorithm Discovery and Design
Identifiers and Assignment Statements. Data structures In any programming language you need to refer to data The simplest way is with the actual data.
1 CSC 221: Introduction to Programming Fall 2012 course overview  What did you set out to learn?  What did you actually learn?  Where do you go from.
CIS Computer Programming Logic
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition.
Invitation to Computer Science, Java Version, Second Edition.
Compsci 101, Fall Plan for the week: Week 2, Sept 1-5 l Understanding program structure  Defining, testing, calling functions  How to run a.
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:
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.
Compsci 06/101, Fall What will we do today? l Practice solving problems  Solving problems without a computer  Is this different than solving.
CSE 131 Computer Science 1 Module 1: (basics of Java)
Compsci 101.2, Fall Plan For the Week l Solve problems by programming in Python  Like to do "real-world" problems, but we're very new to the.
By the end of this session you should be able to...
Compsci 06/101, Fall Compsci 6/101: Random debugging?!# l The joys and rewards of writing code to solve a problem  How do we know where to begin?
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
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 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.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Compsci 6/101, Spring More on Python, Tools, Compsci 101 l APTs, Assignments, Tools  APT: Algorithmic Problem-solving and Testing  How to get.
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 06/101, Fall Vocabulary l What's the Spanish word for 'boy'? 'eat'?  How do you know? l What about the Turkish equivalents?  How do.
8-1 Compilers Compiler A program that translates a high-level language program into machine code High-level languages provide a richer set of instructions.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
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.
Compsci 101, Fall Plan For The Day (PFTD) l Practice solving problems  Some solved with a computer, some with Python  Differences in solving.
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
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.
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.
CompSci 101 Introduction to Computer Science February 4, 2016 Prof. Rodger compsci101 spring161.
CompSci 101 Introduction to Computer Science January 26, 2016 Prof. Rodger compsci 101, spring
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
PYTHON PROGRAMMING. WHAT IS PYTHON?  Python is a high-level language.  Interpreted  Object oriented (use of classes and objects)  Standard library.
CompSci 101 Introduction to Computer Science March 8, 2016 Prof. Rodger.
CPS 100, Spring Interlude for trees l Joyce Kilmer Joyce Kilmer l Balanced Trees  Splay  Red-Black  AVL  B-tree.
CompSci 101 Introduction to Computer Science Sept 13, 2016 Prof. Rodger compsci101 fall161.
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
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, APTs and Sequences and Selection
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
Building Java Programs
Introduction to Primitive Data types
Building Java Programs
Compsci 6/101: PFTW Review how APTs and Python work, run
Lecture 2 Python Programming & Data Types
SSEA Computer Science: Track A
Building Java Programs
Introduction to Computer Science
Introduction to Strings
Building Java Programs
Introduction to Primitive Data types
CompSci 101 Introduction to Computer Science
Presentation transcript:

Compsci 101.2, Fall PFTWeek l Introduce new Python concepts  Control: if, elif, else, for  Data: Strings, Lists Operators on data: slicing, methods, functions  Variables and constants: Names, types, and values l Review Organization and Problem-Solving  Defining functions, calling functions  Return types, print, None

Compsci 101.2, Fall Variables, Types, Values l Variable is a name associated with "storage"  Assign a value: x = 5  Print value of variable: print x  Use variable in expression: y = x * 55 l String is a type and has a value  Assign: x = "hello"  Print value of variable: print x  Use in expression print len(x ) print x + " world" l There are more types, this is a start!

Compsci 101.2, Fall Vocabulary, grammar, rules: Python l Naming  The power of abstraction and parameterization  What are parameters? What has them (Python, World)? l Types  What's used in computing? What's used in Python?  Determine names of types in Python, use type(..) l Expressions and operators in Python  Arithmetic: +, -, *, /, %, **, …  Boolean:, and,...  String: +, *, [], [:], [::]

Compsci 101.2, Fall Types and values in Python l Numbers are important, but not everything is a …  What is a number? In mathematics, in Python, in Java,  Integers, floating-point numbers, complex numbers, … We will worry about types, not speed or storage (though these are a concern sometimes) 1,2,3 compared to , 1.75, math.pi 5/2 compared to 5.0/2 compared to 5//2 l Strings are sequences of characters, "python.org"  Somewhere these are converted to numbers: 0's and 1's  No real need to know this now.  Strings are immutable: make new ones, can't change them

Compsci 101.2, Fall Expressions, Operators, Types Why is 3+5*4 different than (3+5)*4 ?  Where can you find information about precedence? Why is 5/3 different than 5.0/3 ?  What will happen in Python 3? Accommodate in 2.7? l What happens when operators go bad?  What is "apple" + 3? What is "apple" + "pi"?  What is "apple" * 3? What is "apple" * "pi" ? l What is a variable in Python?  Name, Type, Value

Compsci 101.2, Fall Observations about String literals l Sometimes the details are tricky  "I " + "love " + 'Python'  "I " + "love " + '"Python"'  "I " + "love " + "'Python'" l When in doubt, use parentheses  What is "a" + "b" * 3  What is "a" "b" * 3

Compsci 101.2, Fall Names, Types, Values Revisited name = "/data/poe.txt" ff = open(name) st = ff.read() words = st.split() print "# words in",name, "=",len(words) l What are the names in the code above?  Why are names important? l What are the types in the code above?  How do we get Python to help us answer this question l How do we re-use this code more generally  The power of names! The power of functions!

Compsci 101.2, Fall Slicing and Indexing l The Python types str (String) and list both support indexing and slicing s = "blue devils at duke" l s[1], s[2], s[0], s[50] l s[-1], s[-4] l s[5:11], s[15:], s[:4], s[:] l s[5:10:2], s[:], s[::-1]

Compsci 101.2, Fall Value Expert l Answer these questions

Compsci 101.2, 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 : Craig Gentry: : Luis von Ahn 2013: Pedro Felzenszwab 2014: Sylvia Ratnasamy

Compsci 101.2, Fall APT Pancake: l How do you solve this problem?  First steps: are there simple cases that can be solved immediately? What are these for the pancake problem?  Sometimes it helps to know if you are on track, should you use Python to check your paper and pencil work? l Get specific, solve for 5, not N  Fix one parameter, vary the other  Identify the cases and continue

Compsci 101.2, Fall Three pancakes in a two-cake pan… l Number of cakes in the system  First 5 minutes l Number of cakes in the system  Second 5 minutes

Compsci 101.2, Fall Three pancakes in a two-cake pan… l Number of cakes in the system  Third 5 minutes l How many minutes to cook all three pancakes?

Compsci 101.2, Fall Algorithmic Problem/Program Testing l Complete this form about Pancakes and one other APT (read the other one from form)

Compsci 101.2, Fall How to teach pancake flipping l  Is this computer science?  For longer, more complex robotic tasks l Back to specifics:  Capacity = 5  Numcakes = 1,2,…5?  Numcakes = 6,7,8,9,10?  Numcakes = 11,12,13,14,15? l Is five special? 4? 3? 2?

Compsci 101.2, Fall Language and Problems in Context l Convert Spanish Wikipedia page to English  How do we convert HTML to text? l How do you determine if 2040 is a leap year?  Any specified year is a leap year? l How do we make an image larger, more red, …  What is an image? How do read it? Convert it? Access it? l Make "Jones, Howard" from "Howard Jones"

Compsci 101.2, Fall What years are leap years? l 2000, 2004, 2008, …  But not 1900, not 2100, yes 2400!  Yes if divisible by 4, but not if divisible by 100 unless divisible by 400! (what?) def is_leap_year(year): if year % 400 == 0: return True if year % 100 == 0: return False if year % 4 == 0: return True return False l There is more than one way to skin a cat, but we need at least one way

Compsci 101.2, Fall Three versions of is_vowel def is_vowel(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 is_vowel(ch): c = "aeiou".count(ch) if c > 0: return True else: return False def is_vowel(ch): return "aeiou".count(ch) > 0

Compsci 101.2, 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 (useful for APT Emphasize)

Compsci 101.2, Fall Eclipse Interlude l Finishing the Pancake problem  Translating problem-solving ideas to code  Control with if/elif: arithmetic with / and %

Compsci 101.2, Fall Lessons: special cases, abstractions l There are special cases in many, many problems  Identifying them is important  Abstracting them away when possible is important  Example: Pancake APT What happens when everything fits in the pan? Can there be a pan with no capacity? l Solve problems by hand, pencil, brain  Can you do pancakes for any pan-size and number? If not, can’t write code!

Compsci 101.2, Fall Software Dreams l Translating ideas into (Python) code  Create interesting “heads”, “totem poles” ?  Create software for face recognition? Gait?  Create "five four" from "four five"?  Create "SCUBA" from "self contained underwater breathing apparatus" l Master the syntax of the language?  Organization of program constructs  Knowledge of libraries  Practice and experience!

Compsci 101.2, Fall Anatomy of a Python String l 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() l 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 l Iterable : Can loop over it, Indexable : can slice it

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

Compsci 101.2, Fall From high- to low-level Python def reverse(s): r = "" for ch in s: r = ch + r return r l Create version on the right using dissassembler dis.dis(code.py) 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

Compsci 101.2, Fall Bug and Debug l software 'bug' software 'bug' l Start small  Easier to cope l Judicious 'print'  Debugger too l Verify the approach being taken, test small, test frequently  How do you 'prove' your code works?