First Program Open a file In Shell Type into the file: 3 You did it!!! You wrote your first...
-
Upload
gwendolyn-marsh -
Category
Documents
-
view
212 -
download
0
Transcript of First Program Open a file In Shell Type into the file: 3 You did it!!! You wrote your first...
![Page 1: First Program Open a file In Shell Type into the file: 3 You did it!!! You wrote your first instruction, or code, in python!](https://reader035.fdocuments.us/reader035/viewer/2022070409/56649e805503460f94b847d3/html5/thumbnails/1.jpg)
First Program
Open a file In Shell
Type into the file:
3
You did it!!! You wrote your first instruction, or code, in python!
![Page 2: First Program Open a file In Shell Type into the file: 3 You did it!!! You wrote your first instruction, or code, in python!](https://reader035.fdocuments.us/reader035/viewer/2022070409/56649e805503460f94b847d3/html5/thumbnails/2.jpg)
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 :42128.4“puddle”
![Page 3: First Program Open a file In Shell Type into the file: 3 You did it!!! You wrote your first instruction, or code, in python!](https://reader035.fdocuments.us/reader035/viewer/2022070409/56649e805503460f94b847d3/html5/thumbnails/3.jpg)
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!
![Page 4: First Program Open a file In Shell Type into the file: 3 You did it!!! You wrote your first instruction, or code, in python!](https://reader035.fdocuments.us/reader035/viewer/2022070409/56649e805503460f94b847d3/html5/thumbnails/4.jpg)
Order of operators:1. (x+y)
parentheses
2. x ** y Power (right associative)
3. x * y, x / y, x // y, x % y Multiplication, division, floor division, modulo
4. 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
![Page 5: First Program Open a file In Shell Type into the file: 3 You did it!!! You wrote your first instruction, or code, in python!](https://reader035.fdocuments.us/reader035/viewer/2022070409/56649e805503460f94b847d3/html5/thumbnails/5.jpg)
Files1. Open a file
In IDLE, go to File->New Window
2. In New Window: Type:
3 + 2
3. File->Save As Save the file as first.py
4. 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.
![Page 6: First Program Open a file In Shell Type into the file: 3 You did it!!! You wrote your first instruction, or code, in python!](https://reader035.fdocuments.us/reader035/viewer/2022070409/56649e805503460f94b847d3/html5/thumbnails/6.jpg)
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) = 6f(5) = 10
g(x) = x3 + 1
g(2) = 9
g(5) = 126
![Page 7: First Program Open a file In Shell Type into the file: 3 You did it!!! You wrote your first instruction, or code, in python!](https://reader035.fdocuments.us/reader035/viewer/2022070409/56649e805503460f94b847d3/html5/thumbnails/7.jpg)
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 + 1g(2) = 9g(5) = 126
![Page 8: First Program Open a file In Shell Type into the file: 3 You did it!!! You wrote your first instruction, or code, in python!](https://reader035.fdocuments.us/reader035/viewer/2022070409/56649e805503460f94b847d3/html5/thumbnails/8.jpg)
Function (in Python)
def g(x):
return(x ** 3 + 1)
g(x) = x3 + 1g(2) = 9g(5) = 126
![Page 9: First Program Open a file In Shell Type into the file: 3 You did it!!! You wrote your first instruction, or code, in python!](https://reader035.fdocuments.us/reader035/viewer/2022070409/56649e805503460f94b847d3/html5/thumbnails/9.jpg)
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
![Page 10: First Program Open a file In Shell Type into the file: 3 You did it!!! You wrote your first instruction, or code, in python!](https://reader035.fdocuments.us/reader035/viewer/2022070409/56649e805503460f94b847d3/html5/thumbnails/10.jpg)
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) = 9g(5) = 126
![Page 11: First Program Open a file In Shell Type into the file: 3 You did it!!! You wrote your first instruction, or code, in python!](https://reader035.fdocuments.us/reader035/viewer/2022070409/56649e805503460f94b847d3/html5/thumbnails/11.jpg)
Input Values: Parameters values into the function are known as parameters
3,2 addfunc 57,4 addfunc 119,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.
![Page 12: First Program Open a file In Shell Type into the file: 3 You did it!!! You wrote your first instruction, or code, in python!](https://reader035.fdocuments.us/reader035/viewer/2022070409/56649e805503460f94b847d3/html5/thumbnails/12.jpg)
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
![Page 13: First Program Open a file In Shell Type into the file: 3 You did it!!! You wrote your first instruction, or code, in python!](https://reader035.fdocuments.us/reader035/viewer/2022070409/56649e805503460f94b847d3/html5/thumbnails/13.jpg)
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
![Page 14: First Program Open a file In Shell Type into the file: 3 You did it!!! You wrote your first instruction, or code, in python!](https://reader035.fdocuments.us/reader035/viewer/2022070409/56649e805503460f94b847d3/html5/thumbnails/14.jpg)
Try:
3 newfunc 27
5 newfunc 3125
2 newfunc 4
5,3 newfunc2 2
18,6 newfunc2 0
7,2 newfunc2 1
3,4,2 newfunc3 5
7,6,2 newfunc3 10
4,21,6 newfunc3 7
![Page 15: First Program Open a file In Shell Type into the file: 3 You did it!!! You wrote your first instruction, or code, in python!](https://reader035.fdocuments.us/reader035/viewer/2022070409/56649e805503460f94b847d3/html5/thumbnails/15.jpg)
Code:
def newfunc(par1): return(par1**par1)
def newfunc2(par1,par2): return (par1 % par2)
def newfunc3(par1, par2, par3): return(par1+(par2//par3))
print(newfunc(3))print(newfunc(5))print(newfunc(2))
print(newfunc2(5,3))print(newfunc2(18,6))print(newfunc2(7,2))
print(newfunc3(3,4,2))print(newfunc3(7,6,2))print(newfunc3(4,21,6))
![Page 16: First Program Open a file In Shell Type into the file: 3 You did it!!! You wrote your first instruction, or code, in python!](https://reader035.fdocuments.us/reader035/viewer/2022070409/56649e805503460f94b847d3/html5/thumbnails/16.jpg)
Would it have been easier if I’d said:“This function takes an integer as an input value and returns an integer. It calculates the square of the input and returns that value”
3 newfunc 275 newfunc 31252 newfunc 4
“This function takes as input 2 integers and returns an integer. It divides the first input integer by the second input integer and returns the remainder. In other words, it calculates the following: num1 % num2.”
5,3 newfunc2 218,6 newfunc2 07,2 newfunc2 1
“This function takes 3 integers as input and returns an integer. It floor-divides the second input integer by the third input integer. It takes the result of that and adds it by the first input integer. In other words, it calculates the following: num1+(num2//num3)”
3,4,2 newfunc3 57,6,2 newfunc3 104,21,6 newfunc3 7
![Page 17: First Program Open a file In Shell Type into the file: 3 You did it!!! You wrote your first instruction, or code, in python!](https://reader035.fdocuments.us/reader035/viewer/2022070409/56649e805503460f94b847d3/html5/thumbnails/17.jpg)
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
![Page 18: First Program Open a file In Shell Type into the file: 3 You did it!!! You wrote your first instruction, or code, in python!](https://reader035.fdocuments.us/reader035/viewer/2022070409/56649e805503460f94b847d3/html5/thumbnails/18.jpg)
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.