Programming Patterns CSC 161: The Art of Programming Prof. Henry Kautz 9/30/2009.

Slides:



Advertisements
Similar presentations
Python Basics: Statements Expressions Loops Strings Functions.
Advertisements

ECS 15 if and random. Topic  Testing user input using if statements  Truth and falsehood in Python  Getting random numbers.
Enumeration Data Type enum Day {SUN, MON, TUE, WED, THU, FRI, SAT}; Day today; today = WED; if (today == FRI) cout
STRINGS IN PYTHON. Where have we seen strings before? #string type variables s = “hello” #obtaining keyboard input from the user choice = input(“Please.
Top-Down Design CSC 161: The Art of Programming Prof. Henry Kautz 9/16/2009.
Display a 12-Month Calendar CS-2301 D-term Programming Assignment #2 12-Month Calendar CS-2301 System Programming C-term 2009 (Slides include materials.
Designing Algorithms February 2nd. Administrativia Lab assignments will be due every Monday Lab access –Searles 128: daily until 4pm unless class in progress.
Visual Basic: An Object Oriented Approach 3 – Making Objects Work.
Chapter 2 Writing Simple Programs
Assignment #2, 12- month Calendar CS-2301, B-Term Programming Assignment #2 12-Month Calendar CS-2301, System Programming for Non-Majors (Slides.
Computing with Strings CSC 161: The Art of Programming Prof. Henry Kautz 9/16/2009.
Working with Files CSC 161: The Art of Programming Prof. Henry Kautz 11/9/2009.
Java Basics (continued)
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
Computing with Numbers CSC 161: The Art of Programming Prof. Henry Kautz 9/14/2009.
October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 1 Introduction to Computer Programming Chapter 4: For Loops, Strings, and Tuples Michael.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
FUNCTIONS. Function call: >>> type(32) The name of the function is type. The expression in parentheses is called the argument of the function. Built-in.
An Overview of Programming in Python CSC 161: The Art of Programming Prof. Henry Kautz 9/9/2009 Slides stolen shamelessly from Dr. Mark Goadrich, Centenary.
Data Types Integer 15 StringHelloThere! Float/Real BooleanYes / No CharP.
Programming for Engineers in Python Sawa 2015 Lecture 2: Lists and Loops 1.
60 Questions (review for final exam) CSC 161: The Art of Programming Prof. Henry Kautz 12/7/
Strings CS303E: Elements of Computers and Programming.
Manipulating Text In today’s lesson we will look at: why we might want to pick out parts of text strings some BASIC functions that can be used to chop.
Functions Chapter 4 Python for Informatics: Exploring Information
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 8 Working.
For loops in programming Assumes you have seen assignment statements and print statements.
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 5A Repetition (Concepts)
Loops Robin Burke IT 130. Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Two: Fundamental Data Types Slides by Evan Gallagher.
Variables and Expressions CMSC 201. Today we start Python! Two ways to use python: You can write a program, as a series of instructions in a file, and.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
GCSE Computing: Programming GCSE Programming Remembering Python.
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
CS 1430: Programming in C++.
COP 2510 Chapter 6 - Functions. Random function (randint) #import the desired function import random #integer random number in the range of 1 through.
Variables and Strings. Variables  When we are writing programs, we will frequently have to remember a value for later use  We will want to give this.
Controlling Program Structures. Big Picture We are learning how to use structures to control the flow of our programs Last week we looked at If statements.
Java Basics (continued) Ms. Pack AP Computer Science A.
Chapter Four Common facilities of procedural languages.
Input, Output and Variables GCSE Computer Science – Python.
Chapter 2 Writing Simple Programs
CSC 211 Java I for loops and arrays.
5 Day Forecast Mon Tues Wed Thu Fri.
While Loops in Python.
GANTT CHARTS Example Example Example Example text Tasks Example 1
3rd prep. – 2nd Term MOE Book Questions.
Mon – 2Tue – 3Wed – 4Thu - 5Fri - 6Sat - 7Sun - 8 Mon – 9Tue – 10Wed – 11Thu - 12Fri – 13Sat – 14Sun -15 Mon – 16Tue – 17Wed – 18Thu - 19Fri – 20Sat –
Introduction to Python
MON TUE WED THU
Chapter (3) - Looping Questions.
Agenda Collating sequence / Sorting data
2008 Calendar.
Introduction to Value-Returning Functions: Generating Random Numbers
Sun Mon Tue Wed Thu Fri Sat
CHAPTER 3: String And Numeric Data In Python
CHAPTER 6: Control Flow Tools (for and while loops)
Python Basics with Jupyter Notebook
Calendar – 2010 (October, November & December)
Sun Mon Tue Wed Thu Fri Sat
1/○~1/○ weekly schedule MON TUE WED THU FRI SAT SUN MEMO
2016 | 10 OCT SUN MON TUE WED THU FRI SAT
Sun Mon Tue Wed Thu Fri Sat
CMPT 120 Lecture 10 – Unit 2 – Cryptography and Encryption –
2008 Calendar.
Presentation transcript:

