Python Programming in Context Chapter 2. Objectives To understand how computers can help solve real problems To further explore numeric expressions, variables,

Slides:



Advertisements
Similar presentations
History of Pi By, Travis Nitko. The Beginning The first time Pi was used in an equation was when Archimedes used it to find the area of a circle with.
Advertisements

ECS 15 if and random. Topic  Testing user input using if statements  Truth and falsehood in Python  Getting random numbers.
Chapter 6 – Circles In previous chapters, you have extensively studied triangles and quadrilaterals to learn more about their sides and angles. In this.
th grade math Volume of Triangular Prisms! and Cylinders!
1 The number  is defined as the ratio of the circumference of a circle to its diameter. What is  ? CS110: Introduction to Computer Science: Lab Module.
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.
Chapter 3 Math Vocabulary
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.
Transparency 6 Click the mouse button or press the Space Bar to display the answers.
(7.4) Patterns, relationships, and algebraic thinking. The student represents a relationship in numerical, geometric, verbal, and symbolic form. The student.
Chapter 10 Lesson 1: Circles and Circumference Objective: Learn to find the circumference of circles.
Circumference & Pi    3.14.
Are you prepared?. Find the perimeter and the area of the figure below.
8-1C The Circumference of a Circle What is circumference? What is pi? What is the symbol for pi? What are the formulas for the circumference of a circle?
A to Z Math Project Jay Dave. A- Algebra Algebraic Expressions –3ax + 11wx 2 y Algebra uses letters like x or y or other symbols in place of unknown values.
Mathematical Processes GLE  I can recognize which symbol correlates with the correct term.  I can recall the correct definition for each mathematical.
Computer Science 101 Lists in Python. The need for lists Often we need many different variables that play a similar role in the program Often we need.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Branching and Conditions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 6 Value-Returning.
7.9A Estimation and Measurement
CS320n –Visual Programming More LabVIEW Control Structures.
th grade math Circumference. Objective To find circumference of circles Why? To know how to use formulas and evaluate variable expressions using.
12-6 6th grade math Area of a Circle.
Circumference and Areas of Circles Lesson 11.5 Geometry Honors Objective: Know and use the formulas for the Circumference and Areas of Circles.
6.12 Solve problems involving the circumference &/or area of a circle when given the diameter or radius. Derive approximations for pi(π) from measurements.
Chapter 1. Objectives To provide examples of computer science in the real world To provide an overview of common problem- solving strategies To introduce.
Monte Carlo Methods.
Monte Carlo Methods Versatile methods for analyzing the behavior of some activity, plan or process that involves uncertainty.
PiPi What is Pi?  Pi stands for 3.14 or 3  You need Pi to figure out the circumference, diameter, and the area of a circle  Pi stands for 3.14 or.
Perimeter & Circumference Return to table of contents.
Python Programming in Context Chapter 4. Objectives To understand Python lists To use lists as a means of storing data To use dictionaries to store associative.
If..else Use random numbers to compute an approximation of pi Simulation of a special game of darts Randomly place darts on the board pi can be computed.
Monté Carlo Simulation  Understand the concept of Monté Carlo Simulation  Learn how to use Monté Carlo Simulation to make good decisions  Learn how.
Variables. Todays Lesson  In todays lesson you are going to:  Learn to use variables  Learn to ask for user input  Learn to save the users response.
Circumference. Question 1 Which formula would be best to use to find the diameter of the following circle? C = 81 ft ?
Iteration “for ??? in range(min,max,interval):” What if we do not know the number of repetitions ? “while (???):” (p.49) problem = input(“Enter a math.
Circumference Review. Review What is the relationship between a radius and a diameter? What does a circumference measure? What formulas do we use to calculate.
Warm-up 8.5 Area of Circles and 8.1 to 8.4 Quiz. Answers to H.W. pg 443 #1-8.
JavaScript 101 Lesson 3: Variables and Arithmetic.
Python Programming in Context Chapter 7. Objectives To use Python lists as a means of storing data To implement a nontrivial data mining application To.
3. COMPUTING WITH NUMBERS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
CS 100Lecture 231 CS100J Lecture 23 n Previous Lecture –MatLab and its implementation, continued. n This Lecture –MatLab demonstration.
Python Programming in Context Chapter 11. Objectives To explore classes and objects further To design a large multiclass application To implement a graphical.
Iteration In Python new file, enter the following ‘for loop’ can be nested for a in range(3): for b in range(-2, 3): print(“ Inner loop: ”,a, b, a+b) print(“Outer.
Objective Students will solve practical area and perimeter problems involving composite plane figures.
Do Now:. Circumference What is circumference? Circumference is the distance around a circle.
Easy as “Pi” By Yamira Hart 6 th Grade Math Maven.
G-11 (1-5) Using formulas in Geometry I can use formulas to compute perimeter and area of triangles, squares, rectangles, and circles.
March 8 th,  Problem 1) A) What is the formula for the area of a circle? B) What is the formula for the circumference of a circle? C) How do these.
9-1 Perimeter and Circumference. 9-1 Perimeter and Circumference Warm Up Add or multiply. You need to do BOTH of these!
Using a spreadsheet to calculate p
Chapter 4.
Geometry: Circles and Circumference
Check your understanding!
Making Choices with if Statements
Solving Linear Equations and Inequalities
Circumference Definition: The distance around a circle. It’s a special word for perimeter. Picture:
Random numbers What does it mean for a number to be random?
The Math class The java.lang.Math class contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square.
Random numbers What does it mean for a number to be random?
Maintenance Sheet 19 via study island
Archimedes and Pi What is p? How can you compute p? Module Arch-Pi 1
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
Bellringer.
Topic: How to calculate the area of a circle.
CS100A Lecture 21 Previous Lecture This Lecture
Geometry: Circles and Circumference
Using Modules.
Presentation transcript:

