0% found this document useful (0 votes)
39 views

Print: Sum Sum Range Sum Sum Sum

The document defines several functions including ones to reverse a string, calculate the sum of integers in a range, generate the Fibonacci sequence up to a given number, calculate factorials, find the volume of a cylinder, calculate resistance of resistors in series, use Ohm's law to calculate voltage given current and resistance, find cube roots and nth roots of numbers, and define anonymous lambda functions to calculate sums and convert between Fahrenheit and Celsius. It also discusses Python coding style recommendations like indentation, line length, blank lines, commenting, docstrings, and naming conventions.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Print: Sum Sum Range Sum Sum Sum

The document defines several functions including ones to reverse a string, calculate the sum of integers in a range, generate the Fibonacci sequence up to a given number, calculate factorials, find the volume of a cylinder, calculate resistance of resistors in series, use Ohm's law to calculate voltage given current and resistance, find cube roots and nth roots of numbers, and define anonymous lambda functions to calculate sums and convert between Fahrenheit and Celsius. It also discusses Python coding style recommendations like indentation, line length, blank lines, commenting, docstrings, and naming conventions.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

4/8/2020 Pastebin.com - Printed Paste ID: https://pastebin.

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

55. """Return the factorial of a number"""


56. if num == 1:
57. return 1
58. else:
59. return num * fact(num-1) #calculation calls itself
60.
61. print("5! = {}".format(fact(5)))
62. print()
63.
64. print("Example: Vol of a cylinder")
65. from math import pi
66. def vol_of_cyl(radius, height):
67. """Return the volume of a cylinder given the radius and height"""
68. return (pi*(radius**2)*height)
69.
70. print("Radius = 1.2")
71. print("Height = 4")
72. print("Volume =", round(vol_of_cyl(1.2,4),3))
73. print()
74.
75. print("Example: Resistors in series")
76. def res_in_series(r1, r2):
77. """Return the overall resistance of 2 resistors in series"""
78. return (r1+r2)
79.
80. print(res_in_series.__doc__)
81. print("Overall resistance = ", int(res_in_series(1000,2200)))
82. print()
83.
84. print("Example: Ohms Law")
85. #default values can be used when arguments are defined
86. def V(I=1, R=1):
87. """Return the voltage given the current and resistance"""
88. return (I*R)
89.
90. print("Voltage =", round(V(),2), "V")
91. print("Voltage =", round(V(2.1),2), "V")
92. print("Voltage =", round(V(0.02,250),2), "V")
93. print()
94.
95. # cube root of a number
96. def cuberoot(num):
97. """Return the cube root of a number"""
98. result = num**(1/3)
99. return result
100.
101. # nth root of a number
102. def root(num,root):
103. """Return the nth root of a number"""
104. result = num**(1/root)
105. return result
106.
107. print(root(5, 3))
108. print(root(12, 7))

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

You might also like