Print: Sum Sum Range Sum Sum Sum
Print: Sum Sum Range Sum Sum Sum
com/RuQ2WFiY
1. #FUNCTIONS:
2. print("FUNCTIONS:")
3.
4. #keyword 'def' introduces a function definition followed by the
5. #function name and parameters (arguments) in parentheses
6.
7. #Reverse a string
8. print("Reverse:")
9. def reverse(text):
10. return text[::-1]
11.
12. print(reverse("This is a string"))
13. print()
14.
15. #Sum of all integers
16. def sum(low, high): #returns the sum of all integers
17. """Return the sum of all integers"""
18. sum = 0
19. for n in range(low,high+1):
20. sum+=n
21. return sum
22.
23. print("Sum of all numbers on a roulette wheel = {}".format(sum(0,36)))
24.
25. #Fibonacci
26. print("Example: Fibonacci")
27. def fib(n): # write Fibonacci series up to n
28. """Return a list containing the Fibonacci series up to n."""
29. result = [] #create an empty list
30. a, b = 0, 1
31. while a < n:
32. result.append(a) #calls a 'method' of the list 'object' result
33. #a method is a function that belongs to an object
34. #here it is equivalent to 'result = result + [a]'
35. a, b = b, a+b
36. return result
37.
38. #f = fib(100)
39. print(fib(100))
40. print()
41.
42. print("Example: Factorial")
43. def fac(num): #returns the factorial of num
44. """Return the factorial of a number"""
45. f = 1
46. for n in range(1,num+1):
47. f *= n
48. return f
49.
50. print(fac(5))
51. print()
52.
53. # Another Factorial Solution
54. def fact(num):
https://pastebin.com/print/RuQ2WFiY 1/3
4/8/2020 Pastebin.com - Printed Paste ID: https://pastebin.com/RuQ2WFiY
https://pastebin.com/print/RuQ2WFiY 2/3
4/8/2020 Pastebin.com - Printed Paste ID: https://pastebin.com/RuQ2WFiY
109. print()
110.
111. print("Lambda Functions:")
112. # Anonymous Lambda Functions
113. # Calculate sum of 2 numbers
114. sum = lambda arg1, arg2: arg1+arg2
115.
116. print(sum(10,20))
117. print()
118.
119. # Include any number of arguments
120. sum = lambda w, x, y, z: w+x+y+z
121.
122. print(sum(2,4,6,8))
123. print()
124.
125. # Convert Fahrenheit to Celsius
126. import math
127. Celsius = lambda F: float(5)/9*(F-32)
128.
129. print("72 °F =", round(Celsius(72),1), "°C")
130. #str1 = "72°F = %0.2f°C" % Celsius(72)
131. #print(str1)
132. print()
133.
134. # Calculate area of a circle
135. from math import pi
136. area = lambda rad: (rad**2)*pi
137.
138. #str1 = "The area for a circle of radius 2.5 = %0.2f" % area(2.5)
139. #print(str1)
140. print("The area of a circle with radius 2.5 =", round(area(2.5),2))
141. print()
142.
143. #Python Coding Style (PEP8)
144. #Use 4-space indentation.
145.
146. #Wrap lines so that they don’t exceed 79 characters.
147. #This helps users with small displays and makes it possible to have
148. # several code files side-by-side on larger displays.
149.
150. #Use blank lines to separate functions and classes,
151. # and larger blocks of code inside functions.
152.
153. #When possible, put comments on a line of their own.
154.
155. #Use docstrings.
156.
157. #Use spaces around operators and after commas,
158. # but not directly inside bracketing constructs: a = f(1, 2) + g(3, 4).
159.
160. #Name your classes and functions consistently;
161. # the convention is to use CamelCase for classes and
162. # lower_case_with_underscores for functions and methods.
https://pastebin.com/print/RuQ2WFiY 3/3