1 / 25

Variables: (assignment)

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 ==!

shay-cannon
Télécharger la présentation

Variables: (assignment)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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)

  2. 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)

  3. 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())

  4. 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)

  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)

  6. 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”)

  7. 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!

  8. Shortcuts >>> x = 4 >>> x +=2 x = x + 2 >>> x 6 >>> x -=7 >>> x -1 >>> x *= 32 >>> x -32 >>> x /=8 >>> x -4.0 >>>

  9. 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))

  10. 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?

  11. Strings: • Strings? • concatenate (join) + • make multiple copies using * • compare: > < == >= <= != • What else can we do?

  12. 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")

  13. 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))

  14. 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))

  15. 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.”)

  16. 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])

  17. 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

  18. # display a slice def g(s,f,wd): return("wd["+str(s)+":"+str(f)+"] is "+str(wd[s:f])) print(g(3,7,"sesquipedalian"))

  19. 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()

  20. 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))

  21. 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)))

  22. 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)))

  23. 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,""))

  24. 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))

  25. 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",""))

More Related