Comp. Sci. I Lectures 5-6 Notes
Comp. Sci. I Lectures 5-6 Notes 1100
Popular in Computer Science I
Popular in ComputerScienence
This 4 page Class Notes was uploaded by Chris Fall on Friday February 12, 2016. The Class Notes belongs to 1100 at Rensselaer Polytechnic Institute taught by Charles Stewart in Spring 2016. Since its upload, it has received 23 views. For similar materials see Computer Science I in ComputerScienence at Rensselaer Polytechnic Institute.
Reviews for Comp. Sci. I Lectures 5-6 Notes
Report this Material
What is Karma?
Karma is the currency of StudySoup.
You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!
Date Created: 02/12/16
Chris Fall 2/8/16 Comp. Science I Lecture 5 1. Functions 1.1.They take as input zero(generally one) or more arguments 1.2.Calculates a new value, string, or number 1.3.Returns the value, so it can be assigned to a variable or output 1.4.Using a built-in function: 1.4.1. Len(“RPI”) = 3 1.4.2. The “len” function is the function 1.4.3. The input argument is “RPI” 1.4.4. “3” is the computation 1.5.The “def” function(also a Python keyword) allows the user to create and define new functions 2. Flow of Control 2.1.Python has a particular way of analyzing functions 2.2.First, it reads the function without executing it 2.3.Next, it sees a “call” to the function(where the function is created usually), goes to that call, and executes the function from the beginning 2.4.Lastly, it returns back to where in the program the function was called and continues executing 3. Arguments, Parameters, and Local Variables 3.1.An argument was used when the string “RPI” was used earlier in these notes 3.2.Parameters are often variables that arguments get passed through in order to be input into a function 3.3.Any variable contained within a newly defined function is called a local variable 3.4.If a user tries to use local variables outside of the function they are solely defined for, Python will return a syntax error 4. Storing Functions in a Python File 4.1.In application, we rarely will only use the interpreter to input code; most of the time we will type our code into a file with the rest of the file, save the file, and run the program 5. Functions with Multiple Arguments/ Parameters 5.1.Let’s use this function as an example: 5.1.1. Import Math def Volume(radius, height): return math.pi * radius ** 2 * height 5.2.Python decides what parameter to pass the argument through based on the order of the parameters in the function 5.3.In this case, the first argument will go to the radius parameter, as it is the first parameter in the function, and the second argument will be passed through the height parameter 6. Revisiting the Program Structure 6.1.First comes a general comment describing the overall purpose of the program 6.2.Next comes any import statements that need to be used (import math, etc.) 6.3.After that, any function that needs to be defined is defined 6.4.Lastly comes the actual code that makes up the program itself, aka the main body of the program 7. Function that Return No Values 7.1.Some Python functions don’t return a value; they usually print out their result 7.2.Take the example from 5.1.1.; let’s say we use this example: 7.2.1. Volume(1,1) 7.3.When it comes to this, there is no return and therefore no value printed when this is typed in 7.4.If one were to type “print Volume(1,1)”, it would return back the string “None” as there is no value to be returned 8. Functions that Call Other Functions 8.1.Take a defined function such as def Area_Circle(radius): return math.pi * radius**2 8.2.Now, pretend another defined function calls back to this function: 8.2.1. def area_cylinder(radius,height): circle_area = area_circle(radius) height_area = 2 * radius * math.pi * height return 2*circle_area + height_area 8.3.Now imagine this set of commands followed these two functions: 8.3.1. print 'The area of a circle of radius 1 is', area_circle(1) r = 2 height = 10 print 'The surface area of a cylinder with radius', r print 'and height', height, 'is', area_cylinder(r,height) 8.4.The first print statement will return the value of area_circle when 1 is inputted into that function 8.5.The second print statement will print that statement with the value of r at the end of the line, which is 2 8.6.The third print statement will take the value of r, and first input it into the area_circle function, since that is the first part of the area_cylinder function 8.7.It will also use that value of r as radius and the value of height in the area_cylinder function 8.8.Lastly, it will print the result of the function given those two values 2/11/16 Comp. Sci. I Lecture 6 1. Boolean Values 1.1.Another data type 1.2.They can only be either True or False 1.3.There are many operations that produce Boolean values, including ones using relational operators that have been used here before, including =, and <=, etc. 1.4.Variables can be assigned Boolean values as well 1.4.1. Ex: x = True 2. Less Than / Greater Than 2.1.These are used to compare values, possibly values associated with variables, and in this particular application, also produce Boolean values 2.1.1. X= 17, y= 15.1, x<y = False 2.2.The example in 2.1.1. is essentially the same as asking whether 17 is less than 15.1, which of course is false, because 17 is greater than 15.1 2.3.These equations can be assigned to variables as well 2.3.1. Val = x<y 2.4.Combining the less than or greater than sign with the equals sign creates the “less than or equal to” or “greater than or equal to” operators 2.4.1. Take “less than or equal to” as an example; if we have two variables, x and y, for the equation x <= y would return true if x is less than OR equal to y; the opposite goes for greater than or equal to, for the equation x >= y, it will return true if x is greater than or equal to y 2.5.This can be done with strings, however the results are harder to grasp 2.6.For strings, all uppercase values come before lowercase values 2.6.1. For example, take the strings s1= “Art” and s2 = “art”. Since s1 has the capital letter and its’ beginning, it is considered to be “less than” s2, a.k.a. s1 < s2 3. Other Relational Operators 3.1.X == y would return true if X and y equaled the same value 3.2.X != y would return true if X and y were NOT equal to the same value 4. If Statements 4.1.These statements give one or more conditions in which if that condition is met then the output specifically linked to that condition will occur 4.2.The format is quite similar to a hypothesis in the scientific method(if-then) 4.3.For example, take the values of x and y we had before in 2.1.1.; we could say: “If x < y: print “X is less than Y” 4.3.1. Since the condition of x < y is met, the phrase “X is less than Y’ would be printed in this instance 4.4.For a conditional statement and a command to be performed if the previous is false, the “else” clause is used 4.4.1. ‘If x > y: print “X is greater than Y” else: print “X is less than Y”’ 4.4.2. In this case, the x > y condition is not met, so the “else” condition and command is run 4.5.For two or more conditional statements in the same if block, the “elif” command(short for elseif) is used 4.5.1. Let’s change the value of x and y both to 10; knowing this, here’s an example of an if block: 4.5.2. “If x< y: print “X is less than Y” elif x> y, print “X is greater than Y” else: print “X and Y are equal” 4.5.3. Python will cycle through all of the conditions in the if block, and since x is neither less than or greater than y, the “else” clause runs 4.5.4. The “elif” command will only even be considered if there has been an initial if statement/ condition in that block of code that has been ruled false before it 5. The Boolean Expression “Or” 5.1.Take the following if statement: 5.1.1. If temperature < 0 or temperature > 100: print “At this temperature water is not a liquid” 5.2.This expression is true if one of 3 possible cases happen: 5.2.1. The first relational expression (temperature < 0) is true 5.2.2. The second relational expression (temperature > 100) is true 5.2.3. Both expressions are true 5.3.It will only return false if both expressions are false 6. The Boolean Expression “And” 6.1.Take the last if statement with a slight twist: 6.1.1. If temperature > 0 and temperature < 100: print “At this temperature water is a liquid” 6.2.This will only return true if both expressions are true 6.3.Any other possible case(first one true only, second one true only, both false) will return false 6.4.In any case the if statement returns false with either “or” or “and”, there simply won’t be any return whatsoever; it will simply skip to the next line 7. The “Not” Operator 7.1.If the word “Not” is thrown into the if statement, it will return the opposite of what you would expect it to 7.2.This means that if without the word “not” you expect the if statement to return true, it will return false and vice versa 7.3.This flips the roles of “or” and “and” as now “or” will now only have 1 way to return true and “and” will have 3 ways to return true, the opposite of what the case was before
Are you sure you want to buy this material for
You're already Subscribed!
Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'