FE Review Chapter 4-14 PDF

Summary

This document is a summary review of chapters 4 through 14 of a computer science textbook. It covers topics such as Python's mathematical functions, strings, objects, loops, and functions. The document focuses on practical applications and explanations within the context of programming.

Full Transcript

**Summary Review of Chapter 4 to Chapter 14** **Summary Chapter 4: Mathematical Functions, Strings, and Objects** 1. Python provides the mathematical functions abs, max, min, pow, and round in the interpreter and the functions fabs, ceil, floor, exp, log, sqrt, sin, asin, cos, acos, tan,...

**Summary Review of Chapter 4 to Chapter 14** **Summary Chapter 4: Mathematical Functions, Strings, and Objects** 1. Python provides the mathematical functions abs, max, min, pow, and round in the interpreter and the functions fabs, ceil, floor, exp, log, sqrt, sin, asin, cos, acos, tan, degrees, and radians in the math module. 2. A *string* is a sequence of characters. String values can be enclosed in matching single quotes (\') or double quotes (\"). Python does not have a data type for characters; a single-character string represents a character. 3. An *escape sequence* is a special syntax that begin with the character \\ followed by a letter or a combination of digits to represent special characters, such as \'\\\'\', \'\\\"\', \'\\t\', and \'\\n\'. 4. The characters \' \', \'\\t\', \'\\f\', \'\\r\', and \'\\n\' are known as the *whitespace characters*. 5. All data including numbers and strings are *objects* in Python. You can invoke *methods* to perform operations on the objects. 6. You can use the format function to format a number or a string and return the result as a string. **Summary Chapter 5: Loops** 1. There are two types of repetition statements: the while loop and the for loop. 2. The part of the *loop* that contains the statements to be repeated is called the *loop body*. 3. A one-time execution of a loop body is referred to as an *iteration of the loop*. 4. An *infinite loop* is a loop statement that executes infinitely. 5. In designing loops, you need to consider both the loop-control structure and the loop body. 6. The while loop checks the loop-continuation-condition first. If the condition is true, the loop body is executed; otherwise, the loop terminates. 7. A *sentinel value* is a special value that signifies the end of the input. 8. The for loop is a *count-controlled loop* and is used to execute a loop body a predictable number of times. 9. Two keywords, break and continue, can be used in a loop. 10. The break keyword immediately ends the innermost loop, which contains the break. 11. The continue keyword ends only the current iteration. **Summary Chapter 6: Functions** 1. Making programs modular and reusable is one of the central goals in software engineering. Functions can help to achieve this goal. 2. A function header begins with the def keyword followed by function's name and parameters, and ends with a colon. 3. Parameters are optional; that is, a function does not have to contain any parameters. 4. A function is called a void function if it does not return a value. 5. A return statement can also be used in a void function for terminating the function and returning to the function's caller. This is useful occasionally for circumventing the normal flow of control in a function. 6. The arguments that are passed to a function should have the same number, type, and order as the parameters in the function header. 7. When a program calls a function, program control is transferred to the called function. A called function returns control to the caller when its return statement is executed or when the last statement in the function is executed. 8. A value-returning function can also be invoked as a statement in Python. In this case, the function's return value is ignored. 9. A function's arguments can be passed as positional arguments or keyword arguments. 10. When you invoke a function with a parameter, the reference of the argument is passed to the parameter. 11. A variable created in a function is called a local variable. The scope of a local variable starts from its creation and exists until the function returns. A variable must be created before it is used. 12. Global variables are created outside **Summary Chapter 7: List** 1. You can use the Python built-in functions len, max, min, and sum to return the length of a list, the maximum and minimum elements in a list, and the sum of all the elements in a list., 2. You can use the shuffle function in the random module to shuffle the elements in a list. 3. You can use the index operator \[\] to reference an individual element in a list. 4. Programmers often mistakenly reference the first element in a list with index 1, but it should be 0. This is called the *index off-by-one error*. 5. You can use the concatenation operator + to concatenate two lists, the repetition operator \* to duplicate elements, the *slicing operator* \[:\] to get a sublist, and the in and not in operators to check whether an element is in a list. 6. You can use a for loop to traverse all elements in a list. 7. You can use the comparison operators to compare the elements of two lists. 8. A list object is mutable. You can use the methods append, extend, insert, pop, and remove to add and remove elements to and from a list. 9. You can use the index method to get the *index* of an element in a list and the count method to return the count of the element in the list. 10. You can use the sort and reverse methods to sort or reverse the elements in a list. 11. You can use the split method to split a string into a list. 12. When a function is invoked with a list argument, the reference of the list is passed to the function. 13. If a list is sorted, binary search is more efficient than linear search for finding an element in the list. 14. Selection sort finds the smallest element in the list and swaps it with the first element. It then finds the smallest element remaining and swaps it with the first element in the remaining list, and so on, until only a single element remains. **Summary Chapter 9: Objects and Classes** 1. A *class* is a template, a blueprint, a contract, and a data type for objects. It defines the properties of objects and provides an *initializer* for initializing objects and methods for manipulating them. 2. The initializer is always named \_\_init\_\_. The first parameter in each method including the initializer in the class refers to the object that calls the method. By convention, this parameter is named self. 3. An object is an *instance* of a class. You use the *constructor* to create an object, and the *dot operator* (.) to access members of that object through the variable that references the object. 4. An instance variable or method belongs to an instance of a class. Its use is associated with individual instances. 5. *Data fields* in classes should be hidden to prevent data tampering and to make classes easy to maintain. 6. You can provide a getter method or a setter method to enable clients to see or modify the data. Colloquially, a getter method is referred to as a *getter* (or *accessor*), and a setter method as a *setter* (or *mutator*). **Summary Chapter 12: Inheritance and Polymorphism** 1. You can derive a new class from an existing class. This is known as *class inheritance*. The new class is called a *subclass*, *child class*, or *extended class*. The existing class is called a *superclass*, *parent class*, or *base class*. 2. To *override* a method, the method must be defined in the subclass using the same header as in its superclass. 3. The object class is the root class for all Python classes. The methods \_\_str\_\_() and \_\_eq\_\_(other) are defined in the object class. 4. *Polymorphism* means that a method can work with different objects as long as the method can be invoked from the object. A method may be implemented in several classes along the inheritance chain. Python decides which method is invoked. This is known as *dynamic binding*. 5. The isinstance function can be used to determine whether an object is an instance of a class. 6. The common relationships among classes are *association*, *aggregation*, *composition*, and *inheritance*. **Summary Chapter 13: Files and Exception Handling** 1. You can use file objects to read/write data from/to files. You can open a file to create a file object with mode r for reading, w for writing, and a for appending. 2. You can use the os.path.isfile(f) function to check if a file exists. 3. Python has a file class that contains the methods for reading and writing data, and for closing a file. 4. You can use the read(), readline(), and readlines() methods to read data from a file. 5. You can use the write(s) method to write a string to a file. 6. You should close the file after the file is processed to ensure that the data is saved properly. 7. You can read a Web resource just like reading data from a file. 8. You can use exception handling to catch and handle runtime errors. You place the code that may raise an exception in the try clause, list the exceptions in the except clauses, and process the exception in the except clause. 9. Python provides built-in exception classes such as ZeroDivisionError, SyntaxError, and RuntimeError. All Python exception classes inherit directly or indirectly from BaseException. You can also define your own exception class derived from BaseException or from a subclass of BaseException, such as RuntimeError. 10. You can use the Python pickle module to store objects in a file. The dump function writes an object to the file and the load function reads an object from the file. **Summary Chapter 14: Tuples, Sets, and Dictionaries** 1. A *tuple* is a fixed list. You cannot add, delete, or replace elements in a tuple. 2. Since a tuple is a sequence, the common operations for sequences can be used for tuples. 3. Though you cannot add, delete, or replace elements in a tuple, you can change the content of individual elements if the elements are mutable. 4. A tuple is immutable if all its elements are immutable. 5. *Sets* are like lists in which you use them for storing a collection of elements. Unlike lists, however, the elements in a set are nonduplicates and are not placed in any particular order. 6. You can add an element to a set using the add method and remove an element from the set using the remove method. 7. The len, min, max, and sum functions can be applied to a set. 8. You can use a for loop to traverse the elements in a set. 9. You can use the issubset or issuperset method to test whether a set is a subset or a superset of another set and use the \|, &, -, and \^ operators to perform set union, intersection, difference, and symmetric difference. 10. Set is more efficient than list for testing whether an element is in a set and a list or for removing elements from a set and a list. 11. A *dictionary* can be used to store key/value pairs. You can retrieve a value using a *key*. The keys are like an index operator. In a list, the indexes are integers. In a dictionary, the keys can be any hashable objects such as numbers and strings. 12. You can use dictionaryName\[key\] to retrieve a value in the dictionary for the given key and use dictionaryName\[key\] = value to add or modify an item in a dictionary. 13. You can use del dictionaryName\[key\] to delete an item for the given key. 14. You can use a for loop to traverse all keys in a dictionary. 15. You can use the len function to return the number of items in a dictionary. 16. You can use the in and not in operators to test if a key is in a dictionary and use the == and != operator to test if two dictionaries are the same. 17. You can use the methods keys(), values(), items(), clear(), get(key), pop(key), and popitem() on a dictionary.

Use Quizgecko on...
Browser
Browser