Coursera Python Flashcards

1
Q

How to use print for debugging?

A

In each function print all inputs and outputs, do not forget to label them

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How to simplify if a == True?

A

if a:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to simplify if a == False?

A

if not a:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
How to simplify 
if not a and b:
    return True
else:
    return False
A

return not a and b

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to simplify
if a == True and b == True:
return False

A

return not (a and b)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is PEP 8?

A

Coding style

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

According to PEP 8, what is the best way to create indents?

A

4 spaces, no tabs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

According to PEP 8, what is the best way to wrap lines?

A

Lines should not exceed 79 characters

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

According to PEP 8, what is the best way to make functions and classes to be readible in the code?

A

Use blank lines to separate them

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

According to PEP 8, what is the best way to put comments?

A

Put comments on the line of their own

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

According to PEP 8, what is the best way to make functions easier to up understand for a reader?

A

Use docstrings

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

According to PEP 8, where is the best way to use spaces?

A

Use spaces around operators and after commas but not directly inside the bracketed constructs a = f(1, 2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

According to PEP 8, what is the convention to name classes and functions?

A

CamelCase for classes and lower_case_with_underscores for functions and methods

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How to add an input field to a simplegui frame?

A
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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How to add (register) buttons to a simplegui frame?

A

frame.add_button(“Label”, handler, width of a button)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is a structure of an interactive program?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How to provide a documentation string for a function?

A

Using triple quotes (“””) before and after the documentation text, immediately after the head of the function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What the documentation of the function should provide?

A

What the function does, not how it does it. For the “how”, use a comment inside the function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is a quotient?

A

The number resulting from a division of one number to another

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What is modular arithmetic for the purpose of Python programming?

A

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 %

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Which function will print out tens and ones of a number?

A
def num(a):
     ones = a%10,
     tens = a//10
     print ones, tens

num(46)
6 4

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Which kind of brackets lists in Python use?

A

Square

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What is a variable?

A

A placeholder for important values

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Why are variables helpful?

A
  1. They can be used to avoid recomputing values

2. They can have names that help reader understand code

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

What are valid variable names?

A
  1. Consist of letters, numbers, underscore
  2. Start with letter or underscore (not number!)
  3. Several word can be used, only if connected by an underscore
  4. Case sensitive
  5. Are not Python special words
  6. Helpful - can remind what this variable is all about
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

What is +=?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

In division, if one operand is a float, what type is the answer?

A

Float

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

In division, if both operand are ints, what type is an answer?

A

The answer is an int, rounded down. If the number is negative, it is still rounded down. For example -9/4 =-3, because -2.25 is rounded down

29
Q

What is the output of -9/4 ?

A

-3

30
Q

Please excuse my dear aunt sallie

A

( ), **, *, / , +,-

31
Q

What is //?

A

An explicitly integer division

32
Q

Are floating numbers and numbers with decimal points the same?

A

No, floats are an approximation of decimal, only about 15 digits of accuracy, the rest are thrown away. His is so called floating point error.

33
Q

What is a computer monitor?

A

2D grid of pixels stored in frame buffer

34
Q

What is a refresh rate for a monitor?

A

Computers update the monitor based on the frame buffer at rate around 60-72 times a second - refresh rate

35
Q

What is a “draw handler?”

A

A special function that many applications register that change the content of a frame buffer

36
Q

How to draw on a canvas using a simplegui?

A

import simplegui

#define draw handler
def draw(canvas):
    canvas.draw_text("Hello", [100,100], 24, "white")
#create a frame
frame = simplegui.create_frame("test",300,200)
# register draw handler
frame.set_draw_handler(draw)
# start frame
frame.start()
37
Q

How to determine type of a variable?

A

print type(variable1), type(variable2)

38
Q

how to create a timer in simplegui?

A

timer=simplegui.create_timer(interval, timer_handler)

timer.start()

where interval is in milliseconds

39
Q

What is a list in Python?

A

A sequence of elements or a collection of objects. Allows us to keep data that belongs together together. Can be empty, can contain numbers, strings, or other sequences (for example, other lists)

40
Q

What are chr() and ord() functions in Python?

A
print chr(I) 
Prints a string of one character whose ASCII code is i (range from [0:250] inclusive
print ord('a') will print a code for the character
41
Q

How to use a keyboard control in simplegui?

A
def keydown(key):
If key == simplegui.key_map["left"]:
   pos[0] = 4 etc.

frame.set_keyboard_handler(keydown)

42
Q

How to determine the length of a sequence?

A

len(my_list)

43
Q

How to access the first and the last elements of the list?

A

print my_list[0]

print my_list[-1]

44
Q

Among strings and lists, which are mutable?

A

Lists. You can assign a new element to the list and it will change the list

45
Q

How to calculate distance using Pythagorean formula?

A

import math

def distance(p, q):
       Return math.sqrt( (p[0] - q[0]) ** 2  + (p[1] - q[1]) ** 2 )
46
Q

How to update position for a motion, using a vector

A

pic

47
Q

How to add a mouse input in simplegui?

A

Register event handler:

frame. set_mouseclick_handler(click)
frame. set_draw-handler(draw)

Define 2 handlers, draw and click. Click only takes one parameter - position, which is a tuple of 2 numbers

48
Q

How to print the last member of a list?

A

print list[-1]

49
Q

How to use “in” in lists?

A

in checks if the item is in the list

82 in list

Will evaluate as True or False, depending if 82 is actually in the list

50
Q

How to use “index” with list?

A

Index check where the item is in the list:

list.index(8) => returns location of the item in the list

51
Q

How to use “append” and “pop”with the lists?

A

Allows to change the list by adding an element

list. append(632) => adds 632 to the end of the list
list. pop() => removes the last element from the list, returns the element that has been popped
list. pop(4) => pops element #4 and returns it

52
Q

How to use “remove” with a list?

A

list.remove(element)

Removes the first element from the list that has value “element”. Returns error if element is not in the list

53
Q

What is a string?

A

A line of text in single or double quotes (do not mix them up!)

54
Q

What kinds of operations you can do with strings?

A

Concatenante and multiply

When you multiply - it repeats the string that many times

55
Q

What are Boolean operators?

A

True or False

Have to be with capital first letters

56
Q

How to express ‘equal to” and “not equal to”?

A

== and !=

57
Q

Can you pop items from the list while iterating through this list with the “for” loop ?

A

No. You can remove items from the list outside of the for loop, by creating a new list which contains items to be removed.

58
Q

What are dictionaries in Python?

A

Mapped lists.

They use curly brackets and loo like {a:b, c:d}, where a and c are keys, and b and d are values

59
Q

How to access a value in a dictionary?

A

dict = {a:b, c:d}

dict(a) = b

60
Q

What are the differences between lists and dictionaries?

A

In the lists, indices are fixed, they are always numeric, always consecutive, and values are in a certain order

In dictionaries - no such constraints. Keys can be either numbers or strings

61
Q

How to load image into simplegui?

A

im = simplegui.load_image(URL)

62
Q

What does a Class do in Python

A

Classes give us ability to create more complicated. Data structures that contain arbitrary content. We can create Pet class that keeps track of the name and species in usefully named attributes called name and species, respectively.

63
Q

What is the difference between a class and an instance?

A
Class just contain structure, it defines how something should be laid out or structured, but does not fill the content. For example, a Pet class needs to have a name and species, but does not say what they are. Other example - tax form.
An instance - a specific copy of the class that does contain all the content. If we create a pet polly, with name  "Polly" and species "Parrot", then Polly is an instance of a Pet. A tax return is an instance of the tax class.
64
Q

First step in creating a class?

A

Class Pet(object):

The first word, class, is a keyword. The second word, Pet, indicates the name of the class. By convention it is capitalized. The word in parentheses, object, is the class that Pet is inheriting from. This object is a special variable in Python that you should include in the parentheses when you are creating a new class. Do not forget colon.

65
Q

How to set up an initializer for a class?

A

def __init__(self, name, species):

     self. name  =  name
     self. species = species
     self. spots = [ ]

The __init__ method (method is just a special term for functions that are part of a class) is a special Python function that is called when an instance of a class is first created. For example, when running the code polly = Pet(“Polly”, “Parrot”), the __init__ method is called with values polly, “Polly”, and “Parrot” for the variables self, name, and species, respectively. Note empty lists do not have to be passed as arguments in the parentheses.

The self variable is the instance of the class (a reference to a new object). Instances have the structure of the class but that the values within an instance may vary from instance to instance. self.name = name instead of Pet.name = name. A self.name - is a field name inside the object self.

The __init__ method (as well as other methods in the Pet class) have this self variable, but that when you call the method (e.g. polly = Pet(“Polly”, “Parrot”)), you only have to pass in two values. This phenomena is a special behavior of Python: when you call a method of an instance, Python automatically figures out what self should be (from the instance) and passes it to the function. In the case of __init__, Python first creates self and then passes it in.

66
Q

How to define methods within a class?

A
def getName(self):
     return self.name
def getSpecies(self):
     return self.species

Define methods to get the contents of the instance or the behaviors. The getName method takes an instance of a Pet as a parameter and looks up the pet’s name. Similarly, the getSpecies method takes an instance of a Pet as a parameter and looks up the pet’s species. The self parameter is required for the function to know which instance of Pet to operate on: it needs to be able to find out the content.
We don’t actually have to pass in the self parameter because Python automatically figures it out. There are two different ways of calling getName. 1) The standard way of doing it: polly.getName(). 2) Pet.getName(polly) while not conventional, is equivalent.

67
Q

How to create a __str__ method for a class?

A
def \_\_str\_\_ (self):
    return "%s is %s", % (self.name, self.species)
(pronounced like Stir)
This \_\_str\_\_ method is a special function that is defined for all classes in Python  \_\_str\_\_ takes one argument - self, and it is not called directly (just like init). Builds up strings for printing.
68
Q

What are “behind your back methods” in Python?

A

The ones that are surrounded by underbar-underbar ( like __init__ or __str__