Order of operators: x ** y x * y, x / y, x // y, x % y x + y, x - y

Slides:



Advertisements
Similar presentations
Procedures and Functions. What are they? They are both blocks of code that can be reused to perform specific task. However there is a difference: Function-
Advertisements

This Week More Types boolean string Modules print statement Writing programs if statement Type boolean.
Python Basics: Statements Expressions Loops Strings Functions.
Code: from cisc106 import * def myfunc(value1,value2): return (value1 + value2) assertEqual(myfunc(3,2),5) assertEqual(myfunc(7,4),11) assertEqual(myfunc(9,8),17)
16-May-15 Sudden Python Drinking from the Fire Hose.
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.
PYTHON: LESSON 1 Catherine and Annie. WHAT IS PYTHON ANYWAY?  Python is a programming language.  But what’s a programming language?  It’s a language.
Structure of program You must start with a module import# You must then encapsulate any while loop in a main function at the start of the program Then.
CS 116 Tutorial 5 Introduction to Python. Review Basic Python Python is a series of statements def f(p1, p2,…pn): x = 5 if x > p1: x = p1 + p2 return.
Python Programming Fundamentals
CH Programming An introduction to programming concepts.
Introduction to Python
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
Georgia Institute of Technology Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology Aug 2005.
Python Programming Using Variables and input. Objectives We’re learning to make use of if statements to enable code to ask questions. Outcomes Build an.
COMP 171: Functions, for loops and range John Barr Section 03 Slides by Toby Dragon.
First Program  Open a file  In Shell  Type into the file: 3  You did it!!! You wrote your first instruction, or code, in python!
Chapter 6 Functions -- QuickStart. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. What is a function?
PPT 2. Other things: functions  Can you have a function with no inputs?  Yes: def f( ): return (3 + 4 )  Can you have a function with no outputs? 
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
More on Functions (Part 2) Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #005 (April somthin, 2015)
Hey, Ferb, I know what we’re gonna do today! Aims: Use formatted printing. Use the “while” loop. Understand functions. Objectives: All: Understand and.
C463 / B551 Artificial Intelligence Dana Vrajitoru Python.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Decision Structures, String Comparison, Nested Structures
Python Functions : chapter 3
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Python Let’s get started!.
INTRODUCTION TO PROGRAMMING. Program Development Life Cycle The program development life cycle is a model that describes the stages involved in a program.
Programming Languages Programming languages are a compromise between spoken language and formal math. They allow humans to communicate with computers at.
COMPUTER PROGRAMMING Year 9 – Unit 9.04 Week 3. Open the Python INTERPRETER Can you use the interpreter to solve these maths problems? 156 add
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
First Program  Open a file  In Shell  Type into the file: 3  You did it!!! You wrote your first instruction, or code, in python!
COMPUTER PROGRAMMING Year 9 – lesson 1. Objective and Outcome Teaching Objective We are going to look at how to construct a computer program. We will.
CS 115 Lecture 5 Math library; building a project Taken from notes by Dr. Neil Moore.
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Machine Language Computer languages cannot be directly interpreted by the computer – they are not in binary. All commands need to be translated into binary.
More on Functions (Part 2) Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
Python Programming Module 3 Functions Python Programming, 2/e1.
PYTHON PROGRAMMING Year 9. Objective and Outcome Teaching Objective Today we will look at conditional statements in order to understand how programs can.
Math operations 9/19/16.
Fundamentals of Programming I Overview of Programming
Basic concepts of C++ Presented by Prof. Satyajit De
A Playful Introduction to Programming by Jason R. Briggs
Introduction to Python
Line Continuation, Output Formatting, and Decision Structures
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Be A programmer in Steps
Other things: functions
Introduction to Programming
Variables, Expressions, and IO
Line Continuation, Output Formatting, and Decision Structures
Escape sequences: Practice using the escape sequences on the code below to see what happens. Try this next code to help you understand the last two sequences.
Coding Concepts (Sub- Programs)
COMPUTER PROGRAMMING PYTHON
G7 programing language Teacher / Shamsa Hassan Alhassouni.
Introduction to Programming
Introduction to Programming Using Python PART 2
Introduction to Python
Python programming exercise
Operations Python code.
Unit 3: Variables in Java
General Computer Science for Engineers CISC 106 Lecture 03
Introduction to Programming
Hardware is… Software is…
COMPUTING.
Presentation transcript:

