Download presentation
Presentation is loading. Please wait.
Published byGwendolyn Marsh Modified over 9 years ago
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!
2
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”
3
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!
4
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
5
Files 1. 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.
6
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) = x 3 + 1 g(2) = 9 g(5) = 126
7
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) = x 3 + 1 g(2) = 9 g(5) = 126
8
Function (in Python) def g(x): return(x ** 3 + 1) g(x) = x 3 + 1 g(2) = 9 g(5) = 126
9
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
10
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) = x 3 + 1 g(2) = 9 g(5) = 126
11
Input Values: Parameters values into the function are known as parameters 3,2 addfunc 5 7,4 addfunc11 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.
12
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
13
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
14
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,6newfunc3 7
15
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))
16
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 27 5newfunc 3125 2newfunc 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 2 18,6 newfunc2 0 7,2newfunc2 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 5 7,6,2 newfunc3 10 4,21,6 newfunc3 7
17
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
18
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.