250 likes | 317 Views
Variables: (assignment). A variable is a name we give to a space in memory. We put values into that space, and then use the name as if it is the value. A lot like parameters, only we create them within the function Then we can use them inside the function y= 3 # note we’re using = not ==!
E N D
Variables: (assignment) • A variable is a name we give to a space in memory. • We put values into that space, and then use the name as if it is the value. • A lot like parameters, only we create them within the function • Then we can use them inside the function y= 3 # note we’re using = not ==! • This says y now holds the value 3, just like parameters def f(x): #simple example y = 3 # y only exists within this function return(x + y)
Variables and Random numbers: • You’ve used variables with Random Numbers • To generate random numbers between x and y: randrange(x,y) • To store the random number: randvar= randrange(0,100) • generates a random number between 0 and 99 (inclusive) and stores it in randvar • Now you can use randvar again and again throughout the function • To see what number is generated: print (randvar)
from random import * def f(): x = int(input("heads or tails? (1 or 0) ")) y = randrange(0,2) if x == y: return("You guessed right! You both guessed " + str(x)) else: return("You were wrong. You guessed "+str(x)+" and the computer generated "+str(y)) print(f())
More with Variables: def f(x): y=3 y=y+x# Do the right side first, then put # that value into the left side. return(y**2) # This is where the function ends. f(5)
More examples: def calcvol(length,width,depth): area = length * width #area only exists inside this function vol = area * depth return(vol) print(calcvol(3,2,4) defbankaccount(x,add): dollars = 2.57 print("Currently, you have " + str(dollars) + " in your bank account") if add == 1: dollars = dollars + x # evaluate right, then assign to left else: dollars = dollars - x return("you now have " + str(dollars) + " in your bank account") #again, function ends when the return statement is executed. print(bankaccount(0.10,1) print(bankaccount(1.0, 0)
Inefficient – why?: def getTot(yp,yl,ye,p,l,e): return(yp * p/100 + yl * l/100 + ye * e/100) defcalcGrade(yp,yl,ye,proj,lab,exam): if getTot(yp,yl,ye,proj,lab,exam) >= 90: return (“A”) elifgetTot(yp,yl,ye,proj,lab,exam) >= 80: return (“B”) elifgetTot(yp,yl,ye,proj,lab,exam) >= 70: return (“C”) elifgetTot(yp,yl,ye,proj,lab,exam) >= 60: return (“D”) else return (“F”)
def getTot(yp,yl,ye,p,l,e): return(yp * p/100 + yl * l/100 + ye * e/100) defcalcGrade(yourproj,yourlab,yourexam,proj,lab,exam): yourscore = getTot(yourproj, yourlab, yourexam,proj,lab,exam) if yourscore >= 90: return (“A”) elifyourscore >= 80: return (“B”) elifyourscore >= 70: return (“C”) elifyourscore >= 60: return (“D”) else return (“F”) print(calcGrade(95,30,25,30,4,40)) Pretty and efficient!
Shortcuts >>> x = 4 >>> x +=2 x = x + 2 >>> x 6 >>> x -=7 >>> x -1 >>> x *= 32 >>> x -32 >>> x /=8 >>> x -4.0 >>>
Example: def f(p1,p2): if p1>p2: x = p1-p2 else: x = p2-p1 if (x%2) == 1: # x is now what value? x+=1 # Now what is x? x/=2 # and now what is x? return(x) print(f(7,2)) print(f(24,82))
Adding a Curve? def getTot(yp,yl,ye,p,l,e): return(yp * p/100 + yl * l/100 + ye * e/100) def calcGrade(yourproj,yourlab,yourexam,proj,lab,exam): yourscore = getTot(yourproj, yourlab, yourexam,proj,lab,exam) if yourscore >= 90: return (“A”) elifyourscore >= 80: return (“B”) elifyourscore >= 70: return (“C”) elifyourscore >= 60: return (“D”) else return (“F”) How can we do it with this?
Strings: • Strings? • concatenate (join) + • make multiple copies using * • compare: > < == >= <= != • What else can we do?
Just about anything you can do with lists: Len, in def f(x): if "e" in x: return("e is in your message.") else: return("e is not in your message.") strvar = “puppies are cute” print(f(strvar)) z = len("cat")
Strings Positional stringvar1 = “ binary” Index 012345 stringvar2 = “ computer” Index 01234567 So to use a particular item in the list: x=stringvar2[3] #”p” y=stringvar1[1] #”i” z=stringvar2[0] #”c” w=stringvar1[6] #??? Example: deff(par): randnum = randrange(0,6) return(“you get “ + par[randnum]) print(f((stringvar1))
String: stringvar1 = “binary” Index 012345 stringvar2 = “ computer” Index 01234567 get the length of a string len(stringvar1) # will give you 6, not 5! len(stringvar2) #will give you 8, not 7 Example: deff(par): y = len(par) randnum = randrange(0,y) return(“you get “ + par[randnum]) print(f(stringvar2))
Strings: stringvar1 = “binary” Index 012345 stringvar2 = “ computer” Index 01234567 test for membership with in if ‘e’ instringvar2: return(“we can do chemistry!") else: return(“Sorry, no chemistry today.”)
Slicing (Different from Indexing) • Copying parts of strings: 0 1 2 3 4 5 | p | i | z | z | a | -5 -4 -3 -2 -1 def f():word = “pizza” return(word[0:5]) def g(): word = “pizza” return(word[1:3]) def h(): word = “pizza” return(word[-4:-2]) def i(): word = “pizza” return(word[-4:3])
Shortcuts 0 1 2 3 4 5 | p | i | z | z | a | -5 -4 -3 -2 -1 word=“pizza” word[0:4] pizz word[:4] pizz word[2:5] zza word[2:] zza
# display a slice def g(s,f,wd): return("wd["+str(s)+":"+str(f)+"] is "+str(wd[s:f])) print(g(3,7,"sesquipedalian"))
Strings are Immutableword of the day • Can: • x = "catamaran" • print(x.count("a")) • print(x.index('a')) • Can’t (if it changes the string, we can’t do it) • Append (use + instead) • Reverse() • Pop() • Insert() • Sort()
What does this do? def f(str1): str2 = “WuGmUmP” if str1.upper() == str2.upper(): return(“You’re one of us!”) else: return(“You’re not one of us!”) newstr = “wuGMuMp” print(f(newstr))
def rec(a,x,y): if (x == len(a)): return 0 elif (a[x] in y): return 1 + rec(a,x+1,y) else: return rec(a, x+1, y) a = "catamaran" y = "aeiou" print(str(rec(a,0,y)))
def rec(a,x,y): if (x == len(a)): return "" elif (a[x] in y): return rec(a,x+1,y) else: return (a[x] + rec(a, x+1, y)) a = "catamaran" y = "aeiou" print(str(rec(a,0,y)))
def f(x,y,z): if y == len(x): return(z) else: if (x[y] == "p"): return(f(x,y+1,z+"m")) else: return(f(x,y+1,z+x[y])) print(f("puppy",0,""))
def k(x,y): if y >= len(x)//2: return (x[y] == x[len(x) - y-1]) elif (x[y] != x[len(x) - y-1]): return(False) else: return(k(x,y+1)) print(k("rotator",0)) print(k(“clue",0)) print(k("pop",0)) print(k("cat",0))
def k(x,y,z): if (z == len(x)): return("") else: if (z != y): return(x[z] + k(x,y,z+1)) else: return(k(x,y,z+1)) print(k("binary",2,0)) def g(x,y): if (len(x) == 1): return(y+x) else: i = randrange(len(x)) return(g(k(x,i,0),y+x[i])) print(g("computer",""))