Download presentation

Presentation is loading. Please wait.

Published byCalvin Haynes Modified about 1 year ago

1
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)) Largest of x random numbers 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.

2
Try: Multiplication tables Write a function that takes as an input parameter a number, and then prints out that number's multiplication table up through 10, e.g., if the number was 4, you'd get 0 4 8 12 … 40 (Or Fancier version): 0x4=0 1x4=4 2x4=8 3x4=12 … 10x4=40 def func(x): count = 0 while (count <= 10): print(count*x) count += 1 return (total) print(func(5)) def fancyfunc(x): count = 0 while (count <= 10): print(str(count)+"x"+str(x)+"="+str(count*x)) count += 1 return (total) print(fancyfunc(7))

3
from random import * def beloworabove(): count = 0 below = 0 above = 0 while (count < 1000): rand = randrange(-100,100) if (rand < 0): below += 1 else: above += 1 count += 1 if below > above: return("generated " + str(below) + " numbers below 0") else: return ("generated " + str(below) + " numbers above 0") print(beloworabove()) Average: count Write a function that takes no inputs. It generates 1000 random numbers between -100 and 100. It counts how many of the random numbers are below 0 and how many are at or above 0. It then tells you which is more likely, below 0 or at or above 0. Starting condition? What makes the loop stop? What inside the loop changes so that the loop will stop?

4
What do we get? from math import * def f(a,b): n = min(a,b) i = 1 g = 1 while (i <= n): if (a%i == 0) and (b%i == 0): g = i i = i+1 return(g) print(f(32,24)) print(f(13,8)) print(f(24,16))

5
For loop: another type of loop We use For loops when we know exactly how many times the loop will occur Form: for variable in [value1, value2,value3…lastvalue]: calculations Example: def f(): for x in [1,2,3,4,5]: print(x) return(x) print(f())

6
More for loops: def f(): for x in [1,3,5,7,9]: print(x) return(x) print(f()) def f(): for x in [2,7,1,9,4]: print(x) return(x) print(f())

7
More for loops: def f(): y = 0 ct = 0 for x in [3.2, 7.1, 8.0, 3.4, 5.1]: print("including " + str(x)) ct +=1 y = y + x return(y/ct) print(f())

8
Loops with strings: def f(y): ct = 0 for x in ["puppy","bunny","puppy","bird","echidna","puppy"]: if x == y: ct += 1 return(ct) print(f("puppy"))

9
More for loops: def f(): for x in [0,1,2,3,4]: print(x) return(x) print(f()) Shortcut: using range def f(): for x in range(5): # range(5) = [0,1,2,3,4] print(x) return(x) print(f())

10
Same? def forfunc(y): total = 0 for x in range(y): total += x return(total) print(forfunc(5)) def whilefunc(y): count = 0 total = 0 while (count < y): total += count count += 1 return (total) print(whilefunc(5))

11
More on Range: def f(): for x in range(-3,3): # from -3 up to but not including 3, e.g., [-3,-2,-1,0,1,2] print(x) return(x) print(f()) def f(): for x in range(-3,3,2): # from -3 up to but not including 3, by increments of 2, e.g., [-3,-1,1] print(x) return(x) print(f()) (Can we make a loop go backwards?)

12
What does this do? def f(): y = 1000 total = 0 for x in ["2","7","1","9"]: total = total + int(x) * int(y) y /=10 return(total) print(f())

13
What does this do? def f(z): y = int(input("enter a number")) for x in range(y): print (str(z)+ "*"+str(x)+"=\t"+str(x*z)) print ("\n") return f(4)

14
How about this? def f(z): y = int(input("enter a number: ")) for x in range(1,y): for q in range(1,z): print (str(q)+ "*"+str(x)+"=\t"+str(x*q)) print ("\n") return f(4)

15
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")

16
Positions def f(wv): return(wv[3]) wordvar = “cats” print(f(wordvar))

17
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])

18
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

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

20
Strings are Immutable word 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()

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

22
What does this do? def f(): strvar = input("enter a string: ") var1 = "" for x in range(len(strvar) - 1,-1,-1): var1 += strvar[x]; return(var1) print(f())

23
This one? def f(): strvar = input("enter a number: ") y = 1 z = 0 var1 = "" for x in range(len(strvar) - 1,- 1,-1): z += int(strvar[x]) * y; y*=10 return(z) print(f())

24
What does this give you? def f(lv): x = len(lv) print(x) for y in range(0,x): if "t" in lv[y]: print(lv[y]) return listvar = ["ham","am","boat","goat","there","anywhere"] f(listvar)

25
How about this one? def f(word): newstr = "" for i in range(1,len(word)+1): newstr += word[-i] return(newstr) wvar = "sesquipedalian" print(f(wvar))

26
What does this do? def f(word): high = len(word) low = 0 newstr = "" for i in range(10): position = randrange(low, high) newstr += word[position] return(newstr) wvar = "sesquipedalian" print(f(wvar))

27
How about this one? def f(m): new_m = "" SPECIAL = "dlmstp" for k in m: if k.lower() not in SPECIAL: new_m += k return(new_m) mvar = "Hi, my name is Lassie" print("Your message is: now" + f(mvar))

28
Something you can’t do word = “ night”; word[0] = “s”; Instead: newword = “” for x in “night”: if x == “n": newword += “s" else: newword += x

29
What do you get? def f(message): newmessage = "" for x in message: if x == "g": newmessage += "l" else: newmessage += x return(newmessage) mvar = "pogysyggabicaggy" print("new string: " + f(mvar))

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google