Order of operators: x ** y x * y, x / y, x // y, x % y x + y, x - y Power (right associative) x * y, x / y, x // y, x % y Multiplication, division, floor division, modulo x + y, x - y Addition, subtraction Shall we try this? 5 + 4 / 2 + 1 7//3 -7//3 5*2**3 7%2 18%4 3 + 2**3 (3 + 2) ** 3 12/2 ** 2 Class 2

Files Open a file In New Window: File->Save As Run the file: In IDLE, go to File->New Window In New Window: Type: 3 + 2 File->Save As Save the file as first.py Run the file: Run->Run Module Now you’ve saved your work so you can run it later When saving a python program, it must have a .py extension!!! So interpreter knows it’s python code. Class 1

Functions We have a file: we can save our work. Now, let’s create “functions” to name code we might want to use again Math: function takes a number or numbers and transforms it to another number E.g., f(x) = 2x f(3) = 6 f(5) = 10 g(x) = x3 + 1 g(2) = 9 g(5) = 126

Creating a function: Function (mathematical) Consists of 3 parts and a name: -> name: g (not a good name! Tells us nothing about what this function does) -> input parameter in the example, integers 2 or 5 -> instructions (code) in the example, x**3 + 1 -> output in the example, the integer 9 or 126 g(x) = x3 + 1 g(2) = 9 g(5) = 126

Function (in Python) def g(x): return(x ** 3 + 1) g(x) = x3 + 1

Function (in Python) def g(x): return(x ** 3 + 1) To Call the Functions (to make them run): g(2) g(5) To see what the function calculates (returns): print (g(2)) print (g(5)) g(x) = x3 + 1 g(2) = 9 g(5) = 126

Input Values: Parameters values into the function are known as parameters 3,2 addfunc 5 7,4 addfunc 11 9,8 addfunc 17 Code: def addfunc(value1,value2): return (value1 + value2) print(addfunc(3,2)) print(addfunc(7,4)) print(addfunc(9,8)) We should know what we want to come out of the function, so we can check to make sure the function is working correctly Print allows us to check the value that is coming out of the function.

Function: Calculate the area of a rectangle? Name of function? Input? Output? Test cases? Calculations? Can we now write the function? def arearectangle(len,width): return(len*width) func(x,y) = x*y func(2,7) = 14 func(5,4) = 20

Other functions? Fahrenheit to celsius? Function name? Input? Output? Take the temperature in Fahrenheit and subtract 32. Divide by 1.8. The result is degrees Celsius. Function name? Input? Output? Calculations? Test cases?

Function name? Input? Output? func(x) = (x-32)/1.8 Calculations? f_to_c Input? An integer (the fahrenheit temperature) Output? A float (the celsius temperature) Calculations? (ftemp – 32) / 1.8 Test Cases? f_to_c(68) -> 20.0 f_to_c(22)->-5.5556 def f_to_c(ftemp): return((ftemp - 32 )/ 1.8) print(f_to_c(32,47)) func(x) = (x-32)/1.8 func(68) = 20.0 func(22) = -5.5556 Class 3 stopped here

Comments #This function calculates the square of the input value #and returns that squared value #input: an integer #output: an integer #Test Cases: # print(newfunc(3)) -> 27 # print(newfunc(5)) -> 3125 # print(newfunc(2))-> 4 #Author: Debra Yarrington #Sept 6, 2011 def newfunc(par1): return(par1**par1) # returns the square Comments aren’t executed (aren’t converted to machine language). Python’s compiler ignores them. They’re for people who are reading your code. They also can be used to help you (and others) understand what you are doing and why

What we’ve learned about writing functions: We should come up with test cases first How many parameters go into the function in order to get the output? We should include comments that clearly describe how the function works. These comments should include our test cases After we’ve got the test cases and the function description, then we write the function. Basically, you have to think it through before you write the function.

Functions: Math: f(x) = x3 Python: def f(x): return(x**3) Given a particular input to this function, will we ALWAYS get the same output? e.g. f(2) f(3) Could we say that f(2) is equivalent to 8? Could we say that f(3) is equivalent to 27?

Functions(how they work) def f(x): # code for a function that return(x**3) # returns the cube of a number f(2) # Calls the function. The function is now executed (i.e., calculated, # converted to machine language and instructions run by the CPU). # # After f(2) runs, all that remains is what is RETURNED

Using functions: def add2(x,y): return(x + y) def add(x,y): return(add2(x,y) + add2(x,y)) print(add(7,3))

Using functions: def add2(x,y): return(x + y) def div(x,y,z): return(add2(x,y) / z) print(div(7,3,2))

