This document contains 20 examples of Python functions that demonstrate various programming concepts like defining functions, using parameters, conditionals, loops, recursion, and more. The functions cover topics like string manipulation, math operations, turtle graphics, and working with lists and dictionaries.
This document contains 20 examples of Python functions that demonstrate various programming concepts like defining functions, using parameters, conditionals, loops, recursion, and more. The functions cover topics like string manipulation, math operations, turtle graphics, and working with lists and dictionaries.
This document contains 20 examples of Python functions that demonstrate various programming concepts like defining functions, using parameters, conditionals, loops, recursion, and more. The functions cover topics like string manipulation, math operations, turtle graphics, and working with lists and dictionaries.
OUTPUT 4 CHAPTER 5 FUNDAMENTALS OF COMPUTING MATH 105
CARPIO, JOEZERK A. SAIPUDDIN, ABDULKADIR B. MAQUILAN, CHRISTLE JUDE L. BS-MATH 1A
MR. JERSON. S. MOHAMMAD
MATH 105 INSTRUCTOR Example 5.1 def g(x): return x**4 / 4.0 - x**3 / 3.0 - 3 * x * x print(g(6)) Example 5.2 def computeAndPrint(x,y): val = x**4 / 4.0 - x**3 / 3.0 - 3 * x * x + y print("You called computeAndPrint("+str(x)+","+str(y)+")") return val print(computeAndPrint(6,5)) Example 5.3 def reverse(s): result = "" for c in s: result = c + result return result t = input("Please enter a string: ") print("The reverse of", t, "is", reverse(t)) Example 5.4 def reverse(s): values.append(s): result = "" for c in s: result = c + result return result values = [] t = input("Please enter a string: ") while t.strip() != "": print("The reverse of", t, "is", reverse(t)) t = input("Enter another string or press enter to quit: ") print("You reversed these strings:") for val in values: print(val) Example 5.5 def reverse(s): values = values + [s] result = "" for c in s: result = c + result return result values = [] t = input("Please enter a string: ") while t.strip() != "": print("The reverse of", t, "is", reverse(t)) t = input("Enter another string or press enter to quit: ") print("You reversed these strings:") for val in values: print(val) Example 5.6 def reverse(s): global values values = values + [s] result = "" for c in s: result = c + result return result values = [] t = input("Please enter a string: ") while t.strip() != "": print("The reverse of", t, "is", reverse(t)) t = input("Enter another string or press enter to quit: ") print("You reversed these strings:") for val in values: print(val) Example 5.7 def reverse(s): result = "" for c in s: result = c + result return result t = input("Please enter a string: ") while t.strip() != "": print("The reverse of", t, "is", reverse(t)) t = input("Enter another string or press enter to quit: ") Example 5.8 def reverseInPlace(lst): for i in range(len(lst)//2): tmp = lst[i] lst[i] = lst[len(lst)-1-i] lst[len(lst)-1-i]=tmp s = input("Please enter a sentence:") lst = s.split() reverseInPlace(lst) print("The sentence backwards is:") for word in lst: print(word) Example 5.9 def evenlyDivides(x,y): dividesIt = False if x % y == 0: dividesIt = True return dividesIt x = int(input("Please enter an integer:")) y = int(input("Please enter another integer:")) if evenlyDivides(x,y): print(x,"evenly divides",y) else: print(x,"does not evenly divide",y) Example 5.10 def evenlyDivides(x,y): if x % y == 0: return True return False x = int(input("Please enter an integer:")) y = int(input("Please enter another integer:")) if evenlyDivides(x,y): print(x,"evenly divides",y) else: print(x,"does not evenly divide",y) Example 5.11 def evenlyDivides(x,y): return x % y == 0 x = int(input("Please enter an integer:")) y = int(input("Please enter another integer:")) if evenlyDivides(x,y): print(x,"evenly divides",y) else: print(x,"does not evenly divide",y) Example 5.12 s = input("Please enter a list of ints separated by spaces:") lst = [] for x in s.split(): lst.append(int(x)) evenlyDivisible(lst) Example 5.13 def factorial(n): if n == 0: return 1 return n * factorial(n - 1) print(factorial(5)) Example 5.14 def reverse(s): if s == "": return "" return reverse(s[1:]) + s[0] print(reverse("hello")) Example 5.15 import turtle def drawSquare(turtle, length): for k in range(4): turtle.forward(l) turtle.left(90) t = turtle.Turtle() screen = t.getscreen() l = int(input("Please enter a side length: ")) drawSquare(t,l) screen.exitonclick() Example 5.16 import turtle def drawSquare(turtle, length): for k in range(4): turtle.forward(length) turtle.left(90) def main(): t = turtle.Turtle() screen = t.getscreen() l = int(input("Please enter a side length: ")) drawSquare(t,l) screen.exitonclick() if __name__ == "__main__": main() Example 5.17 if __name__ == "__main__": def switchpen(): if isdown(): pu() else: pd() def demo1(): reset() def demo2(): speed(1) demo1() demo2() exitonclick() Example 5.18 def drawSquare(turtle,length=20): for k in range(4): t = turtle.Turtle() turtle.forward(length) turtle.left(90) drawSquare(t,40) drawSquare(t) drawSquare(length=30,turtle=t) Example 5.19 import turtle def drawFigure(turtle, *args): for i in range(0,len(args),2): turtle.forward(args[i]) turtle.left(args[i+1]) def main(): t = turtle.Turtle() screen = t.getscreen() drawFigure(t,50,90,30,90,50,90,30,90) screen.exitonclick() if __name__ == "__main__": main() Example 5.20 dimensions = {} dimensions['width'] = 20 dimensions['height'] = 40 Example 5.21 import turtle def drawRectangle(turtle, **dimensions): width = 10 height = 10 if "width" in dimensions: width = dimensions["width"] if "height" in dimensions: height = dimensions["height"] drawFigure(turtle,width,90,height,90,width,90,height,90) def drawFigure(turtle, *args): for i in range(0,len(args),2): turtle.forward(args[i]) turtle.left(args[i+1]) def main(): t = turtle.Turtle() screen = t.getscreen() drawRectangle(t,width=40,height=20) screen.exitonclick() if __name__ == "__main__": main()