Week 2 Flashcards
describe the way a list is indexed
the first item in a list in index 0 the second is index 1
what is the python operand for power
**
what is th eputhon operand to divide a number and reveal the reaminder
%
what is the python operand to divide a number and reveal the whole numbe rpart of the answer?
//
what are the two trypes of numbers
integers and floats
2 != 1+1 is?
False
‘aardvark’ > ‘zebra’
True
1 >= 2 or 9*8 == 72 is?
True
what is the purpose of the interactive shell
to try out code, nothing is saved when the shell is shut down.
How do you denote a comment, and what are they used for?
# make code easy for other users to read - helps when I GO BACK TO IT
what is the use of len
len finds the length of a List
how do i find the lenght of a list
len
myList.append
adds a new item to the list at the end
how do i add an item to a list at the end
myList.append
how do i change the item at index 1?
myList[1]= ‘dog’
myList[1] = ‘dog’
change sthe item at index 1
remove the item at index 0 and assign it to a variable
result = myList.pop(0)
result = myList.pop(0)
remove the item at index 0 and assign it to a variable
remove the last item of a list is done by
myList.pop()
myList.pop()
remove the last item
what do we pass a function? and what does it then do
an argument, it then does something and passes back a result.
what is a method
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 do i join to lists together?
[1, 2, 3, 5] + [8, 13, 21]
HOW DO I SLICE
aList[2:4]
slice from index 2 up to but not including index 4
aList[2:4]
slicing from index 2 up to but not including index 4
slice from index 3 till end?
aList[3:]
what is a string
in Python a string is simply a sequence of characters.
give an example of len being used on a string
myString = ‘algorithm’
len(myString)
9
what does immutable mean
means that once they are created that they cannot be changed, strings are immutable.
what does aString.split(‘a’) do?
splits the string stored in aString on the character ‘a’.
how do i search a variable (which is a string) to find the index where ‘xxx’ occurs?
aString.find('xxx') #returns an index (5)
aString.count(‘x’)
counts how many times the string ‘x’ occurs in the string ‘aString’.
what is SPLIT
The SPLIT method:
produces a list of strings using a character as a separator.
what will be the result of
aString = ‘Python’
list(aString)
[‘P’,’y’,’t’.’h’.’o’.’n’]
What is a tuple
A tuple is like a list except it is immutable
Xxxx = (10, ‘ten’, ‘x’)
What is this? How would I find the length of it and slice it?
This is a tuple
len(myTuple)
myTuple[1:]
What is a set
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
Xxx = {‘red’, 1}
What is this? How would I add a new term?
This is a set
aSet.add(‘blue’)
What would the result of
set(‘cocoa’)
Be?
{‘a’,’c’,’o’}
What does the | operator do?
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’}
What does the & operator do?
It produces a new set containing only items common to both sets.
What are the alternative methods for | and & ?
bSet.union(cSet)
& is
bSet.intersection(cSet)
is
How do I check to see if an item belongs to a set?
dSet = {4,6,7,8,9,5}
4 in dSet
How do I round the answer to a sum if it will be a float?
We can use round, for example
X=2/3
Y=round(x,4)
Print (y)
Will display x to four decimal places
What is the default approach by Python to separating and ending when using print ?
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
When using print in Python how do I change the separator and the end?
We can add optional arguements
Print(‘xx’,’xx’, sep=’’, end=’’)
Print(‘xx’,’xx’, sep=’’)
Gives
Xxxxxxxx
what two things make up the control structures
selection and iteration
what is selection?
its when a part of the algorithm follows different paths depending on some condition.
what is iteration
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.
xxxxxx:
xxxxx
above are two seperate parts of python code, what are they?
The first xxxx is the header and the second is the suite.
how to i quickly create a list of all the numbers between 0 and 4?
list(range(5))
what will
for x in range(5):
print(‘Fine and Dandy’)
acheieve?
it will print out fine and dandy 5 times
what will
for x in range(10, 101):
if x%2 == 0:
print(x)
do?
it will print the even numbers from 10 to 100
How would i create a list from 10 to 20 going up in steps of 3?
list(range(10, 20, 3))
using list comprehension how would i rewrite:-
sqlist =[]
for x in range )1,11):
sqlist.append(x*x)
?
sqlist=[x*x for x in range (1,11)]
what will
list=[x*x for x in range (1,11) if x%2 != 0]
acheieve?
it will print out the square numberds of the odd numbers between 0 and 10
what is a subprogram?
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.
in python how do you set out a user defined function?
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.
what will the following return?
def max2(x, y): z = x if x
it will return the value 12.
define a function mean that takes two numbers as arguements and returns their mean.
def mean (x,y): z = x + y z = z/2 return z
define a function that takes a string as an argument and prints the string between two sets of three stars,
def printWithStars (x): print ('***'+x+'***')
define a Function isEven that takes an argument which is a positive integer and returns True if the number is even, otherwise False
def isEven (x): if x
how do I define a variable to be used across programs?
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
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)
change the p inside the test() function to:
global p
when we are importing something into Python what is it that we are actually doing?
we are allowing functions that are stored in a some other module (library) to be used by our code.
how do we import the module TIME? How would we find the current date and time.
to import the time module and then call the ctime function we:
import time
print(time.ctime())
What exactly is OOP? and why is it useful in the study of algorithms?
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.
in OOP how are objects organised.
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.
What is often the key to finding a computational solution.
choosing a good representation of a problem.
How would i set up a data type called MONEY
class Money: def \_\_init\_\_(self, anAmount, aCurrency): self.amount = anAmount self.currency = aCurrency
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 constructor.- in the code it will appear as __init__ this is always the same and never changes.
in class Money: def \_\_init\_\_(self, anAmount, aCurrency): self.amount = anAmount self.currency = aCurrency what do the three arguments stand for?
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
what does the line
self.amount = anAmount
do when we are creating new data types.
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.
how would we check the values of an object?
we use dot notation, for example:
print(money1.amount)
print(money1.currency)
When thinking about classes what are methods?
The operations that belong to a class are called methods.
When building new classes from existing ones in OO languages, what are the existing class and the new class called?
the existing class is the parent and the new class is the child.