Week 2 Flashcards

1
Q

describe the way a list is indexed

A

the first item in a list in index 0 the second is index 1

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

what is the python operand for power

A

**

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

what is th eputhon operand to divide a number and reveal the reaminder

A

%

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

what is the python operand to divide a number and reveal the whole numbe rpart of the answer?

A

//

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

what are the two trypes of numbers

A

integers and floats

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

2 != 1+1 is?

A

False

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

‘aardvark’ > ‘zebra’

A

True

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

1 >= 2 or 9*8 == 72 is?

A

True

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

what is the purpose of the interactive shell

A

to try out code, nothing is saved when the shell is shut down.

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

How do you denote a comment, and what are they used for?

A
#
make code easy for other users to read - helps when I GO BACK TO IT
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

what is the use of len

A

len finds the length of a List

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

how do i find the lenght of a list

A

len

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

myList.append

A

adds a new item to the list at the end

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

how do i add an item to a list at the end

A

myList.append

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

how do i change the item at index 1?

A

myList[1]= ‘dog’

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

myList[1] = ‘dog’

A

change sthe item at index 1

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

remove the item at index 0 and assign it to a variable

A

result = myList.pop(0)

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

result = myList.pop(0)

A

remove the item at index 0 and assign it to a variable

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

remove the last item of a list is done by

A

myList.pop()

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

myList.pop()

A

remove the last item

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

what do we pass a function? and what does it then do

A

an argument, it then does something and passes back a result.

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

what is a method

A

You can think of a method as something a particular kind of object knows how to do. For instance a list knows how to append a new item to itself. To apply the append method to a given list we use the dot notation:

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

how do i join to lists together?

A

[1, 2, 3, 5] + [8, 13, 21]

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

HOW DO I SLICE

A

aList[2:4]

slice from index 2 up to but not including index 4

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

aList[2:4]

A

slicing from index 2 up to but not including index 4

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

slice from index 3 till end?

A

aList[3:]

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

what is a string

A

in Python a string is simply a sequence of characters.

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

give an example of len being used on a string

A

myString = ‘algorithm’
len(myString)
9

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

what does immutable mean

A

means that once they are created that they cannot be changed, strings are immutable.

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

what does aString.split(‘a’) do?

A

splits the string stored in aString on the character ‘a’.

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

how do i search a variable (which is a string) to find the index where ‘xxx’ occurs?

A
aString.find('xxx')
#returns an index (5)
32
Q

aString.count(‘x’)

A

counts how many times the string ‘x’ occurs in the string ‘aString’.

33
Q

what is SPLIT

A

The SPLIT method:

produces a list of strings using a character as a separator.

34
Q

what will be the result of
aString = ‘Python’
list(aString)

A

[‘P’,’y’,’t’.’h’.’o’.’n’]

35
Q

What is a tuple

A

A tuple is like a list except it is immutable

36
Q

Xxxx = (10, ‘ten’, ‘x’)

What is this? How would I find the length of it and slice it?

A

This is a tuple

len(myTuple)

myTuple[1:]

37
Q

What is a set

A

A set is like a list except that no item can appear twice - duplicates are not allowed. And the items are not displayed in any special order.

Note that this means slice or indexes do not apply

38
Q

Xxx = {‘red’, 1}

What is this? How would I add a new term?

A

This is a set

aSet.add(‘blue’)

39
Q

What would the result of

set(‘cocoa’)

Be?

A

{‘a’,’c’,’o’}

40
Q

What does the | operator do?

A

It takes two sets and produces a new set containing all the items from both sets.

bSet = {'m','i','c','e'}
cSet = {'m','e','n'}

bSet | cSet
{‘c’,’e’,’I’,’m’,’n’}

41
Q

What does the & operator do?

A

It produces a new set containing only items common to both sets.

42
Q

What are the alternative methods for | and & ?

A

bSet.union(cSet)

& is

bSet.intersection(cSet)

is

43
Q

How do I check to see if an item belongs to a set?

A

dSet = {4,6,7,8,9,5}

4 in dSet

44
Q

How do I round the answer to a sum if it will be a float?

A

We can use round, for example

X=2/3
Y=round(x,4)
Print (y)

Will display x to four decimal places

45
Q

What is the default approach by Python to separating and ending when using print ?

A

A space is inserted between each item and a new line character at the end.

Print(5,’plus’,3,’is’,8)

Will give 5 plus 3 is 8

Print(‘xxxxx’)
Print(‘xxxxx’)

Will give
Xxxxx
Xxxxx

46
Q

When using print in Python how do I change the separator and the end?

A

We can add optional arguements

Print(‘xx’,’xx’, sep=’’, end=’’)
Print(‘xx’,’xx’, sep=’’)
Gives
Xx
xxxxxx