Using functions: def add2(x,y): return(x + y) def div(x,z): return(add2(x,3) / z) print(div(7,2))

Using functions: def add2(x,y): return(x + y) def div(y,x): return(add2(y,3) / x) print(div(7,2))

Using functions: def add2(x,y): return(x + y) def add3(y,x): return(add2(y,3) + add2(11,x)) print(add3(7,2))

def f3(p1,p2): return(f2(p1,p2) + f1(p1,p2)) print(f3(3,2)) 10 def f4(p1,p2): return(f2(p2,p2) - f1(p1,p1)) print(f4(4,2)) 6 def f5(q1,q2): return(f2(q2,q1)) print(f5(17,5)) 42 def f6(par1,par2): return( 3 + f1(par1, 17+par1)) print(f6(4,26)) 20 def f1(par1, par2): return(par2 - par1) print(f1(2,4)) #2 def f2(x1,x2): return(x1**2 + x2) print(f2(3,6)) #15 Class 3 stopped after first example on Wed

def Func1(p1,p2): return(Squr(p1) - Squr(p2)) print(Func1(4,3)) >>7 def Func2(p1,p2,p3): return(Squr(p1) * Func1(p2,p3)) print(Func2(2,3,2)) >>20 def Func3(p1,p2): return(dbl(Squr(p1))) print(Func3(4)) >>AACH CRASH BURN def Func4(p1,p2): return(dbl(Squr(p2)+ Squr(p2)+3)) print(Func4(2,4)) >> 70 def Func5(p1): return(dbl(dbl(dbl(p1)))) print(Func5(4)) >>32 def Func6(p1): return(dbl(dbl(dbl(Squr(p1)+1))-Squr(3))) print(Func6(-2)) >>22 Given the function def Squr(par1): return(par1 ** 2) def dbl(par2): return(par2 + par2) Class 2 stopped (Wed)

Given the function def Squr(par1): return(par1 ** 2) def dbl(par2): def Func1(p1,p2): return(Squr(p1) - Squr(p2)) print(Func1(4,3)) >>7 def Func2(p1,p2,p3): return(Squr(p1) * Func1(p2,p3)) print(Func2(2,3,2)) >>20 def Func3(p1,p2): return(dbl(Squr(p1))) print(Func3(4)) >>AACH CRASH BURN def Func4(p1,p2): return(dbl(Squr(p2)+ Squr(p2)+3)) print(Func4(2,4)) >> 70 def Func6(p1): return(dbl(dbl(dbl(Squr(p1)+1))-Squr(3))) print(Func6(-2)) >>22 Given the function def Squr(par1): return(par1 ** 2) def dbl(par2): return(par2 + par2) Class 2 stopped (Wed)

Piecewise functions 32 if x > 0 𝑥 f(x) = 0 otherwise

If /else (branching) def f(x): if x > 0: return (3**2/x) else: f(3) # this equals? f(0) # this equals? f(-2) # this equals? 32 if x > 0 f(x) = x _ 0 otherwise

Piecewise functions x3 + 2x if x > 2 f(x) = -x3 + 2x if x < 0 -1 otherwise Class 3

If /else (branching) def f(x): if x > 2: return (x ** 3 + 2 * x) x3 + 2x if x > 2 f(x) = -x3 + 2x if x < 0 -1 otherwise def f(x): if x > 2: return (x ** 3 + 2 * x) elif x < 0: return(-x ** 3 + 2 * x) else: return (-1) f(3) # this equals? f(0) # this equals? f(-2) # this equals?

Comparators (return T or F) == equal to 5==5 true != not equal to 8!=5 true > greater than 3>10 false < less than 5<8 true >= greater than 6>=8 false or equal to <= less than 6<=8 true or equal to Class 3 stopped here

Note: == if conditions MUST use == (equality) == = not = (assignment) Asks a question: is this equal to that??? this == that ? Yes or No! True, this is equal to that, or False, this is not equal to that = We’ll see this in use shortly

Example def f(x): if x > 10: return (x+9) elif x < 7: return (x + 4) else: return(0) print(f(12)) # what is printed? print(f(6)) # what is printed? print(f(8)) # what is printed? print(f(7)) # what is printed?

Example def f(x): if x != 10: return (x * 2) else: return (x ** 2) print(f(6)) print(f(10))

Example def f(x): if x < 10: return (x+9) elif x == 5: return (x + 4) elif x >10: return (x) else: return(0) print(f(5)) ? Class 2 stopped here