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
Ad

Recommended

PDF
Monads from Definition
Dierk König
 
PPTX
4.1 inverse functions t
math260
 
PPT
Writing linear equations
Jessica Garcia
 
PPT
Linear combination
joannahstevens
 
PPT
8.4 Rules For Linear Functions
Jessca Lundin
 
PDF
Random Number Generation
Kishoj Bajracharya
 
PPT
Multi dimensional arrays
Aseelhalees
 
PPT
5.1 Linear Functions And Graphs
vmonacelli
 
PDF
Evaluating functions
REYEMMANUELILUMBA
 
PDF
Array and pointer
ShinRongHuang
 
PPTX
2.3 slopes and difference quotient t
math260
 
PPT
Matdis 3.4
Claudia Primasiwi
 
PPS
Higher Order Procedures (in Ruby)
Nate Murray
 
PDF
Ch07 13
schibu20
 
PPTX
Evaluating a function
MartinGeraldine
 
PPT
Composition Of Functions
sjwong
 
DOCX
เซต
Khiaokha
 
PPT
Data Structure and Algorithms Graphs
ManishPrajapati78
 
PPTX
Prac 3 lagrange's thm
Himani Asija
 
PPT
Vector bits and pieces
Shaun Wilson
 
PPT
Function Operations
swartzje
 
PPT
Vectors intro
Shaun Wilson
 
PPT
Vector multiplication dot product
Shaun Wilson
 
PPT
1 5 graphs of functions
Alaina Wright
 
PPTX
Algebra 6 Point 1
herbison
 
PDF
7th PreAlg - L53--Jan31
jdurst65
 
PDF
Day 5 u8f13
jchartiersjsd
 
PDF
PyLecture4 -Python Basics2-
Yoshiki Satotani
 
PPTX
Lecture 5 – Computing with Numbers (Math Lib).pptx
jovannyflex
 
PPTX
Lecture 5 – Computing with Numbers (Math Lib).pptx
jovannyflex
 

More Related Content

What's hot (19)

PDF
Evaluating functions
REYEMMANUELILUMBA
 
PDF
Array and pointer
ShinRongHuang
 
PPTX
2.3 slopes and difference quotient t
math260
 
PPT
Matdis 3.4
Claudia Primasiwi
 
PPS
Higher Order Procedures (in Ruby)
Nate Murray
 
PDF
Ch07 13
schibu20
 
PPTX
Evaluating a function
MartinGeraldine
 
PPT
Composition Of Functions
sjwong
 
DOCX
เซต
Khiaokha
 
PPT
Data Structure and Algorithms Graphs
ManishPrajapati78
 
PPTX
Prac 3 lagrange's thm
Himani Asija
 
PPT
Vector bits and pieces
Shaun Wilson
 
PPT
Function Operations
swartzje
 
PPT
Vectors intro
Shaun Wilson
 
PPT
Vector multiplication dot product
Shaun Wilson
 
PPT
1 5 graphs of functions
Alaina Wright
 
PPTX
Algebra 6 Point 1
herbison
 
PDF
7th PreAlg - L53--Jan31
jdurst65
 
PDF
Day 5 u8f13
jchartiersjsd
 
Evaluating functions
REYEMMANUELILUMBA
 
Array and pointer
ShinRongHuang
 
2.3 slopes and difference quotient t
math260
 
Matdis 3.4
Claudia Primasiwi
 
Higher Order Procedures (in Ruby)
Nate Murray
 
Ch07 13
schibu20
 
Evaluating a function
MartinGeraldine
 
Composition Of Functions
sjwong
 
เซต
Khiaokha
 
Data Structure and Algorithms Graphs
ManishPrajapati78
 
Prac 3 lagrange's thm
Himani Asija
 
Vector bits and pieces
Shaun Wilson
 
Function Operations
swartzje
 
Vectors intro
Shaun Wilson
 
Vector multiplication dot product
Shaun Wilson
 
1 5 graphs of functions
Alaina Wright
 
Algebra 6 Point 1
herbison
 
7th PreAlg - L53--Jan31
jdurst65
 
Day 5 u8f13
jchartiersjsd
 

Similar to Python PCEP Creating Simple Functions (20)

PDF
PyLecture4 -Python Basics2-
Yoshiki Satotani
 
PPTX
Lecture 5 – Computing with Numbers (Math Lib).pptx
jovannyflex
 
PPTX
Lecture 5 – Computing with Numbers (Math Lib).pptx
jovannyflex
 
PDF
Recursive Algorithms coded in Python.pdf
drthpeters
 
PDF
Math Modules (DRAFT)
NYCCTfab
 
PDF
Some hours of python
Things Lab
 
PDF
Python lambda functions with filter, map & reduce function
ARVIND PANDE
 
PPT
tutorial5.ppt
jvjfvvoa
 
DOCX
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
priestmanmable
 
PPTX
function_xii-BY APARNA DENDRE (1).pdf.pptx
g84017903
 
PDF
Lecture 5 numbers and built in functions
alvin567
 
DOCX
Python Math Concepts Book
Rohan Karunaratne
 
PPTX
Python Tidbits
Mitchell Vitez
 
PPTX
Python Lecture 4
Inzamam Baig
 
PPTX
Pytho-Chapter-4-Functions.pptx
RaviAr5
 
PDF
Class 7a: Functions
Marc Gouw
 
