Download presentation
Presentation is loading. Please wait.
Published byAnthony Hunter Modified over 9 years ago
2
Largest of x random numbers def find_largest(x,y): largest = 0; count = 0; while count < x: rnum = randrange(0,y) if rnum > largest: largest = rnum count +=1 return(largest); print(find_largest(7,50)) Write a function that takes 2 integers (x and y) as input parameters, and generates x random numbers between 0 and y. It finds the largest of those random numbers.
3
What do we get? def q(x,y): ct = 0 while (ct < x): ct2 = 0 while (ct2 < y): print(ct + ct2) ct2 += 1 ct += 1 return q(4,3) Rules: The contents of the while loop is only what is indented under the while loop We do the contents of the while loop over and over until the while loop’s condition becomes false. Only then do we do what is below the while loop.
4
What does this print out? def h(x): ct = 1 while (ct <= x): ct2 = ct while (ct2<=x): print(ct2, end=“\t") #prints a tab after every ct2 ct2 += 1 ct += 1 print("\n") #this is a line break (starts a new line) return h(5)
5
Can you reverse it Output: 1 2 3 4 5 2 3 4 5 3 4 5 4 5 5 def h(x): ct = 1 while (ct <= x): ct2 = ct while (ct2<=x): print(ct2, end=" ") ct2 += 1 ct += 1 print("\n") return h(5) def h(x): ct = 1 while (ct <= x): ct2 = 1 while (ct2<=ct): print(ct2, end=" ") ct2 += 1 ct += 1 print("\n") h(5) Output: 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
6
What do we get? def g(a,b): while (a != b): while (a > b): a = a-b while(b>a): b = b-a return(a) print(g(6,20))
7
def f14(x): k = 0 n = -1 v = x while k <= (x*2): m = 0 while (m < v): print(v, end = "") m += 1 if (v == 0): n = 1 else: print() v += n k+=1 print() f14(3)
8
Lists: a new type! We've got: ints floats booleans strings and now… (drum roll) LISTS! sets of a thing (e.g., sets of ints)
9
Creating Lists To create a list, put a number of expressions in square brackets: listvar = [ ] #Empty list with nothing in it. A list with things in it: # create a list with some items listvar1 = [2,10,8,4,17] listvar2 = [“spider leg”,”toe of frog”,”eye of newt”, “bat wing”, “slug butter”, ”snake dandruff”] First list is a list of ints Second list is a list of strings.
10
Lists Overview The list type is a container that holds a number of objects, in a given order. Lists have an order to them Like a string, in which the order of the characters matters Examples of lists: Sound files: a long list of numbers measuring vibrations in the air any vibrational measurements The order of the measurements of vibration is dependent on when they occurred in time. List of class participants Measurements of movements (e.g., weather)
11
Lists: listvar2 = [“spider leg”,”toe of frog”,”eye of newt”, “bat wing”, “slug butter”,”snake dandruff”] Index 0 1 2 3 4 5 listvar1 = [2, 10, 8, 4, 17] Index 0 1 2 3 4 So to use a particular item in the list: x=listvar2[3] #”bat wing” y=listvar1[1] #10 z=listvar2[0] #”spider leg”
12
Lists: Like strings, we can use: len in listvar2 = [“spider leg”,”toe of frog”,”eye of newt”, “bat wing”, “slug butter”,”snake dandruff”] listvar1 = [2, 10, 8, 4, 17] get the length of a list len(listvar2) # will give you 6 if “eye of newt" in listvar2: return(“we can do chemistry!") else: return(“Sorry, no chemistry today.”)
13
Lists: Index: 0 1 2 3 4 5 listvar2 = [“spider leg”,”toe of frog”,”eye of newt”, “bat wing”, “slug butter”,”snake dandruff”] Slice: 0 1 2 3 4 5 6 -6 -5 -4 -3 -2 -1 listvar1 = [2, 10, 8, 4, 17] Slicing: x = listvar2[3:5] #x now holds [“bat wing”,”slug butter”] y = listvar1[1:2] #y now holds [10]
14
Lists:Slicing (Different from Indexing) Index: 0 1 2 3 4 5 listvar2 = [“spider leg”,”toe of frog”,”eye of newt”, “bat wing”, “slug butter”,”snake dandruff”] Slice: 0 1 2 3 4 5 6 -6 -5 -4 -3 -2 -1 def f(): return(listvar[0:6]) >>[“spider leg”,”toe of frog”,”eye of newt”, “bat wing”, “slug butter”,”snake dandruff”] def g(): return(listvar2[1:3]) >>[”toe of frog”,”eye of newt”] def h(): return(listvar2[-4:-2]) >>[”eye of newt”,”bat wing”] def i(): return(listvar2[-4:4]) >>[”eye of newt”,”bat wing”]
15
Shortcuts Index: 0 1 2 3 4 5 listvar2 = [“spider leg”,”toe of frog”,”eye of newt”, “bat wing”, “slug butter”,”snake dandruff”] Slice: 0 1 2 3 4 5 6 -6 -5 -4 -3 -2 -1 listvar2[0:4] [“spider leg”,”toe of frog”,”eye of newt”,”bat wing”] listvar2[:4] [“spider leg”,”toe of frog”,”eye of newt”,”bat wing”] listvar2[3:6] [“bat wing”, “slug butter”,”snake dandruff”] listvar2[3:] [“bat wing”, “slug butter”,”snake dandruff”] listvar2[:] [“spider leg”,”toe of frog”,”eye of newt”, “bat wing”, “slug butter”,”snake dandruff”]
16
Stuff we can do to lists that we can’t do with strings:) listofstuff = [“ant", “bear", “cat", “dog“,”elephant”,”fox”] # assign by index listofstuff[0] = “aardvark" print(listofstuff) >>>[“aardvark", “bear", “cat", “dog“,”elephant”,”fox”] # assign by slice listofstuff[3:5] = [“dolphin"] print(listofstuff) >>>[“aardvark", “bear", “cat", “dolphin”,”fox”] # delete an element del listofstuff[2] print(listofstuff) >>>[“aardvark", “bear", “dolphin”,”fox”] # delete a slice del listofstuff[:2] print(listofstuff) >>>[ “dolphin”,”fox”]
17
Concatenate(join) lists list1 = [“skeletons", “zombies ", “witches"] list2 = [“vampires", “ghouls ", “werewolves"] list3 = list1 + list2 print(list3) >>>[“skeletons", “zombies ", “witches“,”vampires", “ghouls ", “werewolves"] list1 +=list2 print(list1) >>>[“skeletons", “zombies ", “witches“,”vampires", “ghouls ", “werewolves"] list1[3]? list1[0]? list1[6]?
18
Note: adding to the end of the list: list1 = [“skeletons", “zombies ", “witches“,”vampires", “ghouls ", “werewolves"] We CAN do: list1[4] = “ghosts” print(list1) >>> [“skeletons", “zombies ", “witches“,”vampires", “ghosts ", “werewolves"] We CAN’T do: list1[6] = “poltergeists” (why not?)
19
Appending to end of list: list1 = [“skeletons", “zombies ", “witches“,”vampires", “ghouls ", “werewolves"] We CAN’T do: list1[6] = “poltergeists” Instead: list1.append(“poltergeists”) print(list1) >>[“skeletons", “zombies ", “witches“,”vampires", “ghouls ", “werewolves“, ”poltergeists”] Append adds an element to the end of a list (sets aside more RAM memory) Always to the end! It is a method (function) that belongs to anything that is of the list type Appending doesn’t work with strings (They’re immutable!)
20
List Methods Methods are functions that manipulate lists specifically lists are objects (object-oriented programming) Objects have methods (functions) associated with them. E.g., dog objects might have a walking function Square objects might have a calc_area function List objects have functions: e.g., Add an element reverse the list Sort a list Etc.
21
Removing an item from the list: list1=[“skeletons", “zombies ", “witches“,”vampires", “ghouls ", “werewolves“, ”poltergeists”] list1.remove(“werewolves”) >>[“skeletons", “zombies ", “witches“,”vampires", “ghouls ",”poltergeists”] Trying to remove something that isn’t in the list gives you an error: list1.remove(“ghosts”) #ERROR So check: if “vampires” in list1: list1.remove (“vampires”) print (list1) >>>[“skeletons", “zombies ", “witches“, “ghouls ", ”poltergeists”] remove() ONLY removes the first occurrence of an item in the list: list2 = [8,2,3,1,5,3,8,4,2,3,5] list2.remove(2) print(list2) >>[8,3,1,5,3,8,4,2,3,5]
22
list1=[“skeletons", “zombies ", “witches“, “ghouls ", ”poltergeists”] Reversing the order of the list: list1.reverse() >>>[‘poltergeist’,’ghouls’,’witches’,’zombies’,’skele tons’] Sorting the list list1.sort() >>>[‘ghouls’,’poltergeists’,’skeletons’,’witches’,’zo mbies’] Sorting the list list1.sort(reverse = True) >>>[‘zombies’,’witches’,’skeletons’,’poltergeists’,’g houls’]
23
Other methods available for lists count(value) – counts the number of times value occurs in list list2 = [8,2,3,1,5,3,8,4,2,3,5,3] x = list2.count(3) print(x) >>>4 index(value) – returns index of first occurrence of value list1=[“skeletons", “zombies ", “witches“, “ghouls ", ”poltergeists”] y = list1.index(“witches”) print(y) >>>2
24
pop([f]) – returns value at position f and removes value from list. Without f, it pops the last element off the list list1=[“skeletons", “zombies ", “witches“, “ghouls ", ”poltergeists”] x=list1.pop() print(x) >>”poltergeists” print (list1) >> =[“skeletons", “zombies ", “witches“, “ghouls ”] x=list1.pop(2) print(x) >>”witches” print (list1) >> [“skeletons", “zombies ", “ghouls ”] insert(f,value)- inserts value at position f list1.insert(1,’dragons”) print(list1) >>>[“skeletons", “dragons”, “zombies ", “ghouls ”]
25
def rg(x,y): while len(x) > 0: y.append(x.pop()) return(y) print(rg(['witch','ghost','werewolf','vampire'],[]))
26
def rf(y,ls): while ls.count(y)>0: if y in ls: z = ls.index(y) ls.pop(z) return(ls) x = ['a','c','b','a','d','b','a','c','b','f','c','b'] print(rf('c',x))
27
def k(x,y): k = True while y < len(x)//2: if (x[y] != x[len(x) - y-1]): k=False y+=1 return (x[y] == x[len(x) - y-1]) and k print(k("rotator",0)) print(k("clue",0))
28
def lr2(x,y,ls): while x < len(ls): if (ls[x] > y): y=ls[x] x += 1 return(y) listarr = [3,2,7,3,1] print(lr2(0,0,listarr))
29
def g(x,y): while (len(x) > 1): i = randrange(len(x)) y+=x[i] x = x[:i]+x[i+1:] return(y+x) print(g("computer","")) #A game comes to mind…
30
def f(x,y,z): while x < len(z): if y> z[x]: y=z[x] x+=1 return(y) print(f(0,"zzyrgy",['ghoul','zombie','werewolf','vampire','ghost','pumpkin'])) def g(x,z): while x < len(z): q = f(0,"zzyrgy",z[x:]) i = z.index(q) t = z[x] z[x] = z[i] z[i] = t x+=1 return(z) print(g(0,['ghoul','zombie','werewolf','vampire','ghost','pumpkin']))
31
def h(x): y = "" k = 0 while k<len(x): y+=(x[k][k]) k+= 1 return(y) ls = ['coffin','creepy','hayride','corpse','ghastly'] print(h(ls))
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.