















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Python Programming: Key Concepts Q&A for Students.
Typology: Cheat Sheet
1 / 23
This page cannot be seen from the preview
Don't miss anything!
















What are Python module? - ANSWERSimply, is a file consisting of Python code. it can define functions, classes and variables and can also include runnable code. What are some of the conceptual Patterns to construct a program? - ANSWERInput: Get data from the "outside world". This might be reading data from a file, or even some kind of sensor like a microphone or GPS. In our initial programs, our input will come from the user typing data on the keyboard. Output: Display the results of the program on a screen or store them in a file or perhaps write them to a device like a speaker to play music or speak text. Sequential execution: Perform statements one after another in the order they are encountered in the script. Conditional execution Check for certain conditions and then execute or skip a sequence of statements. Repeated execution Perform some set of statements repeatedly, usually with some variation. Reuse Write a set of instructions once and give them a name and then reuse those instructions as needed throughout your program.
What 3 general errors will you encounter in Python? - ANSWERSyntax errors: These are the first errors you will make and the easiest to fix. A syntax error means that you have violated the "grammar" rules of Python. does its best to point right at the line and character where it noticed it was confusing. The only tricky bit of syntax errors is that sometimes the mistake that needs fixing is actually earlier in the program than where Python noticed it was confusing. So the line and character that Python indicates in a syntax error may just be a starting point for your investigation. Logic errors A logic error is when your program has good syntax but there is a mistake in the order of the statements or perhaps a mistake in how the statements relate to one another. A good example of a logic error might be, "take a drink from your water bottle, put it in your backpack, walk to the library, and then put the top back on the bottle." Semantic errors A semantic error is when your description of the steps to take is syntactically perfect and in the right order, but there is simply a mistake in the program. The program is perfectly correct but it does not do what you intended for it to do. A simple example would be if you were giving a person directions to a restaurant and said, "... when you reach the intersection with the gas station, turn left and go one mile and the restaurant is a red building on your left." Your friend is very late and calls you to tell you that they are on a farm and walking around behind a barn, with no sign of a restaurant. Then you say "did you turn left or right at the gas station?" and they say, "I followed your directions perfectly, I have them written down, it says turn left and go one mile at the gas station." Then you say, "I am very sorry, because while my instructions were syntactically correct, they sadly contained a small but undetected semantic What is Debugging? - ANSWERis the process of finding the cause of the error in your code.
High-level language - ANSWERA programming language like Python that is designed to be easy for humans to read and write. Interactive mode - ANSWERA way of using the Python interpreter by typing commands and expressions at the prompt. Interpret - ANSWERTo execute a program in a high-level language by translating it one line at a time. Low-level language - ANSWERA programming language that is designed to be easy for a computer to execute; also called "machine code" or "assembly language". Machine code - ANSWERThe lowest-level language for software, which is the language that is directly executed by the central processing unit (CPU). Main memory - ANSWERit loses its information when the power is turned off. Parse - ANSWERTo examine a program and analyze the syntactic structure. Portability - ANSWERA property of a program that can run on more than one kind of computer. Print function - ANSWERAn instruction that causes the Python interpreter to display a value on the screen.
problem solving - ANSWERThe process of formulating a problem, finding a solution, and expressing the solution. Program - ANSWERA set of instructions that specifies a computation. Prompt - ANSWERWhen a program displays a message and pauses for the user to type some input to the program. Secondary memory - ANSWERStores programs and data and retains its information even when the power is turned off. Generally slower than main memory. Examples of secondary memory include disk drives and flash memory in USB sticks. Semantics - ANSWERThe meaning of a program. semantic error - ANSWERAn error in a program that makes it do something other than what the programmer intended. source code - ANSWERA program in a high-level language. what Python reserve Keywords? - ANSWER 33 Variable names - ANSWERcan be arbitrarily long. They can contain both letters and numbers, but they cannot start with a number. It is legal to use uppercase letters, but it is a good idea to begin variable names with a lowercase letter
The other Boolean comparison are - ANSWER== equal != not equal > greater < less >= greater or equal <= less or equal "is" - is the same as "is not" - is not the same as what is a class bool - ANSWERTrue and False are special values that belong to the class bool they are not strings: There are three logical operators - ANSWERand, or, and not. Example: For example, x > 0 and x < 10 the not operator - ANSWERnegates a boolean expression, so not (1 > 3) is true Conditional statements - ANSWERGive the ability to check conditions and change the behavior of the program accordingly. The simplest form is the if statement: example: if x > 0 : print(' x is positive') Description of the if statement: - ANSWERThe boolean expression after the if statement is called the condition. We end the if statement with a colon character (:) and the line( s) after the if statement are indented.
If Logic - ANSWERIf the logical condition is true, then the indented statement gets executed. If the logical condition is false, the indented statement is skipped. if x% 2 = = 0 : print(' x is even') else : print(' x is odd') If-Then-Else Logic - ANSWERSince the condition must either be true or false, exactly one of the alternatives will be executed. The alternatives are called branches, because they are branches in the flow of execution. If-Then-ElseIf Logic - ANSWERThere is no limit on the number of elif statements. If there is an else clause, it has to be at the end, but there doesn't have to be one. if choice = = 'a': print(' Bad guess') elif choice = = 'b': print(' Good guess') elif choice = = 'c': print(' Close, but not correct') Nested If Statements - ANSWERAlthough the indentation of the statements makes the structure apparent, nested conditionals become difficult to read very quickly. In general, it is a good idea to avoid them when you can. if x = = y:
argument - ANSWERA value provided to a function when the function is called. This value is assigned to the corresponding parameter in the function. body - ANSWERThe sequence of statements inside a function definition. composition - ANSWERUsing an expression as part of a larger expression, or a statement as part of a larger statement. deterministic - ANSWERPertaining to a program that does the same thing each time it runs, given the same inputs. dot notation - ANSWERThe syntax for calling a function in another module by specifying the module name followed by a dot (period) and the function name. flow of execution - ANSWERThe order in which statements are executed during a program run. fruitful function - ANSWERA function that returns a value. function - ANSWERA named sequence of statements that performs some useful operation. Functions may or may not take arguments and may or may not produce a result. function call - ANSWERA statement that executes a function. It consists of the function name followed by an argument list.
function definition - ANSWERA statement that creates a new function, specifying its name, parameters, and the statements it executes. function object - ANSWERThe name of the function is a variable that refers to a function object. header The first line of a function definition. import statement - ANSWERA statement that reads a module file and creates a module object. module object - ANSWERA value created by an import statement that provides access to the data and code defined in a module. parameter - ANSWERA name used inside a function to refer to the value passed as an argument. pseudorandom - ANSWERPertaining to a sequence of numbers that appear to be random, but are generated by a deterministic program. return value - ANSWERis the result of a function that is returned by the function when called void function - ANSWERA function that does not return a value. accumulator - ANSWERA variable used in a loop to add up or accumulate a result.
What is a string - ANSWERis a sequence of characters. What is the expression in the brackets: fruit[ 1] - ANSWERit is called an Index What is call the pattern of processing the start of a string at the beginning, select each character in turn, do something to it, and continue until the end - ANSWERit is called a traversal. How do you access the last index character of a string? - ANSWERyou access by typing: len(fruit) - 1 or fruit[-1] What is an item - ANSWERis one of the values in a sequence. what does immutable means? - ANSWERit means you can't change an existing string. The best you can do is create a new string that is a variation on the original. What does the "in" operator does? - ANSWERtakes two strings and returns True if the first appears as a substring in the second: example: "a" in "Banana" returns True. what does the Python function "dir" does? - ANSWERit lists the methods available for an object. The "type" function shows the type of an object and the "dir" function shows the available methods. what does the Python function "help" does? - ANSWERit provides information about the function and its methods
How do you call a method of a function? - ANSWERit is similar to calling a function (it takes arguments and returns a value) but the syntax is different. example of the "upper method": word.upper(). The empty parentheses indicate that it takes no arguments. How is a method call refer? - ANSWERIt is called an invocation; for example: word.upper() so in this case, we would say that we are invoking upper on the word. what is a tuple? A tuple is a sequence of comma-separated values inside a pair of parenthesis. - ANSWER What does the format "%" operator allow us to do? - ANSWERthe format operator "%" allows us to construct strings, replacing parts of the strings with the data stored in variables. When applied to integers, "%" is the modulus operator. But when the first operand is a string, "%" is the format operator. example: camels = 42 type: 'I have spotted %d camels.' % camels result: 'I have spotted 42 camels.' "%d" format an integer, "%g" to format a floating- point number and "%s" to format a string. help files: https:// docs.python.org/ library/ stdtypes.html# printf-style-string-formatting. what is the guardian pattern? - ANSWERWhere we construct a logical expression with additional comparisons to take advantage of the short-circuit behavior. empty string - ANSWERA string with no characters and length 0, represented by two quotation marks.
search - ANSWERA pattern of traversal that stops when it finds what it is looking for. sequence - ANSWERAn ordered set; that is, a set of values where each value is identified by an integer index. slice - ANSWERA part of a string specified by a range of indices. traverse - ANSWERTo iterate through the items in a sequence, performing a similar operation on each. catch - ANSWERTo prevent an exception from terminating a program using the try and except statements. newline - ANSWERA special character used in files and strings to indicate the end of a line. Pythonic - ANSWERA technique that works elegantly in Python. "Using try and except is the Pythonic way to recover from missing files". Quality Assurance - ANSWERA person or team focused on ensuring the overall quality of a software product. QA is often involved in testing a product and identifying problems before the product is released. text file - ANSWERA sequence of characters stored in permanent storage like a hard drive.
what is a list - ANSWERis a sequence of values. like a string but unlike a string they are mutable. What are the values in list called? - ANSWERthey are call elements or sometimes items. nested list are - ANSWERwhen there are list within another list. what are equivalent objects? - ANSWERit is when they have the same elements, but not identical because they are not the same object. what is a reference? - ANSWERit is the association of a variable with an object. example: a = [1,2,3] -- b=a therefore b is a. in this case there are two references to the same object. What is call an object with more than one reference that has more than one name - ANSWERthat object is called an Aliased Severance, Charles. Python for Everybody: Exploring Data in Python 3 (Kindle Location 1949). Kindle Edition. - ANSWER Severance, Charles. Python for Everybody: Exploring Data in Python 3 (Kindle Location 1948). Kindle Edition. - ANSWER aliasing - ANSWERA circumstance where two or more variables refer to the same object.
hash function - ANSWERA function used by a hashtable to compute the location for a key. histogram - ANSWERA set of counters. implementation - ANSWERA way of performing a computation. item - ANSWERAnother name for a key-value pair. key - ANSWERAn object that appears in a dictionary as the first part of a key- value pair. key-value pair - ANSWERThe representation of the mapping from a key to a value. lookup - ANSWERA dictionary operation that takes a key and finds the corresponding value. nested loops - ANSWERWhen there are one or more loops "inside" of another loop. The inner loop runs to completion each time the outer loop runs once. value - ANSWERAn object that appears in a dictionary as the second part of a key-value pair. This is more specific than our previous use of the word "value".
comparable - ANSWERA type where one value can be checked to see if it is greater than, less than, or equal to another value of the same type. Types which are comparable can be put in a list and sorted. data structure - ANSWERA collection of related values, often organized in lists, dictionaries, tuples, etc. DSU - ANSWERAbbreviation of "decorate-sort-undecorate", a pattern that involves building a list of tuples, sorting, and extracting part of the result. gather The operation of assembling a variable-length argument tuple. hashable - ANSWERA type that has a hash function. Immutable types like integers, floats, and strings are hashable; mutable types like lists and dictionaries are not. scatter - ANSWERThe operation of treating a sequence as a list of arguments. shape (of a data structure) - ANSWERA summary of the type, size, and composition of a data structure. singleton A list (or other sequence) with a single element. tuple - ANSWERAn immutable sequence of elements. tuple assignment - ANSWERAn assignment with a sequence on the right side and a tuple of variables on the left. The right side is evaluated and then its elements are assigned to the variables on the left.