Learn Python Basics: First Program, Atomic Data, and Functions
This content covers the basics of Python programming, starting with opening a file in the shell, working with atomic data, building compound expressions, understanding the order of operators, saving files in IDLE, and creating functions in Python. Each section provides practical examples and step-by-step instructions to help beginners grasp fundamental concepts in Python programming.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation 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) parentheses x ** y Power (right associative) x * y, x / y, x // y, x % y Multiplication, division, floor division, modulo x + y, x - y Addition, subtraction 2. 3. 4. 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 16/2 ** 2
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.
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) g(x) = x3 + 1 g(2) = 9 g(5) = 126 def g(x): return(x ** 3 + 1)
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) g(x) = x3 + 1 g(2) = 9 g(5) = 126 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))
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? 1. Name of function? func(x,y) = x*y func(2,7) = 14 func(5,4) = 20 2. Input? 3. Output? 4. Test cases? 5. Calculations? Can we now write the function? def arearectangle(len,width): return(len*width)
FUNCTION NAMES: Naming Rules: cat@yahoo Must start with a letter 4Cats not a number! Cat_n_Mouse Can only contain letters and numbers NO SPACES!!!! Cat-tail NO SPECIAL CHARACTERS! Cat123 Anything that isn t a letter or a number * & ^ % $ $ # @ ! `~ + = - ) ( : ; ><?/, etc. cats4sale Other than _ (the underscore) Cat n Mouse
OTHER FUNCTIONS? Fahrenheit to celsius? Take the temperature in Fahrenheit and subtract 32. Divide by 1.8. The result is degrees Celsius. 1. Function name? 2. Input? 3. Output? 4. Calculations? 5. Test cases?
1. Function name? 1. Input? func(x) = (x-32)/1.8 func(68) = 20.0 func(22) = -5.5556 2. Output? 3. Calculations? 4. Test Cases? Function?
TRY: Write a function that calculates the slope of a line, which is calculated as follows: given point 1 with coordinates x1, y1 and point 2 with coordinates x2,y2, the slope is calculated by: y2-y1 x2-x1 Make sure you print out the returned slope. pt1: 4,3 pt2: 8,5 slope: .5 Pt1:9,9 pt2: 1,2 slope: .875 Pt1: 18,1 pt2: 1,18 slope: -1
def slope(x1,y1,x2,y2): return((y2-y1)/(x2-x1)) print(slope(4,3,8,5)) print(slope(9,9,1,2)) print(slope(18,1,1,18))
TRY: 3 5 2 newfunc newfunc newfunc 27 3125 4 5,3 18,6 7,2 newfunc2 newfunc2 newfunc2 2 0 1 3,4,2 7,6,2 4,21,6 newfunc3 newfunc3 newfunc3 5 10 7
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))
Would it have been easier if Id said: This function takes an integer as an input value and returns an integer. It calculates the input to the power of the input and returns that value 3 newfunc 27 5 newfunc 3125 2 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 2 18,6 newfunc2 0 7,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 5 7,6,2 newfunc3 10 4,21,6 newfunc3 7
COMMENTS #This function calculates the input value raised to the power of the # input value and returns that value #input: an integer #output: an integer #Test Cases: # print(newfunc(3)) -> 27 # print(newfunc(5)) -> 3125 # print(newfunc(2))-> 4 #Author: Debra Yarrington #Feb 6, 2016 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 WEVE 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): return(x**3) # returns the cube of a number # code for a function that 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 When the function is done being executed, 8 is returned (i.e., output from the function) and the instructions are removed from memory (RAM). Only 8 remains. Thus, for our purposes, f(2) is exactly the same thing as the number 8.
USING FUNCTIONS: Remember: after we use a function, what remains is what is returned from the function 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)) def f1(x, y): print(f3(3,2)) return(y- x) def f4(p1,p2): return(f2(p2,p2) - f1(p1,p1)) print(f1(2,4)) #2 print(f4(4,2)) def f2(x1,x2): def f5(q1,q2): return(f2(q2,q1)) return(x2+x1**2) print(f5(17,5)) print(f2(3,6)) #15 def f6(par1,par2): return( 3 + f1(par1, 17+par1)) print(f6(4,26))
def Func1(p1,p2): return(Squr(p1) - Squr(p2)) print(Func1(4,3)) >> def Func2(p1,p2,p3): Given the function def Squr(par1): return(par1 ** 2) return(Squr(p1) * Func1(p2,p3)) print(Func2(2,3,2)) >> def Func3(p1,p2): def dbl(par2): return(par2 + par2) return(dbl(Squr(p1))) print(Func3(4)) >> def Func4(p1,p2): return(dbl(Squr(p2)+ Squr(p2)+3)) print(Func4(2,4)) >> def Func6(p1): return(dbl(dbl(dbl(Squr(p1)+1))-Squr(3))) print(Func6(-2)) >>
PIECEWISE FUNCTIONS Can we have a function like this? 32 if x > 0 ? f(x) = 0 otherwise
IF /ELSE (BRANCHING) def f(x): 32 if x > 0 _ x 0 otherwise if x > 0: f(x) = return (3**2/x) else: return (0) f(3) # this equals? f(0) # this equals? f(-2) # this equals?
PIECEWISE FUNCTIONS How about this? f(x) = -x3 + 2x if x < 0 x3 + 2x if x > 2 otherwise -1 What do you get with f(3)? f(2)? f(1)? f(-2)?
x3 + 2x if x > 2 f(x) = -x3 + 2x if x < 0 -1 otherwise IF /ELSE (BRANCHING) def f(x): if x > 2: elif x < 0: else: return (x ** 3 + 2 * x) return(-x ** 3 + 2 * x) return (-1) f(3) f(0) f(-2) # this equals? # this equals? # this equals? Aside: if we just say f(3), will the function f be executed by the CPU? Will we see the results?
COMPARATORS (RETURN T OR F) == != > < >= <= true true false true false true equal to 5==5 not equal to 8!=5 greater than 3>10 less than 5<8 greater than 6>=8 less than or equal to 6<=8 or equal to
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
IF STATEMENT STRUCTURE: if condition1 is true: execute this statement(s) elif condition 2 is true: execute this statement(s) elif condition3 is true: execute this statement(s) else: execute this statement(s) #only one else condition!
RULES FOR IF/ELIF/ELSE: 1. If/elif condition must evaluate something that is True or False if (3== 4) if (8 > 4) if (f2(3) < 4) if (func(7)!=4) 2. If does not require an elif or an else 1. Only one else if there is an else 3. The first branch that is true is executed, and nothing else: if (x > 3): return(3) elif (x > 2): return (2) 4. If the condition is False, nothing indented under the condition is executed.
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)) ?
AND def q(x): if (x>5) and (x < 10): return("just enough") elif (x >= 10) and (x < 15): return("too much") else: return("no idea") print(q(12)) 1. What does and do? 2. What type is returned from this function?
LOGICAL OPERATORS (True and True) (True and False) (False and True) (False and False) (True or True) (True or False) (False or True) (False or False) (not True) (not False) and True False False False True True True False False True or not
DIFF? def q2(x): if (x>6) or (x < 5): return("just enough") elif (x > 15) or (x < 20): return("too much") else: return("no idea") def q1(x): if (x>6) and (x < 5): return("just enough") elif (x > 15) and (x < 20): return("too much") else: return("no idea") print(q2(7)) print(q2(13)) print(q1(7)) print(q1(13))
WHAT HAPPENS? def ReturnSomething(value): if value = 1: return glub else: return blug print (ReturnSomething(1))