Download First PPT

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Big O notation wikipedia , lookup

Functional decomposition wikipedia , lookup

Continuous function wikipedia , lookup

Non-standard calculus wikipedia , lookup

Elementary mathematics wikipedia , lookup

Dirac delta function wikipedia , lookup

Function of several real variables wikipedia , lookup

Function (mathematics) wikipedia , lookup

History of the function concept wikipedia , lookup

Transcript
First Program

Open a file
 In
Shell
Type
into the file:
3

You did it!!! You wrote your first instruction, or code, in python!
Atomic Data

the lowest level of detail from which aggregate data is
computed.

the smallest unit of data you can do something with

if we give Python atomic data, Python spits it back at us
 Try
:
42
128.4
“puddle”
Expressions

Use atomic data to build compound expressions.

Compound expressions consist of two expressions (either atomic or
compound), with an operator between them
3 + 2
3 - 2
3 * 2
3 / 2
3 ** 2
(3 + 2) ** 3
(3 * 4) / 2

We’re starting to acquire TOOLS!
Order of operators:
1.
(x+y)

2.
x ** y

3.
Power (right associative)
x * y,

4.
parentheses
x // y,
x%y
Multiplication, division, floor division, modulo
x + y,

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
Files
1.
Open a file

2.
In IDLE, go to File->New Window
In New Window:

Type:
3 + 2
3.
File->Save As

4.
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.
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)
g(x) = x3 + 1
Consists of 3 parts and a name:
g(2) = 9
-> name:
g(5) = 126
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
Function (in Python)
def g(x):
return(x ** 3 + 1)
g(x) = x3 + 1
g(2) = 9
g(5) = 126
Creating a function:
Function (programming)

Function: a set of instructions (code) identified with a name

Every function in a program has a unique name

The instructions inside the function do not get executed until the
function’s name is called

Can be called again and again

Or can never be called

…which is kind of pointless
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
7,4
9,8
Code:
addfunc
addfunc
addfunc
5
11
17
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?
1.
Name of function?
2.
Input?
3.
Output?
4.
Test cases?
5.
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
Function names:

Naming Rules:

Must start with a letter

not a number!

Can only contain letters and numbers

NO SPACES!!!!

NO SPECIAL CHARACTERS!

Anything that isn’t a letter or a number


* & ^ % $ $ # @ ! `~ + = - ) ( “ ‘ : ; ><?/, etc.
Other than _ (the underscore)

cat@yahoo

4Cats

Cat_n_Mouse

Cat-tail

Cat123

cats4sale

Cat n Mouse
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)) -> 9
#
print(newfunc(5)) -> 25
#
print(newfunc(2))-> 4
#Author: Debra Yarrington
#Feb 6, 2016
def newfunc(par1):
return(par1**2) # 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


We should include comments that clearly describe how the function
works.


How many parameters go into the function in order to get the output?
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.