47
Q

what two things make up the control structures

A

selection and iteration

48
Q

what is selection?

A

its when a part of the algorithm follows different paths depending on some condition.

49
Q

what is iteration

A

iteration also known as looping or repetition, repeats a group of steps some number of times depoending on a condition which is tested before each repetition.

50
Q

xxxxxx:
xxxxx

above are two seperate parts of python code, what are they?

A

The first xxxx is the header and the second is the suite.

51
Q

how to i quickly create a list of all the numbers between 0 and 4?

A

list(range(5))

52
Q

what will

for x in range(5):
print(‘Fine and Dandy’)

acheieve?

A

it will print out fine and dandy 5 times

53
Q

what will

for x in range(10, 101):
if x%2 == 0:
print(x)

do?

A

it will print the even numbers from 10 to 100

54
Q

How would i create a list from 10 to 20 going up in steps of 3?

A

list(range(10, 20, 3))

55
Q

using list comprehension how would i rewrite:-

sqlist =[]
for x in range )1,11):
sqlist.append(x*x)

?

A

sqlist=[x*x for x in range (1,11)]

56
Q

what will

list=[x*x for x in range (1,11) if x%2 != 0]

acheieve?

A

it will print out the square numberds of the odd numbers between 0 and 10

57
Q

what is a subprogram?

A

In python these are known as functions they are small prgrams that can complete a certain task an return a vaklue back to teh main program.

58
Q

in python how do you set out a user defined function?

A

begins with a header consisting the keyword def, followed by the name of the function, the arguments and a colon. following this is the body and finally a return statement.

59
Q

what will the following return?

def max2(x, y):
    z = x
    if x
A

it will return the value 12.

60
Q

define a function mean that takes two numbers as arguements and returns their mean.

A
def mean (x,y):
    z = x + y
    z = z/2
    return z
61
Q

define a function that takes a string as an argument and prints the string between two sets of three stars,

A
def printWithStars (x):
    print ('***'+x+'***')
62
Q

define a Function isEven that takes an argument which is a positive integer and returns True if the number is even, otherwise False

A
def isEven (x):
    if x
63
Q

how do I define a variable to be used across programs?

A

to do this we use the keyword global, si in the following:

q = 4
def test2():
    global q 
    q = q + 1 
test2()
print(q)

the final value to be printed would be 5

64
Q
How could you amend the following code so that the function test sets the value of the variable p to 3 throughout the rest of the program?
p = 2
def test():
    p = 3
    print(p)
test()
print(p)
A

change the p inside the test() function to:

global p

65
Q

when we are importing something into Python what is it that we are actually doing?

A

we are allowing functions that are stored in a some other module (library) to be used by our code.

66
Q

how do we import the module TIME? How would we find the current date and time.

A

to import the time module and then call the ctime function we:

import time
print(time.ctime())

67
Q

What exactly is OOP? and why is it useful in the study of algorithms?

A

Object-orientated programming is a way of thinking about a program as a community of interacting objects. It takes the idea of breaking a program up into a number of subprograms a stage further and also separates the data being processed into discrete chunks. Each object is responsible for just a small subset of data and the operations on that data. Collaboration between the objects gets the work of the program done.

68
Q

in OOP how are objects organised.

A

they are organised into classes: all the objects in a clasas have the same structure and operations but they have their own data. The objects that belong to a class are called its instances.

69
Q

What is often the key to finding a computational solution.

A

choosing a good representation of a problem.

70
Q

How would i set up a data type called MONEY

A
class Money:
    def \_\_init\_\_(self, anAmount, aCurrency):
        self.amount = anAmount
        self.currency = aCurrency
71
Q

When setting up a new class ‘xxx’ and defining what data ‘xxx’ objects will hold, what are we creating and how does it look?

A

a constructor.- in the code it will appear as __init__ this is always the same and never changes.

72
Q
in
 class Money:
    def \_\_init\_\_(self, anAmount, aCurrency):
        self.amount = anAmount
        self.currency = aCurrency
what do the three arguments stand for?
A

the three arguemtns are (self, anAmount, aCurrency)

self = refers to the object under construction.  so the constructor is being passed the new object itself, followed by the data the object has to store.
anAmount = amount
aCurrency = currency
73
Q

what does the line

self.amount = anAmount

do when we are creating new data types.

A

the left hand side defines a variable called amount that will belong to the object under construction. The right-hand part then sets amount to store the value passed via the argument anAmount.

74
Q

how would we check the values of an object?

A

we use dot notation, for example:

print(money1.amount)
print(money1.currency)

75
Q

When thinking about classes what are methods?

A

The operations that belong to a class are called methods.

76
Q

When building new classes from existing ones in OO languages, what are the existing class and the new class called?

A

the existing class is the parent and the new class is the child.