ODP
Python basics
Himanshu Awasthi
 
PDF
Talk Code
Agiliq Solutions
 
PPTX
Unit 2function in python.pptx
vishnupriyapm4
 
PyLecture4 -Python Basics2-
Yoshiki Satotani
 
Lecture 5 – Computing with Numbers (Math Lib).pptx
jovannyflex
 
Lecture 5 – Computing with Numbers (Math Lib).pptx
jovannyflex
 
Recursive Algorithms coded in Python.pdf
drthpeters
 
Math Modules (DRAFT)
NYCCTfab
 
Some hours of python
Things Lab
 
Python lambda functions with filter, map & reduce function
ARVIND PANDE
 
tutorial5.ppt
jvjfvvoa
 
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
priestmanmable
 
function_xii-BY APARNA DENDRE (1).pdf.pptx
g84017903
 
Lecture 5 numbers and built in functions
alvin567
 
Python Math Concepts Book
Rohan Karunaratne
 
Python Tidbits
Mitchell Vitez
 
Python Lecture 4
Inzamam Baig
 
Pytho-Chapter-4-Functions.pptx
RaviAr5
 
Class 7a: Functions
Marc Gouw
 
Python basics
Himanshu Awasthi
 
Talk Code
Agiliq Solutions
 
Unit 2function in python.pptx
vishnupriyapm4
 
Ad

More from IHTMINSTITUTE (19)

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

Recently uploaded (20)

PPTX
The ARUBA Kind of new Proposal Umum .pptx
andiwarneri
 
PPTX
BASICS OF SAP _ ALL ABOUT SAP _WHY SAP OVER ANY OTHER ERP SYSTEM
AhmadAli716831
 
PDF
Slides: Eco Economic Epochs for The World Game (s) pdf
Steven McGee
 
PPTX
ChatGPT_and_Its_Uses_Presentationss.pptx
Neha Prakash
 
PPTX
history of internet in nepal Class-8 (sparsha).pptx
SPARSH508080
 
PDF
ChatGPT A.I. Powered Chatbot and Popularization.pdf
StanleySamson1
 
PPTX
原版澳洲斯文本科技大学毕业证(SUT毕业证书)如何办理
taqyed
 
PPTX
最新版美国特拉华大学毕业证(UDel毕业证书)原版定制
taqyea
 
PDF
DDoS in India, presented at INNOG 8 by Dave Phelan
APNIC
 
PPTX
最新版加拿大奎斯特大学毕业证(QUC毕业证书)原版定制
taqyed
 
PPTX
Q1 English3 Week5 [email protected]
JenniferCawaling1
 
PPTX
BitRecover OST to PST Converter Software
antoniogosling01
 
PDF
Global Networking Trends, presented at the India ISP Conclave 2025
APNIC
 
PPTX
原版一样(ANU毕业证书)澳洲澳大利亚国立大学毕业证在线购买
Taqyea
 
PPTX
PROCESS FOR CREATION OF BUSINESS PARTNER IN SAP
AhmadAli716831
 
PDF
Logging and Automated Alerting Webinar.pdf
ControlCase
 
PPTX
Azure_Landing_Zone_Best_Practices_Visuals.pptx
fredsonbarbosa1
 
PPTX
IAREUOUSTPIDWHY$)CHARACTERARERWUEEJJSKWNSND
notgachabite123
 
PDF
B M Mostofa Kamal Al-Azad [Document & Localization Expert]
Mostofa Kamal Al-Azad
 
PDF
BroadLink Cloud Service introduction.pdf
DevendraDwivdi1
 
The ARUBA Kind of new Proposal Umum .pptx
andiwarneri
 
BASICS OF SAP _ ALL ABOUT SAP _WHY SAP OVER ANY OTHER ERP SYSTEM
AhmadAli716831
 
Slides: Eco Economic Epochs for The World Game (s) pdf
Steven McGee
 
ChatGPT_and_Its_Uses_Presentationss.pptx
Neha Prakash
 
history of internet in nepal Class-8 (sparsha).pptx
SPARSH508080
 
ChatGPT A.I. Powered Chatbot and Popularization.pdf
StanleySamson1
 
原版澳洲斯文本科技大学毕业证(SUT毕业证书)如何办理
taqyed
 
最新版美国特拉华大学毕业证(UDel毕业证书)原版定制
taqyea
 
DDoS in India, presented at INNOG 8 by Dave Phelan
APNIC
 
最新版加拿大奎斯特大学毕业证(QUC毕业证书)原版定制
taqyed
 
BitRecover OST to PST Converter Software
antoniogosling01
 
Global Networking Trends, presented at the India ISP Conclave 2025
APNIC
 
原版一样(ANU毕业证书)澳洲澳大利亚国立大学毕业证在线购买
Taqyea
 
PROCESS FOR CREATION OF BUSINESS PARTNER IN SAP
AhmadAli716831
 
Logging and Automated Alerting Webinar.pdf
ControlCase
 
Azure_Landing_Zone_Best_Practices_Visuals.pptx
fredsonbarbosa1
 
IAREUOUSTPIDWHY$)CHARACTERARERWUEEJJSKWNSND
notgachabite123
 
B M Mostofa Kamal Al-Azad [Document & Localization Expert]
Mostofa Kamal Al-Azad
 
BroadLink Cloud Service introduction.pdf
DevendraDwivdi1
 

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