260 likes | 373 Vues
This guide presents three Python functions: one to generate a specified number of random integers within a range, returning the largest; another to create a standard multiplication table for a given number up to 10; and a fancier version that includes formatted outputs. Lastly, a third function assesses and counts random numbers to evaluate the likelihood of being above or below zero. Each function features examples and explanations to enhance understanding of loops and randomness in programming.
E N D
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. deffind_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))
Try: Multiplication tables • 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)) • 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
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? 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())
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))
For loop: another type of loop • We use For loops when we know exactly how many times the loop will occur • Form: for variablein [value1, value2,value3…lastvalue]: calculations • Example: def f(): for x in [1,2,3,4,5]: print(x) return(x) print(f())
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())
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())
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"))
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())
Same? def whilefunc(y): count = 0 total = 0 while (count < y): total += count count += 1 return (total) print(whilefunc(5)) def forfunc(y): total = 0 for x in range(y): total += x return(total) print(forfunc(5))
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?)
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())
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)
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")
Positions def f(wv): return(wv[3]) wordvar = “cats” print(f(wordvar))
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(): strvar = input("enter a string: ") var1 = "" for x in range(len(strvar) - 1,-1,-1): var1 += strvar[x]; return(var1) print(f())
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())
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)
What does this do? deff(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))
How about this one? deff(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))
Something you can’t do word = “ night”; word[0] = “s”; Instead: newword = “” for x in “night”: if x == “n": newword+= “s" else: newword+= x
What do you get? deff(message): newmessage= "" for x in message: if x == "g": newmessage+= "l" else: newmessage+= x return(newmessage) mvar= "pogysyggabicaggy" print("new string: " + f(mvar))