SlideShare a Scribd company logo
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

More Related Content

PDF
Monads from Definition
PPTX
4.1 inverse functions t
PPT
Writing linear equations
PPT
Linear combination
PPT
8.4 Rules For Linear Functions
PDF
Random Number Generation
PPT
Multi dimensional arrays
PPT
5.1 Linear Functions And Graphs
Monads from Definition
4.1 inverse functions t
Writing linear equations
Linear combination
8.4 Rules For Linear Functions
Random Number Generation
Multi dimensional arrays
5.1 Linear Functions And Graphs

What's hot (19)

PDF
Evaluating functions
PDF
Array and pointer
PPTX
2.3 slopes and difference quotient t
PPT
Matdis 3.4
PPS
Higher Order Procedures (in Ruby)
PDF
Ch07 13
PPTX
Evaluating a function
PPT
Composition Of Functions
Β 
DOCX
ΰΉ€ΰΈ‹ΰΈ•
PPT
Data Structure and Algorithms Graphs
PPTX
Prac 3 lagrange's thm
PPT
Vector bits and pieces
PPT
Function Operations
PPT
Vectors intro
PPT
Vector multiplication dot product
PPT
1 5 graphs of functions
PPTX
Algebra 6 Point 1
PDF
7th PreAlg - L53--Jan31
PDF
Day 5 u8f13
Evaluating functions
Array and pointer
2.3 slopes and difference quotient t
Matdis 3.4
Higher Order Procedures (in Ruby)
Ch07 13
Evaluating a function
Composition Of Functions
Β 
ΰΉ€ΰΈ‹ΰΈ•
Data Structure and Algorithms Graphs
Prac 3 lagrange's thm
Vector bits and pieces
Function Operations
Vectors intro
Vector multiplication dot product
1 5 graphs of functions
Algebra 6 Point 1
7th PreAlg - L53--Jan31
Day 5 u8f13
Ad

Similar to Python PCEP Creating Simple Functions (20)

PDF
PyLecture4 -Python Basics2-
PPTX
Lecture 5 – Computing with Numbers (Math Lib).pptx
PPTX
Lecture 5 – Computing with Numbers (Math Lib).pptx
PDF
Recursive Algorithms coded in Python.pdf
PDF
Math Modules (DRAFT)
PDF
Some hours of python
PDF
Python lambda functions with filter, map & reduce function
PPT
tutorial5.ppt
DOCX
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
PPTX
function_xii-BY APARNA DENDRE (1).pdf.pptx
PDF
Lecture 5 numbers and built in functions
DOCX
Python Math Concepts Book
PPTX
Python Tidbits
PPTX
Python Lecture 4
PPTX
Pytho-Chapter-4-Functions.pptx
PDF
Class 7a: Functions
ODP
Python basics
PDF
Talk Code
PPTX
Unit 2function in python.pptx
PyLecture4 -Python Basics2-
Lecture 5 – Computing with Numbers (Math Lib).pptx
Lecture 5 – Computing with Numbers (Math Lib).pptx
Recursive Algorithms coded in Python.pdf
Math Modules (DRAFT)
Some hours of python
Python lambda functions with filter, map & reduce function
tutorial5.ppt
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
function_xii-BY APARNA DENDRE (1).pdf.pptx
Lecture 5 numbers and built in functions
Python Math Concepts Book
Python Tidbits
Python Lecture 4
Pytho-Chapter-4-Functions.pptx
Class 7a: Functions
Python basics
Talk Code
Unit 2function in python.pptx
Ad

More from IHTMINSTITUTE (19)

