KEMBAR78
Python PCEP Creating Simple Functions | PPTX
Creating simple functions
Module 3 Functions, Tuples, Dictionaries,
Data processing
Module 3 Creating simple functions
Evaluating the BMI
def bmi(weight, height):
return weight / height ** 2
print(bmi(52.5, 1.65))
def ft_and_inch_to_m(ft, inch = 0.0):
return ft * 0.3048 + inch * 0.0254
def lb_to_kg(lb):
return lb * 0.45359237
def bmi(weight, height):
if height < 1.0 or height > 2.5 or 
weight < 20 or weight > 200:
return None
return weight / height ** 2
print(bmi(weight = lb_to_kg(176), height = ft_and_inch_to_m(5, 7)))
Module 3 Creating simple functions
Check triangles 1
def is_a_triangle(a, b, c):
if a + b <= c or b + c <= a or c + a <= b:
return False
return True
print(is_a_triangle(1, 1, 1))
print(is_a_triangle(1, 1, 3))
def is_a_triangle(a, b, c):
return a + b > c and b + c > a and c + a > b
print(is_a_triangle(1, 1, 1))
print(is_a_triangle(1, 1, 3))
True
False
True
False
Module 3 Creating simple functions
Check triangles 2
def is_a_triangle(a, b, c):
return a + b > c and b + c > a and c + a > b
a = float(input('Enter the first side's length: '))
b = float(input('Enter the second side's length: '))
c = float(input('Enter the third side's length: '))
if is_a_triangle(a, b, c):
print('Yes, it can be a triangle.')
else:
print('No, it can't be a triangle.')
Module 3 Creating simple functions
Check right-angle triangle
def is_a_triangle(a, b, c):
return a + b > c and b + c > a and c + a > b
def is_a_right_triangle(a, b, c):
if not is_a_triangle(a, b, c):
return False
if c > a and c > b:
return c ** 2 == a ** 2 + b ** 2
if a > b and a > c:
return a ** 2 == b ** 2 + c ** 2
print(is_a_right_triangle(5, 3, 4))
print(is_a_right_triangle(1, 3, 4))
Module 3 Creating simple functions
Evaluate a triangle's area
Heron's formula:
def is_a_triangle(a, b, c):
return a + b > c and b + c > a and c + a > b
def heron(a, b, c):
p = (a + b + c) / 2
return (p * (p - a) * (p - b) * (p - c)) ** 0.5
def area_of_triangle(a, b, c):
if not is_a_triangle(a, b, c):
return None
return heron(a, b, c)
print(area_of_triangle(1., 1., 2. ** .5))
Module 3 Creating simple functions
Factorials
0! = 1 (yes! it's true)
1! = 1
2! = 1 * 2
3! = 1 * 2 * 3
4! = 1 * 2 * 3 * 4
:
:
n! = 1 * 2 ** 3 * 4 * ... * n-1 * n
def factorial_function(n):
if n < 0:
return None
if n < 2:
return 1
product = 1
for i in range(2, n + 1):
product *= i
return product
for n in range(1, 6): # testing
print(n, factorial_function(n))
1 1
2 2
3 6
4 24
5 120
Module 3 Creating simple functions
Fibonacci numbers
• the first element of the sequence is equal to one
(Fib1 = 1)
• the second is also equal to one (Fib2 = 1)
• every subsequent number is the the_sum of the
two preceding numbers:
(Fibi = Fibi-1 + Fibi-2)
fib_1 = 1
fib_2 = 1
fib_3 = 1 + 1 = 2
fib_4 = 1 + 2 = 3
fib_5 = 2 + 3 = 5
fib_6 = 3 + 5 = 8
fib_7 = 5 + 8 = 13
def fib(n):
if n < 1:
return None
if n < 3:
return 1
elem_1 = elem_2 = 1
the_sum = 0
for i in range(3, n + 1):
the_sum = elem_1 + elem_2
elem_1, elem_2 = elem_2, the_sum
return the_sum
for n in range(1, 10): # testing
print(n, "->", fib(n))
1 -> 1
2 -> 1
3 -> 2
4 -> 3
5 -> 5
6 -> 8
7 -> 13
8 -> 21
9 -> 34
Module 3 Creating simple functions
Recursion
Fibi = Fibi-1 + Fibi-2
def fib(n):
if n < 1:
return None
if n < 3:
return 1
return fib(n - 1) + fib(n - 2)
n! = (n-1)! × n
def factorial_function(n):
if n < 0:
return None
if n < 2:
return 1
return n * factorial_function(n - 1)
Module 3 Creating simple functions
Key takeaways
A function can call other functions or even itself.
When a function calls itself, this situation is known as recursion.
Recursive calls consume a lot of memory.
# Recursive implementation of the factorial function.
def factorial(n):
if n == 1: # The base case (termination condition.)
return 1
else:
return n * factorial(n - 1)
print(factorial(4)) # 4 * 3 * 2 * 1 = 24

Python PCEP Creating Simple Functions

  • 1.
    Creating simple functions Module3 Functions, Tuples, Dictionaries, Data processing
  • 2.
    Module 3 Creatingsimple functions Evaluating the BMI def bmi(weight, height): return weight / height ** 2 print(bmi(52.5, 1.65)) def ft_and_inch_to_m(ft, inch = 0.0): return ft * 0.3048 + inch * 0.0254 def lb_to_kg(lb): return lb * 0.45359237 def bmi(weight, height): if height < 1.0 or height > 2.5 or weight < 20 or weight > 200: return None return weight / height ** 2 print(bmi(weight = lb_to_kg(176), height = ft_and_inch_to_m(5, 7)))
  • 3.
    Module 3 Creatingsimple functions Check triangles 1 def is_a_triangle(a, b, c): if a + b <= c or b + c <= a or c + a <= b: return False return True print(is_a_triangle(1, 1, 1)) print(is_a_triangle(1, 1, 3)) def is_a_triangle(a, b, c): return a + b > c and b + c > a and c + a > b print(is_a_triangle(1, 1, 1)) print(is_a_triangle(1, 1, 3)) True False True False
  • 4.
    Module 3 Creatingsimple functions Check triangles 2 def is_a_triangle(a, b, c): return a + b > c and b + c > a and c + a > b a = float(input('Enter the first side's length: ')) b = float(input('Enter the second side's length: ')) c = float(input('Enter the third side's length: ')) if is_a_triangle(a, b, c): print('Yes, it can be a triangle.') else: print('No, it can't be a triangle.')
  • 5.
    Module 3 Creatingsimple functions Check right-angle triangle def is_a_triangle(a, b, c): return a + b > c and b + c > a and c + a > b def is_a_right_triangle(a, b, c): if not is_a_triangle(a, b, c): return False if c > a and c > b: return c ** 2 == a ** 2 + b ** 2 if a > b and a > c: return a ** 2 == b ** 2 + c ** 2 print(is_a_right_triangle(5, 3, 4)) print(is_a_right_triangle(1, 3, 4))
  • 6.
    Module 3 Creatingsimple functions Evaluate a triangle's area Heron's formula: def is_a_triangle(a, b, c): return a + b > c and b + c > a and c + a > b def heron(a, b, c): p = (a + b + c) / 2 return (p * (p - a) * (p - b) * (p - c)) ** 0.5 def area_of_triangle(a, b, c): if not is_a_triangle(a, b, c): return None return heron(a, b, c) print(area_of_triangle(1., 1., 2. ** .5))
  • 7.
    Module 3 Creatingsimple functions Factorials 0! = 1 (yes! it's true) 1! = 1 2! = 1 * 2 3! = 1 * 2 * 3 4! = 1 * 2 * 3 * 4 : : n! = 1 * 2 ** 3 * 4 * ... * n-1 * n def factorial_function(n): if n < 0: return None if n < 2: return 1 product = 1 for i in range(2, n + 1): product *= i return product for n in range(1, 6): # testing print(n, factorial_function(n)) 1 1 2 2 3 6 4 24 5 120
  • 8.
    Module 3 Creatingsimple functions Fibonacci numbers • the first element of the sequence is equal to one (Fib1 = 1) • the second is also equal to one (Fib2 = 1) • every subsequent number is the the_sum of the two preceding numbers: (Fibi = Fibi-1 + Fibi-2) fib_1 = 1 fib_2 = 1 fib_3 = 1 + 1 = 2 fib_4 = 1 + 2 = 3 fib_5 = 2 + 3 = 5 fib_6 = 3 + 5 = 8 fib_7 = 5 + 8 = 13 def fib(n): if n < 1: return None if n < 3: return 1 elem_1 = elem_2 = 1 the_sum = 0 for i in range(3, n + 1): the_sum = elem_1 + elem_2 elem_1, elem_2 = elem_2, the_sum return the_sum for n in range(1, 10): # testing print(n, "->", fib(n)) 1 -> 1 2 -> 1 3 -> 2 4 -> 3 5 -> 5 6 -> 8 7 -> 13 8 -> 21 9 -> 34
  • 9.
    Module 3 Creatingsimple functions Recursion Fibi = Fibi-1 + Fibi-2 def fib(n): if n < 1: return None if n < 3: return 1 return fib(n - 1) + fib(n - 2) n! = (n-1)! × n def factorial_function(n): if n < 0: return None if n < 2: return 1 return n * factorial_function(n - 1)
  • 10.
    Module 3 Creatingsimple functions Key takeaways A function can call other functions or even itself. When a function calls itself, this situation is known as recursion. Recursive calls consume a lot of memory. # Recursive implementation of the factorial function. def factorial(n): if n == 1: # The base case (termination condition.) return 1 else: return n * factorial(n - 1) print(factorial(4)) # 4 * 3 * 2 * 1 = 24