COMP 171: Functions, for loops and range John Barr Section 03 Slides by Toby Dragon.

Slides:



Advertisements
Similar presentations
Multiplying Polynomials
Advertisements

Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and.
Solving Linear Equations
Order of operators: x ** y x * y, x / y, x // y, x % y x + y, x - y
CMSC 104, Version 9/011 Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class Project Incremental.
Section 2.1 Solving Linear Equations
Adapted from John Zelle’s Book Slides
Vectors Sections 6.6.
CS 201 Functions Debzani Deb.
Ch 6 Sec 2: Slide #1 Columbus State Community College Chapter 6 Section 2 The Multiplication Property of Equality.
College Algebra Fifth Edition James Stewart Lothar Redlin Saleem Watson.
Solve a multi-step problem
Solve an “and” compound inequality
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.
Chapter 6: Functions.
Rewrite Formulas and Equations
Warm-up: Define the following: Square root 8. Identity Radicand ratio
1.3 “Solving Linear Equations” Steps: 1.Isolate the variable. 2.To solve when there is a fraction next to a variable, multiply both sides by the reciprocal.
Introduction to Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Subtract b from each side. Write original equation. Solve ax + b = c for x. STEP 1 SOLUTION Solve ax +b = c for x. Then use the solution to solve 2x +
3-7 HW: Pg #6-18eoe, 20-28e, 33-35,
College Algebra Sixth Edition James Stewart Lothar Redlin Saleem Watson.
Functions Part I (Syntax). What is a function? A function is a set of statements which is split off into a separate entity that can be used like a “new.
Chapter 6 Functions -- QuickStart. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. What is a function?
Chapter 2 Examples Section 2 Linear Functions. Objective: Students will identify patterns with linear forms of equations and functions. They will also.
“A man with a watch knows what time it is
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Copyright © 2013 Pearson Education, Inc. Section 2.4 Formulas.
Chapter 1. Objectives To provide examples of computer science in the real world To provide an overview of common problem- solving strategies To introduce.
Jeopardy Functions Review. Solve equations 100 ANSWER.
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
College Algebra Sixth Edition James Stewart Lothar Redlin Saleem Watson.
Warm-Up Exercises Divide each side by 2. Write original equation. Write 3x + 2y = 8 so that y is a function of x. EXAMPLE 2 Rewrite an equation Subtract.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Area With Mrs Ford. What is a Quadrilateral? A figure that has 4 sides and 4 angles. Example Square Rectangle.
Area of a quadrilateral is when you calculate Length X Width length width Area = length x width.
Calling Methods. Review  We can declare and create objects: Dinosaur dino; dino = new Dinosaur();  We can also shortcut the first two lines: Dinosaur.
One step equations Add Subtract Multiply Divide  When we solve an equation, our goal is to isolate our variable by using our inverse operations.  What.
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
Take out a calculator to try to solve the following problems on area of composite figures.
Warm-Up Exercises Subtract b from each side. Write original equation. Solve ax + b = c for x. STEP 1 SOLUTION Solve ax + b = c for x. Then use the solution.
2. WRITING SIMPLE PROGRAMS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
3.8 Warm Up Warm Up Lesson Quiz Lesson Quiz Lesson Presentation Lesson Presentation Rewrite Equations and Formulas.
Solving 2-Step Variable Equations What??? I just learned 1-step! Relax. You’ll use what you already know to solve 2-step equations.
Warm Up: Evaluate each expression for y = 3.
Temperature.
Literal Equations.
Solve an “and” compound inequality
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.
Perimeter & Area. Today’s Objectives:  Learn what it means to find perimeter and area.  Practice finding or estimating the perimeter and area.
Fundamental Programming Fundamental Programming Data Processing and Expressions.
Chapter 6 Section 6. EXAMPLE 1 Graph simple inequalities a. Graph x < 2. The solutions are all real numbers less than 2. An open dot is used in the.
What do you notice about this new relation? Solve each equation for the given variable. 1. in terms of b 5. in terms of r 3. in terms of m 2. in terms.
Factoring Polynomials Grouping, Trinomials, Binomials, GCF & Solving Equations.
Section 6.2 Solving Linear Equations Math in Our World.
Functions. What is a Function?  We have already used a few functions. Can you give some examples?  Some functions take a comma-separated list of arguments.
Python Programming Module 3 Functions Python Programming, 2/e1.
simplify radical expressions involving addition and subtraction.
Rewrite a formula with three variables
Using Distributive Property
exa.im/stempy16.files - Session 12 Python Camp
Functions and Procedures
Lesson 2.1 How do you use properties of addition and multiplication?
Solving for Variables Section 1.6.
Multiplication using the grid method.
Solve Multi-step Equations
10/10/ Bell Work Write and answer the following questions.
Adding and Subtracting Radicals
Equations Systems.
Presentation transcript:

COMP 171: Functions, for loops and range John Barr Section 03 Slides by Toby Dragon

What is a function? a block of code that we can call with a single name.  "Black Box" defined in order to provide functionality without dealing with how it works Some functions are provided to you:  input (), print () Some functions you have written:  drawRect(length, width)

Why functions? break code into smaller, functional pieces  testing parts before trying to solve everything  Make pieces that can be reused for multiple purposes simplify (abstract) to make your program easier to understand and to use

Functions: defining the "Black Box" def drawInitials (): Tell python we're defining a function "Name" that we decide place for parameters (empty if none) tells python that the function is starting, everything indented after this will be part of the function

Parameters def drawInitials (): def drawSquare (size): def drawRect (length, width): No parameters One parameter Two parameters

Parameters Parameters answer questions the function has.  e.g., drawRect needs to know the length and width of the rectangle you want drawn Use parameters only when necessary!

Practice Write a function that takes the temperature in Fahrenheit and prints the temperature in Celsius.  To calculate Celsius from Fahrenheit: subtract 32 multiply by 5/9

Practice Write a function that takes the temperature in Fahrenheit and prints the temperature in Celsius.  To calculate Celsius from Fahrenheit: subtract 32 multiply by 5/9

Functions: using the "Black Box" when we use a function, we say we are calling the function. Always end in parentheses!  Otherwise, python treats it as a variable Between parentheses you give values to the parameters

Calling functions and sending Parameters drawInitials () drawSquare (100) sides=100 ends = 200 drawInitials (sides, ends)

the for loop for num in range(0, 10, 1): indented code below it will happen multiple times, each with num = a number from the range. variable you create range function

Repetition: the range function range (10) – give each number, starting at 0 to less than 10 range (3, 10) – give each number, starting at 3 to less than 10 range (2, 20, 2) – give each number starting at 2, to less than 20, and count by 2 name of the range function Parameter(s)

Repetition: range practice [0, 1, 2, 3,..., 19] [2, 4, 6,..., 46, 48] [50, 48,..., 0]

Repetition: for loop practice Write a for loop that prints: … 18

for loop practice Write a program to print the Celsius equivalent of every 10 degrees Fahrenheit between 0 and 100. Use your function from earlier!