CSC 110/127A Midterm 1 Study Guide
CSC 110/127A Midterm 1 Study Guide CSC 127A - 002
Popular in Introduction to Computer Science
CSC 127A - 002
verified elite notetaker
Popular in Computer Science and Engineering
This 8 page Study Guide was uploaded by Kate Jahaske on Friday September 16, 2016. The Study Guide belongs to CSC 127A - 002 at University of Arizona taught by Allison Osbourn in Fall 2016. Since its upload, it has received 142 views. For similar materials see Introduction to Computer Science in Computer Science and Engineering at University of Arizona.
Reviews for CSC 110/127A Midterm 1 Study Guide
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: 09/16/16
CSC 127A/110 Midterm 1 Instructor: Allison Obourn Notes Notes can be made using a "#" or three single quotes ''' surrounding a note ''' Print The print function is used to print out a string of text in your program. 1. def main(): # always have a main function 2. print("Hello world!") # Encapsulate a string with parentheses () and quotes "" 3. 4. main() # Don't forget to call main The output will be: Hello world! For the above program. "this is a legal string" 'this is a "legal" string' """this is a legal string across more than one line""" Escape sequences can be used in a print string to do the following: \t tab character \n new line \" double quotation mark \\ backslash o To show a backslash in a print statement, you must use a double \\ otherwise it will be an invalid escape sequence end="" or end='' will also create a new line Functions Functions contain information within one call within main. Function syntax: 5. def main(): 6. name() 7. 8. def name(): 9. statement 10. 11. statement 12. main() In the name of the function, separate multiple_words with an underscore. Functions can be called within other functions as well. Operators: combine multiple values or expressions. o + addition o - subtraction o * multiplication o / division o // integer division (no remainder) o % modulus (remainder) o ** exponent We will be given expressions to solve. Remember PEMDAS when solving them. Left to right, Parentheses, Exponents, Multiplication and Division, Addition and Subtraction Number typing o A conversion from one type to another. This will truncate a double from a real number to an integer o Int = integers o float = real numbers Variables Name = expression zipcode = 90210 myGPA = 1.0 = 2.25 Note: For formatting, always use lowercase at the beginning of a sentence and use underscores if it is more than one word. Once given a value, a variable can be used in expressions: X = 3 Y = 5 * x – 1 X = "hello" You can assign a variable more than once as well. The program will run top to bottom and will use the most recent variable assigned. The exception to this is with functions. Assignment uses "=" as an assignment operator, not an algebraic equation. It will mean to Python, "store the value at right in variable at left." X = 3 X = x +2 This will become 5, and will then use 5 in the future. You can also use these commands in combination with your strings for your variables: 1. veg = "potatoes" 2. veg # to print out "a" 3. 4. veg # will print out an error 5. veg[-31] # will also print out an error 6. 7. veg[4:5] # will only print out "t" 8. veg[4:6] # will print out "to" 9. # this is because the last number is not included 10. # just like in for loops 11. 12. veg[:4] # will print the begining up to "t" 13. # output wil be "pota" 14. 15. veg.find("to") # output will be 4 16. # if there is no "to" it will not print out 17. 18. veg.upper() # output: "POTATOS" 19. veg = veg.upper() # output will be upper from now on 20. 21. veg # will print out what has been assigned For loops For loop statement reduces redundancy. The for variable in range (start, stop). The start lets the function know when to start, and stop will be when to stop. Good times to use this are when what you want to print uses a pattern. Set the variable equal to the start value Repeat the following: Check if the variable is less than the stop, if not, stop. Execute the statements Increase the variables value by 1. Print ("T-minus ", end=") For i in range (10, 0, -1): *Make sure to add the –1, otherwise the computer will continue counting down forever Print(str(I) + ", ", end=") Print() Print("Blastoff!") Print("The end.") Output: T-minus 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, Blastoff! The end. Nested loops, a loop inside of another loop. For i in range(1, 6): Five rows down For j in range(1, 11): 10 across Print("*", end"") Print() Output: ********** ********** ********** ********** ********** For i in range(1, 6): Five rows down. 1, 2, 3, 4, 5, 6 (last number, the stop number, will always be ignored) For j in range(1, i + 1): 1, and then (i + 1) across. Print("*", end"") Print() Output: * ** *** **** ***** Tables for Loops Using a table for your code is extremely helpful. Take the nested loop above as an example. The first loop is "i" and is how many rows you want the program to print. Follow the steps below to make a table: # of rows # of stars Difference in stars Line * 2 - 2 Line * 2 + size - 6 1 0 2 1 * 2 – 2 = 0 2 2 2 2 * 2 –2 = 2 3 4 2 3 * 2 – 2 = 4 4 6 2 4 * 2 – 2 = 6 5 8 2 5 * 2 – 2 = 8 Difference in stars is finding what is the difference between the lines. The difference in 1 and 2 is 2 stars. Same with 3 and 4 and 5 and etc. Line is line * (the difference in stars) - or + what will get you to the original output. In this case it is – 2. Size is replacing the – or + that got you the original output. If in this case size is 4, to return to – 2 you will have to subtract 6. Constants Are a fixed value visible to the whole program. This is the only exception to be outside of the main function. ALL_UPPERCASE_SYNTAX For example, size is a constant. Parameters A value passed to a function by its caller Instead of line_of_7, line_of_13, write line. Syntax: Def <name> (<parameter>): <statemens>(s) Def say_password(code): Print("The password is: " + code) <name>(<expression>) for expression, add what you want for code in the above line Example: it passes the expression into the parameter: say_password(42) say_password(12345) Output: The password is: 42 The password is: 12345 A parameter in loops; A parameter can guide the number of repetitions of a loop Chant(3) Def chant(times): For I in range(0, times): This starts at 0 and chant is 3. The range is 0, 3. This will output 3 lines as opposed to starting at 1. I.e. (1, 3) would only print out 2 lines. Print("Just a salad") Output: Just a salad Just a salad Just a salad Multiple parameters can be made as much as you want, as long as you separate them by commas. I.e. (height, width) Value semantics: when numbers and string are passed as parameters their values are copied Modifying the parameter will not affect the variable passed in X = 23 Def strange(x): X = x + 1 Print("1. X = " + x) X will now be 24 Mystery parameters Follow the color coding below. 22. def main(): 23. major = "fred" 24. fred = "computer" 25. computer = "department" 26. department = "student" 27. student = "major" 28. 29. sentence(major, fred, department) fred, computer, student 30. sentence(student, computer, fred) major, department, computer 31. sentence("fred", "honor", computer) fred, honor, department 32. sentence("foo", "bar", "baz") foo, bar, baz 33. 34. def sentence(major, fred, foo): 35. print("Many a " foo + " in the " + fred + " of " + major) 36. 37. ''' 38. output: 39. 40. Many a student in the computer of fred 41. Many a computer in the department of major 42. Many a department in the honor of fred 43. Many a baz in the bar of foo Math It is necessary to use from math import * to use the specific math functions listed in the slides. Simply calling a Python math function does not produce a visible result, to produce a visible output you must turn it into a variable: 44. from math import * 45. 46. def main(): 47. 48. result = floor(7.1) 49. print(result) 50. 51. main() The math functions min and max can be used to bound numbers. For example with an age, no one can be –3. For example, (0, -8) use the max function, as max will never be less than 0. max (0, -5) To create a cap, use the min function. I.e. min(90, 104) will always select the lowest number, which can never be above the range we set as 90. min (90, 104) If/else These statements work the same way that they would in English. For example, "If it is raining, then I will bring an umbrella" If it is raining = bring an umbrella Else = do not bring an umbrella Use exclusively if/if you require 0 more path/output. Use exclusively if/elif if you need 0 or 1 path/output. Use If/elif/else if you require exactly 1 path/output. The following will not be on the midterm: Graphics While loops Everything from lecture 13 and above Converting between decimal and binary numbers
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'