Programming Patterns CSC 161: The Art of Programming Prof. Henry Kautz 9/30/2009

Assignments Assignment #4 (Design Lab) Due by Saturday 10 th October Electronic turn in Assignment #5 (Written exercises) Due Wednesday 7 th October (no class Oct 5 th ) Turn in hardcopy in class Reading assigned today: If statements: Textbook Sec. 7.1 and 7.3 While statements: Textbook Sec. 8.2 Pre-reading for Oct 7 th : Textbook Chapter 5 (Objects & Graphics) 2

Last Class Top-Down Design How to go from a problem statement to a program Key idea: don't just start trying to write code! Take small steps: 1. Task statement 2. Beginning, Middle, End 3. Main loop and stopping condition (if any) 4. Identify sub-tasks 5. Refine sub-tasks (repeat as necessary) 6. Write code 7. Read & revise 3

Today: Programming Patterns Begin writing code when it is obvious how to implement it What makes something obvious? When you have seen something like it before! Programming Patterns Kinds of expressions, statements, and code fragments that appear over and over again As your experience grows, your mental library grows More and more things become obvious 4

Math Expression Patterns Basic arithmetic slope = (y2 – y1)/(x2 – x1) Complete mathematics import math Dist = math.sqrt((x1 – x2)**2 + (y1 – y2)**2) Random choice import random Roll = random.randint(1,6) 5

String Expression Patterns Creating a string S = 'Hello, World!' Length of a string len(S) Picking out the K-th character in a string S[K] First character in a string S[0] Last character in a string S[len(S) – 1] 6

String Expression Patterns Concatenating (stringing together) strings S = 'Hello,' P = 'Sam' S + ' ' + P == 'Hello, Sam' Finding the position of a word or character in a string import string string.find('c', 'abcdefghijklmnopqrstuvwxyz') Replacing a character or word in a string S = string.replace('Hi! How are you!', '!', ' ') 7

String Expression Patterns Translating back and forth between a integer and character, based on the position of the characters in a string S = 'abcdefghijklmnopqrstuvwxyz' S[3-1] == 'c' string.find(S, 'c')+1 == 3 8

List Expression Patterns Creating list [0, 2, 4, 6, 8] range(0,8,2) Length of a list len(L) Picking out an element in a list L = ['Fee', 'Fi', 'Fo', 'Fum'] L[2] == 'Fo' Last element of a list L[len(L) – 1] 9

List Expression Patterns Convert an integer value to a string using a list Days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] K = 4 print 'Today is ' + Days[K] 10

Input Patterns Numeric input age = input('What is your age? ') Text input (a single line) name = raw_input('What is your name?') Text input (individual words) fullname = raw_input('What is your full name?') L = string.split(fullname) firstname = L[0] lastname = L[1] 11

Iteration (Repeating) Patterns Iterate over a range of numbers Summing numbers from 1 to 5 Sum = 0 for X in range(1,5): Sum = Sum + X Iterate over members of a list L = ['Sam', 'Fred', 'John'] for X in L: print 'Hello, ', X 12

Iteration (Repeating) Patterns Iterate over members of a list Summing numbers in a list L = [25, 20, 22, 19] Grade = 0 for X in L: Grade = Grade + X Iterate over positions in a list Summing numbers in a list L = [25, 20, 22, 19] Grade = 0 for X in range(len(L)) Grade = Grade + L[X] 13

Choice Do something different depending on the value of a expression if (Age > 18): print 'Okay to enter' elif (Age > 13): print 'Okay to enter with guardian' else: print 'You may not enter' 14

Using Patterns Problem: determine the average length of the words in a sentence typed by the user. 15