PPTX
Python PCEP Tuples and Dictionaries
PPTX
Python PCEP Tuples and Dictionaries
PPTX
Python PCEP Functions And Scopes
PPTX
Python PCEP Function Parameters
PPTX
Python PCEP Functions
PPTX
Python PCEP Multidemensional Arrays
PPTX
Python PCEP Operations On Lists
PPTX
Python PCEP Sorting Simple Lists
PPTX
Python PCEP Lists Collections of Data
PPTX
Python PCEP Logic Bit Operations
PPTX
Python PCEP Loops
PPTX
Python PCEP Comparison Operators And Conditional Execution
PPTX
Python PCEP How To Talk To Computer
PPTX
Python PCEP Variables
PPTX
Python PCEP Operators
PPTX
Python PCEP Literals
PPTX
IHTM Python PCEP Hello World
PPTX
IHTM Python PCEP Introduction to Python
PPTX
Python PCEP Welcome Opening
Python PCEP Tuples and Dictionaries
Python PCEP Tuples and Dictionaries
Python PCEP Functions And Scopes
Python PCEP Function Parameters
Python PCEP Functions
Python PCEP Multidemensional Arrays
Python PCEP Operations On Lists
Python PCEP Sorting Simple Lists
Python PCEP Lists Collections of Data
Python PCEP Logic Bit Operations
Python PCEP Loops
Python PCEP Comparison Operators And Conditional Execution
Python PCEP How To Talk To Computer
Python PCEP Variables
Python PCEP Operators
Python PCEP Literals
IHTM Python PCEP Hello World
IHTM Python PCEP Introduction to Python
Python PCEP Welcome Opening

Recently uploaded (20)

PDF
SASE Traffic Flow - ZTNA Connector-1.pdf
PDF
How to Ensure Data Integrity During Shopify Migration_ Best Practices for Sec...
PPTX
Job_Card_System_Styled_lorem_ipsum_.pptx
PDF
RPKI Status Update, presented by Makito Lay at IDNOG 10
Β 
PDF
Introduction to the IoT system, how the IoT system works
PDF
Best Practices for Testing and Debugging Shopify Third-Party API Integrations...
PPTX
Digital Literacy And Online Safety on internet
PPTX
Power Point - Lesson 3_2.pptx grad school presentation
Β 
PPT
Design_with_Watersergyerge45hrbgre4top (1).ppt
PPTX
innovation process that make everything different.pptx
PDF
πŸ’° π”πŠπ“πˆ πŠπ„πŒπ„ππ€ππ†π€π πŠπˆππ„π‘πŸ’πƒ π‡π€π‘πˆ 𝐈𝐍𝐈 πŸπŸŽπŸπŸ“ πŸ’°
Β 
PDF
Automated vs Manual WooCommerce to Shopify Migration_ Pros & Cons.pdf
PPTX
international classification of diseases ICD-10 review PPT.pptx
PPTX
Funds Management Learning Material for Beg
PPT
tcp ip networks nd ip layering assotred slides
PDF
Sims 4 Historia para lo sims 4 para jugar
PDF
Vigrab.top – Online Tool for Downloading and Converting Social Media Videos a...
PPTX
E -tech empowerment technologies PowerPoint
DOCX
Unit-3 cyber security network security of internet system
PPTX
Introuction about WHO-FIC in ICD-10.pptx
SASE Traffic Flow - ZTNA Connector-1.pdf
How to Ensure Data Integrity During Shopify Migration_ Best Practices for Sec...
Job_Card_System_Styled_lorem_ipsum_.pptx
RPKI Status Update, presented by Makito Lay at IDNOG 10
Β 
Introduction to the IoT system, how the IoT system works
Best Practices for Testing and Debugging Shopify Third-Party API Integrations...
Digital Literacy And Online Safety on internet
Power Point - Lesson 3_2.pptx grad school presentation
Β 
Design_with_Watersergyerge45hrbgre4top (1).ppt
innovation process that make everything different.pptx
πŸ’° π”πŠπ“πˆ πŠπ„πŒπ„ππ€ππ†π€π πŠπˆππ„π‘πŸ’πƒ π‡π€π‘πˆ 𝐈𝐍𝐈 πŸπŸŽπŸπŸ“ πŸ’°
Β 
Automated vs Manual WooCommerce to Shopify Migration_ Pros & Cons.pdf
international classification of diseases ICD-10 review PPT.pptx
Funds Management Learning Material for Beg
tcp ip networks nd ip layering assotred slides
Sims 4 Historia para lo sims 4 para jugar
Vigrab.top – Online Tool for Downloading and Converting Social Media Videos a...
E -tech empowerment technologies PowerPoint
Unit-3 cyber security network security of internet system
Introuction about WHO-FIC in ICD-10.pptx

Python PCEP Creating Simple Functions

  • 1. Creating simple functions Module 3 Functions, Tuples, Dictionaries, Data processing
  • 2. 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)))
  • 3. 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
  • 4. 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.')
  • 5. 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))
  • 6. 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))
  • 7. 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
  • 8. 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
  • 9. 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)
  • 10. 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