Recitation 4 Programming for Engineers in Python.

Slides:



Advertisements
Similar presentations
Solve problems with Java code Algorithms with Java.
Advertisements

What we will cover today… Where is the camera on my phone? Taking a photo Zoom in and out Deleting a photo Where do my photos go to? Viewing my photos.
This Week More Types boolean string Modules print statement Writing programs if statement Type boolean.
Write a program step by step. Step 1: Problem definition. Given the coordinate of two points in 2-D space, compute and print their straight distance.
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 8 Fruitful Functions 05/02/09 Python Mini-Course: Day 2 - Lesson 8 1.
MANUFACTURER WISE PRODUCT CODE SETUP SOFTWARES MAIN SCREEN. FOR PRODUCT CODE SETUP PRESS ENTER ON “SETUP” MENU.
Cosc 5/4730 Game Design. A short game design primer. A game or animation is built on an animation loop. – Instance variables of “objects” are updated.
Recitation 7 Programming for Engineers in Python.
Chapter 4: Looping CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
Fundamentals of Python: From First Programs Through Data Structures
Thanks to: Dr. John S. Mallozzi Department of Computer Science 1. Introduction 2. Overview of programming in Python.
SIGCSE 2008 Maria Litvin Phillips Academy Andover, Massachusetts AND Combining Discrete Mathematics Python Programming Copyright ©
V Avon High School Tech Club Agenda Old Business –Delete Files New Business –Week 18 Topics: Intro to HTML/CSS: Questions? Summer Work Letter.
Teach Yourself Windows 98 Module 2: Working with Files, Folders, and the Desktop.
MICROSOFT WORD GETTING STARTED WITH WORD. CONTENTS 1.STARTING THE PROGRAMSTARTING THE PROGRAM 2.BASIC TEXT EDITINGBASIC TEXT EDITING 3.SAVING A DOCUMENTSAVING.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
PYTHON CONDITIONALS AND RECURSION : CHAPTER 5 FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
1 Word 2010 Intro to Word – Part 2. 2 Steps for Creating a Document  Step 1: Open a Blank Document (New, or Open)  Step 2: Name the Document (Save As.
CREATING A TEST IN WORD 2007 Also creating and using equations in Word 2007 Jeff Klamm Tec 539.
You SHOUD HAVE.. C:\100 folder A desktop shortcut to “IDLE (Python34)”
Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 13 Case study: Word play 05/02/09 Python Mini-Course: Day 4 – Lesson.
Quit Pierre de Fermat Fermat’s Last Conjecture Prime Numbers Euler’s Conjecture.
9/2/ CS171 -Math & Computer Science Department at Emory University.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Fill the screen challenge! This is a starter activity and should take 3 minutes [ slide 1 ] 1.Log in to your computer 2.Open IDLE 3.In interactive mode,
File Management Keeping your files organised. How to use this demo When you have read the information on the screen, click on the yellow arrow on each.
Introduction to Computing Using Python for loop / def- ining a new function  Execution control structures ( if, for, function call)  def -ining a new.
1 Working with 2007 WORD Part 2 Developed October 2007 with lots of help from.
Week 4 : Function with parameters and return values.
> 1 Using Word to Create Posters Faculty of Health Alan Grace.
Zack Russ.   A simple platform game using pygame.  Player collects jewels and progresses through the levels. A Platformer.
Coding Time This is a starter activity and should take about 10 minutes [ slide 1 ] 1.Log in to your computer 2.Open IDLE 3.Start a script session (Select.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
Python Basics  Functions  Loops  Recursion. Built-in functions >>> type (32) >>> int(‘32’) 32  From math >>>import math >>> degrees = 45 >>> radians.
9. ITERATIONS AND LOOP STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Today… The for loop. Introducing the Turtle! Loops and Drawing. Winter 2016CISC101 - Prof. McLeod1.
Word Processing vocabulary (a day) & (b day) Put the vocabulary words in your notebook.  Alignment - The way multiple lines of text line.
3. Drawing Let’s Learn Saengthong School, June – August 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
Make a function This is a starter activity and should take 5 minutes [ slide 1 ] >>> def count(number): n=1 while n
Creating a Document MOAC Lesson 1.
Lesson 06: Functions Class Participation: Class Chat:
Development Environment
IST256 : Applications Programming for Information Systems
Intro To Pete Alonzi University of Virginia Library
Python: Control Structures
Functions CIS 40 – Introduction to Programming in Python
9. Drawing Let's Learn Python and Pygame
Download Youtube videos without installing software
Fraction Fun! 5th Grade Math.
Right-of-Way Cost Estimating Planning Tool Training Guide
بسم الله الرحمن الرحيم.
For -G7 programing language Teacher / Shamsa Hassan Alhassouni.
WICS - Flappy Anteater Python Tutorial
CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.
CISC101 Reminders Quiz 2 this week.
Iteration: Beyond the Basic PERFORM
“If you can’t write it down in English, you can’t code it.”
Suppose I want to add all the even integers from 1 to 100 (inclusive)
This is a setup file for a Jeopardy game.
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Introduction to Value-Returning Functions: Generating Random Numbers
Word Lesson 1 Word Basics
CISC101 Reminders Assignment 2 due this Friday.
Errors.
Players choose either light or dark blue blocks to play.
 A function is a named sequence of statement(s) that performs a computation. It contains  line of code(s) that are executed sequentially from top.
Iteration – While Loops
Presentation transcript:

Recitation 4 Programming for Engineers in Python

Agenda Sample problems Hash functions & dictionaries (or next week) Car simulation 2

A function can be an argument 3 def do_twice(f): f() def print_spam(): print 'spam' >>> do_twice(print_spam) spam

Fermat’s last theorem 4 Pierre de Fermat

Fermat’s last theorem 5 >>> check_fermat(3,4,5,2) No, that doesn't work >>> check_fermat(3,4,5,3) No, that doesn't work Dirty shortcut since 1995: def check_fermat(a,b,c,n): print "Wiles proved it doesn’t work" Sir Andrew John Wiles 1953-

Cumulative sum 6 For a given list A we will return a list B such that B[n] = A[0]+A[1]+…A[n] Take 1: def cumulative_sum(lst): summ = [ lst[0] ] * len(lst) for i in range(1, len(lst)): summ[i] = summ[i-1] + lst[i] return summ Take 2: def cumulative_sum(lst): return [sum(lst[0:n]) for n in range(1, len(lst)+1)]

Estimating e by it’s Taylor expansion 7 from math import factorial, e term = 1 summ = 0 k = 0 while term > 1e-15: term = 1.0/factorial(k) summ += term k += 1 print "Python e:", e print “Taylor’s e:", summ print “Iterations:”, k Brook Taylor,

Estimating π by the Basel problem 8 from math import factorial, pi, sqrt term = 1 summ = 0 k = 1 while term > 1e-15: term = 1.0/k**2 summ += term k += 1 summ = sqrt(summ*6.0) print "Python pi:", pi print “Euler’s pi:", summ print “Iterations:”, k Leonard Euler,

Ramanujan’s π estimation (optional) 9 from math import factorial, pi term = 1 summ = 0 k = 0 while term > 1e-15: term = factorial(4.0*k) / factorial(k)**4.0 term *= ( *k) / 396.0**(4.0*k) summ += term k += 1 summ = 1.0/(summ * 2.0*2.0**0.5 / ) print "Python Pi:", pi print "Ramanujan Pi:", summ print “Iterations:”, k Srinivasa Ramanujan,

Triple Double Word 10 We want to find a word that has three double letters in it, like aabbcc (which is not a word!) Almost qualifiers: Committee Mississippi Write a function to check if a word qualifies Write a function that reads a text file and checks all the words Code: Corpus: words.txt words.txt

PyGame 11 A set of Python modules designed for writing computer games Download & install:

Car game 12 Control a car moving on the screen YouTube demo: Code: or in car.pyhttps://gist.github.com/ Car controlled by arrows Honk with Enter Exit with ESC

ToDo List: 13 Fix stirring problem Honk by pressing space Car will go from the bottom to top and from one side to the other (instead of getting stuck) Switch to turtle!

2 players car game 14 Collision avoidance simulator: When the cars are too close one of them honks Players need to maneuver the cars to avoid honks Code: or cars.pyhttps://gist.github.com/ Red car controlled by arrows Blue car controlled by z, x, c, s