Comp Sci. I Note Summary for Test 1
Comp Sci. I Note Summary for Test 1 1100
Popular in Computer Science I
Popular in ComputerScienence
This 9 page Study Guide was uploaded by Chris Fall on Friday February 19, 2016. The Study Guide belongs to 1100 at Rensselaer Polytechnic Institute taught by Charles Stewart in Spring 2016. Since its upload, it has received 65 views. For similar materials see Computer Science I in ComputerScienence at Rensselaer Polytechnic Institute.
Reviews for Comp Sci. I Note Summary for Test 1
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/19/16
2/19/16 Comp.Sci.I Study Guide/ Note Summary for Test 1 Lecture 2 1. Using Python as an Interactive Calculator It is possible to type and evaluate basic mathematical expressions directly into the interpreter Ex: 3 * 5= 15 and other similar examples 2. Python Types A type is a set of possible values (a.k.a. a “representation”), and the set of operations on those values 3. Integers Includes all whole numbers The operators on numbers are: +, -, *, /, %, ** All operators work the same as expected except for “/” and “%” “/” gives the highest possible dividend positively or smallest number negatively (Ex: 29 / 6 = 4, -4 /6 = -1) “%” gives the remainder (Ex: 29 % 6 = 5) “**” represents raising a number to a power 4. Float Values Number with decimal attached(1.5, 3.14159, -7.89) If result is too large involving numbers, an “overflow error” will occur, meaning there are too many digits for Python to fully process and display 5. Mixing Integers and Floats To make any integer a float value, simply add a decimal point after it (5.) When integers and float values are mixed in a computation, everything is converted to float values (when needed- on an operation to operation basis) 6. Variables and Assignment Python uses a variable system in order to assign numbers to particular variables in memory to make computation easier These variables can be used with operators just like integers and floats Must manually set these variables in the interpreter or in code(Ex: pi = 3.14159) These variables can be individual letters or full words The equals sign(=) has a different meaning in Python It essentially means “assign to” The value of the variable is substituted in for the variable when the variable appears to the right of the = Values are assigned to the variable when the variable name appears on the left side of the =(the variable will be set to whatever value is present on the right side) 7. Variable Names Variable names can only start with letters or underscores, and then be followed by any number of letters, underscores, or digits Cannot contain any spaces, and cannot start with a number Variable names are case-sensitive(Ex: Rpi is not the same as rpi) 8. Syntax/Semantic Errors Syntax errors prevent the program from running, whereas semantic errors will allow the program to run, but it will give an output that is not desired or is different from what is expected and/or meant 9. Python Keywords There are certain words that can’t be used in variable names; these are called keywords 31 of these words exist in this current version of Python If any of these words are used in or as a variable, the program will return a syntax error 10.Do Variables exist before they are given a value? No, they must be assigned a value before they are used in any given calculation 11.Expressions Expressions are legal combinations of values, operators, and variables They can be as simple as a single value, but the power of Python allows they to be arbitrarily complicated 12.Precedence The order of operations that are applied is important The order is: Parentheses, “**” from right to left, Unary + and -, “*” “/” and “%” from left to right, and “+” and “-“ from left to right 13.Mixed Operators Assignments of the form “i = i + 1” are common in Python, and the program contains shorthand for this type of situation The shorthand is: i += 1 There are other examples(Ex: -=, *=, both of which carry the same meaning as +=, except with the – sign and * sign respectively) Lecture 3 1. Definition of a String 1.1.A string is a sequence of 0 or more characters delimited by single or double quotes 1.2.A string must begin and end on the same quotes 1.2.1. For example, “Pie” or ‘Pie’ 1.2.2. If a string like “Pie’ or ‘Pie” is input, Python will return back an end of line syntax error 1.3.If numbers are surrounded in quotes such as in strings, they are treated as strings and not as numbers 1.3.1. Ex: ‘4 8 12 16’ is a string, simply listing 4 8 12 16 keeps the numbers as integers 1.4.An empty string (‘ ‘) is still valid 1.5.Strings, just like integers and floats, can be assigned to variables 1.5.1. Ex: nums = “4 8 12 16” 1.6.Strings are printed differently depending on the commands inputted to print it out 1.6.1. If the command “print” is used, the string returns without any quotes (4 8 12 16) 1.6.2. If the variable name is just typed into the interpreter, the string returns with single quotes (‘4 8 12 16’) 2. Multi-line Strings 2.1.In defining a string in which 3 sets of single or double quotes(“””/ ‘’’) are used, it is possible to type multiple lines within one string 2.2.Until a matching set of 3 single/double quotes are inputted, Python will assume the string is continuing on indefinitely 2.3.Simply typing out the variable associated with the string will print a “\n” between each line while printing the string on the same line 2.3.1. “\n” is a marker that denotes the end of a line, and the creation and start of a new line 3. Escape Characters 3.1.When a back slash is inputted into code, it tells Python that a special character is coming next 3.2.\n has been mentioned 3.3.\t will skip to the next “tab stop” in the text to allow output in columns 3.4.\’ or \” will tell the interpreter to ignore the marking as a string starter or ender and will keep it as normal text within the string 3.5.\\ will indicate that the back slash needs to put in as text instead of denoting an escape character 4. String Operations 4.1.Concatenation 4.1.1. Concatenation takes 2 or more strings and combines them into one string 4.1.2. This is done using the “+” sign 126.96.36.199. Ex: s0 = “Hello” s1 = “World” s0 + s1 = “HelloWorld” 188.8.131.52. Adding strings to this also works(Ex: s0 + ‘ ‘ + s1 = “Hello World”) 4.1.3. Putting multiple strings on the same line comes to the same effect 184.108.40.206. Ex: ‘Good’ ‘Morning’ ‘America’ = ‘GoodMorningAmerica’ 4.1.4. Putting s0 s1 does not work 4.2.Replication 4.2.1. Strings can be replicated using the multiplication operator(*) 220.127.116.11. Ex: s = “Ha” s * 5 = “HaHaHaHaHa” 4.2.2. This can only be done with integers or variables with integers assigned to them, not floats 4.2.3. Multiplying by 0 or a negative number will give an empty string 4.3.Length Function 4.3.1. This determines how long a string is 4.3.2. Denoted by len(string) 4.3.3. When the length is taken, the quotes on the ends do not count towards the length 18.104.22.168. Ex: len(s) = 2 4.3.4. Escape characters only count as 1 characters despite the fact that they contain 2 22.214.171.124. Ex: len(\n) = 1 4.4.String Function 4.4.1. This function converts other values to strings 126.96.36.199. Ex: str(4.1) = ‘4.1’ 4.5.Float Function 4.5.1. This function converts other values to floats 188.8.131.52. Ex: float(4) = 4.0 4.5.2. The value must be able to convert to a float, therefore trying to convert a string to float will result in a syntax error 5. Formatted Output 5.1.Conversion Specifiers 5.1.1. The percent sign(%) is used to convert values into other types of values within a print statement 5.1.2. To use it, you put a percent sign, followed by a decimal point, then the number of decimal place desired, and lastly the value you want to convert to 184.108.40.206. Ex: %.3f will convert a value to a float with 3 decimal places 5.1.3. At the end of the print statement, you must put a percent sign followed by the variables you want to convert in parentheses 220.127.116.11. Ex: %(area, volume) 5.1.4. %f is for integers, %d is for strings 6. Raw Input Function 6.1.The raw_input function allows Python programs to prompt users for input 6.2.It waits for the user to input something, which is read as a string 6.3.This string can be converted to an integer or float Lecture 4 1. More String Functions 1.1.All of the functions we’ve learned so far have left the original string unchanged 1.2.The “lower” function take a string and converts all of the characters to lowercase 1.2.1. Name = “Chris” Name.lower = “chris” 1.3.The “upper” function converts all characters to uppercase 1.3.1. Name.upper = “CHRIS” 1.4.The “capitalize” function capitalizes the first letter of the string 1.4.1. Name2 = “chris fall” Name2.Capitalize = “Chris fall” 1.5.The “title” function capitalizes the first letter of every word in the string 1.5.1. Name2.title = “Chris Fall” 1.6.The “replace” function will take specified sets of substrings within the string and replaces them with other substrings 1.6.1. Name3 = “Computer Science” Name3.replace(“e”, “f”) = “Computfr Scifncf” 1.7.The “find” function will return back the first occurrence of a specific substring within a string 1.7.1. Each string starts with character number 0(important!) 1.7.2. Name3.find(“e”) = 6 1.7.3. Capital vs. lowercase DOES matter 1.7.4. If the string does not contain that particular substring, the function returns back a -1 1.7.5. Name3.find(“x”) = -1 1.7.6. You can particularly look for the second, or third, any amount of occurrence of a substring in a string by putting the number position you would like to find) 18.104.22.168. Name3.find(“e”, 2”) = 12 1.8.The “count” function returns how many of a particular substring are in a given string 1.8.1. Name3.count(“e”) = 3 1.9.The “strip” function will look for a specific character/substring that the user chooses and will remove any instances of that character/substring that occur on the ends of the string 1.9.1. Name4 = “ 0Cool0” Name4.strip(“0”) = “Cool” 1.9.2. Leaving the parentheses in the strip function blank will remove any spaces on the ends of the string 22.214.171.124. Name5= “ Cool “ Name5.strip() = “Cool” 1.9.3. Escape characters are affected by this function 126.96.36.199. Name6 = “/t Cool” prints “ Cool”, however Name6.strip() = “Cool” 2. Numerical Functions 2.1.The “abs” function takes the absolute value of a number 2.2.The “pow” function takes a number and raises it to a power of the user’s choice 2.3.The “int” function takes a value and converts it to an integer(if possible) 2.4.The “float” function takes a value and converts it to a float value(if possible) 2.5.The “round” function takes a number and rounds it to the nearest whole number 2.6.The “max” function takes a sequence of numbers and returns the largest value 2.7.The “min” function takes a sequence of numbers and returns the smallest value 3. Built__ins function 3.1.By typing in help(__builtins__), Python returns all of the built-in functions that it has to offer and what each function does 4. Objects 4.1.All Python variables are objects 4.2.Each object defines an organization and structure to the data they store 4.3.They have operation/functions- these are called methods- in order to access and manipulate their data 5. Math Database 5.1.By importing the math database(import math), we get access to a whole lot of new commands for things like square roots, powers, and various other numerical processes 6. From command 6.1.Using this command allows the user to only specific commands from a database without importing the entire database 6.2.The command “from math import pi, sqrt” will import only the value of pi and the square root function and nothing else from the math database 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 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'