Download presentation
Presentation is loading. Please wait.
Published byGregory Cummings Modified over 9 years ago
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!
3
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”
4
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!
5
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
6
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.
7
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
8
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
9
Function (in Python) def g(x): return(x ** 3 + 1) g(x) = x 3 + 1 g(2) = 9 g(5) = 126
10
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
11
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
12
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.
13
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
14
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
15
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
16
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.