Python Programming in Context Chapter 2

Objectives To understand how computers can help solve real problems To further explore numeric expressions, variables, and assignment To understand the accumulator pattern To utilize the math library To further explore simple iteration patterns To understand simple selection statements To use random numbers to approximate an area

What is PI? Ratio of circumference to diameter math.pi from the math module

Figure 2.1

The Archimedes Approach Use many sided polygon to approximate circumference of a circle. Requires a bit of geometry

Figure 2.2

Figure 2.3

Function A name for a sequence of actions Can return a value

Listing 2.1 def functionName(param1,param2,...): statement1 statement2... return expression

Figure 2.4

Listing 2.2 import math def archimedes(numSides): innerangleB = 360.0/numSides halfangleA = innerangleB/2 onehalfsideS = math.sin(math.radians(halfangleA)) sideS = onehalfsideS * 2 polygonCircumference = numSides * sideS pi = polygonCircumference/2 return pi

Accumulator Pattern >>> acc=0 >>> for x in range(1,6): acc = acc + x

Figure 2.5

Leibniz Formula Summation of terms Use accumulator pattern to add up the terms More terms makes the approximation better

Figure 2.6

Figure 2.7

Listing 2.3 def leibniz(terms): acc = 0 num = 4 den = 1 for aterm in range(terms): nextterm = num/den * (-1)**aterm acc = acc + nextterm den = den + 2 return acc

Wallis Formula Product of terms Use accumulator pattern again – This time multiply instead of add – Need to initialize with 1 not 0

Figure 2.8

Listing 2.4 def wallis(pairs): acc = 1 num = 2 for apair in range(pairs): leftterm = num/(num-1) rightterm = num/(num+1) acc = acc * leftterm * rightterm num = num + 2 pi = acc * 2 return pi

Monte Carlo Simulation Use random numbers to compute an approximation of pi Simulation of a special game of darts Randomly place darts on the board pi can be computed by keeping track of the number of darts that land on the board

Figure 2.9

Figure 2.10

Selection Statements Ask a question (Boolean Expression) Based on the answer, perform a task

Figure 2.11

Figure 2.12

Listing 2.5 import random import math def montePi(numDarts): inCircle = 0 for i in range(numDarts): x = random.random() y = random.random() d = math.sqrt(x**2 + y**2) if d <= 1: inCircle = inCircle + 1 pi = inCircle/numDarts * 4 return pi

Figure 2.13

Listing 2.6 import random import math import turtle def showMontePi(numDarts): wn = turtle.Screen() drawingT = turtle.Turtle() wn.setworldcoordinates(-2,-2,2,2) drawingT.up() drawingT.goto(-1,0) drawingT.down() drawingT.goto(1,0) drawingT.up() drawingT.goto(0,1) drawingT.down() drawingT.goto(0,-1) circle = 0 drawingT.up()

Listing 2.6 continued for i in range(numDarts): x = random.random() y = random.random() d = math.sqrt(x**2 + y**2) drawingT.goto(x,y) if d <= 1: circle = circle + 1 drawingT.color("blue") else: drawingT.color("red") drawingT.dot() pi = circle/numDarts * 4 wn.exitonclick() return pi

Figure 2.14