Coursera Python Flashcards
How to use print for debugging?
In each function print all inputs and outputs, do not forget to label them
How to simplify if a == True?
if a:
How to simplify if a == False?
if not a:
How to simplify if not a and b: return True else: return False
return not a and b
How to simplify
if a == True and b == True:
return False
return not (a and b)
What is PEP 8?
Coding style
According to PEP 8, what is the best way to create indents?
4 spaces, no tabs
According to PEP 8, what is the best way to wrap lines?
Lines should not exceed 79 characters
According to PEP 8, what is the best way to make functions and classes to be readible in the code?
Use blank lines to separate them
According to PEP 8, what is the best way to put comments?
Put comments on the line of their own
According to PEP 8, what is the best way to make functions easier to up understand for a reader?
Use docstrings
According to PEP 8, where is the best way to use spaces?
Use spaces around operators and after commas but not directly inside the bracketed constructs a = f(1, 2)
According to PEP 8, what is the convention to name classes and functions?
CamelCase for classes and lower_case_with_underscores for functions and methods
How to add an input field to a simplegui frame?
inp = frame.add_input("Label", input-handler, width) The handler should be defined as follows, where text_input will be a string: def input_handler(text_input):
How to add (register) buttons to a simplegui frame?
frame.add_button(“Label”, handler, width of a button)
What is a structure of an interactive program?
1 Import the module
2 Define global variables (program state)
3 Define “helper” functions
4 Define classes
5 Define event handler functions
6 Create a frame
7 Register event handlers
8 Start frame and timers
How to provide a documentation string for a function?
Using triple quotes (“””) before and after the documentation text, immediately after the head of the function
What the documentation of the function should provide?
What the function does, not how it does it. For the “how”, use a comment inside the function.
What is a quotient?
The number resulting from a division of one number to another
What is modular arithmetic for the purpose of Python programming?
Systematically restrict computation to a range. Long division - divide by a number, we get quotient and a remainder. Quotient is integer division, and the remainder is %
Which function will print out tens and ones of a number?
def num(a): ones = a%10, tens = a//10 print ones, tens
num(46)
6 4
Which kind of brackets lists in Python use?
Square
What is a variable?
A placeholder for important values
Why are variables helpful?
- They can be used to avoid recomputing values
2. They can have names that help reader understand code
What are valid variable names?
- Consist of letters, numbers, underscore
- Start with letter or underscore (not number!)
- Several word can be used, only if connected by an underscore
- Case sensitive
- Are not Python special words
- Helpful - can remind what this variable is all about
What is +=?
Takes a value on the left side, adds a value from the right side and applies the result to the left side:
a = a + 1
is the same as:
a += 1
In division, if one operand is a float, what type is the answer?
Float