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

What's hot

5.1 Linear Functions And Graphs
5.1 Linear Functions And Graphs5.1 Linear Functions And Graphs
5.1 Linear Functions And Graphsvmonacelli
 
Evaluating functions
Evaluating functionsEvaluating functions
Evaluating functions
REYEMMANUELILUMBA
 
Array and pointer
Array and pointerArray and pointer
Array and pointer
ShinRongHuang
 
2.3 slopes and difference quotient t
2.3 slopes and difference quotient t2.3 slopes and difference quotient t
2.3 slopes and difference quotient t
math260
 
Higher Order Procedures (in Ruby)
Higher Order Procedures (in Ruby)Higher Order Procedures (in Ruby)
Higher Order Procedures (in Ruby)
Nate Murray
 
Evaluating a function
Evaluating a functionEvaluating a function
Evaluating a function
MartinGeraldine
 
Composition Of Functions
Composition Of FunctionsComposition Of Functions
Composition Of Functionssjwong
 
Data Structure and Algorithms Graphs
Data Structure and Algorithms GraphsData Structure and Algorithms Graphs
Data Structure and Algorithms Graphs
ManishPrajapati78
 
133467 p3a3
133467 p3a3133467 p3a3
Prac 3 lagrange's thm
Prac  3 lagrange's thmPrac  3 lagrange's thm
Prac 3 lagrange's thm
Himani Asija
 
Vector bits and pieces
Vector bits and piecesVector bits and pieces
Vector bits and pieces
Shaun Wilson
 
Function Operations
Function OperationsFunction Operations
Function Operationsswartzje
 
Vectors intro
Vectors introVectors intro
Vectors intro
Shaun Wilson
 
Vector multiplication dot product
Vector multiplication   dot productVector multiplication   dot product
Vector multiplication dot product
Shaun Wilson
 
1 5 graphs of functions
1 5 graphs of functions1 5 graphs of functions
1 5 graphs of functions
Alaina Wright
 
Algebra 6 Point 1
Algebra 6 Point 1Algebra 6 Point 1
Algebra 6 Point 1herbison
 
7th PreAlg - L53--Jan31
7th PreAlg - L53--Jan317th PreAlg - L53--Jan31
7th PreAlg - L53--Jan31jdurst65
 

What's hot (20)

5.1 Linear Functions And Graphs
5.1 Linear Functions And Graphs5.1 Linear Functions And Graphs
5.1 Linear Functions And Graphs
 
Evaluating functions
Evaluating functionsEvaluating functions
Evaluating functions
 
Array and pointer
Array and pointerArray and pointer
Array and pointer
 
2.3 slopes and difference quotient t
2.3 slopes and difference quotient t2.3 slopes and difference quotient t
2.3 slopes and difference quotient t
 
Matdis 3.4
Matdis 3.4Matdis 3.4
Matdis 3.4
 
Higher Order Procedures (in Ruby)
Higher Order Procedures (in Ruby)Higher Order Procedures (in Ruby)
Higher Order Procedures (in Ruby)
 
Ch07 13
Ch07 13Ch07 13
Ch07 13
 
Evaluating a function
Evaluating a functionEvaluating a function
Evaluating a function
 
Composition Of Functions
Composition Of FunctionsComposition Of Functions
Composition Of Functions
 
เซต
เซตเซต
เซต
 
Data Structure and Algorithms Graphs
Data Structure and Algorithms GraphsData Structure and Algorithms Graphs
Data Structure and Algorithms Graphs
 
133467 p3a3
133467 p3a3133467 p3a3
133467 p3a3
 
Prac 3 lagrange's thm
Prac  3 lagrange's thmPrac  3 lagrange's thm
Prac 3 lagrange's thm
 
Vector bits and pieces
Vector bits and piecesVector bits and pieces
Vector bits and pieces
 
Function Operations
Function OperationsFunction Operations
Function Operations
 
Vectors intro
Vectors introVectors intro
Vectors intro
 
Vector multiplication dot product
Vector multiplication   dot productVector multiplication   dot product
Vector multiplication dot product
 
1 5 graphs of functions
1 5 graphs of functions1 5 graphs of functions
1 5 graphs of functions
 
Algebra 6 Point 1
Algebra 6 Point 1Algebra 6 Point 1
Algebra 6 Point 1
 
7th PreAlg - L53--Jan31
7th PreAlg - L53--Jan317th PreAlg - L53--Jan31
7th PreAlg - L53--Jan31
 

Similar to Python PCEP Creating Simple Functions

Class Customization and Better Code
Class Customization and Better CodeClass Customization and Better Code
Class Customization and Better CodeStronnics
 
Assignment3 solution 3rd_edition
Assignment3 solution 3rd_editionAssignment3 solution 3rd_edition
Assignment3 solution 3rd_edition
Mysore
 
14 recursion
14 recursion14 recursion
14 recursion
Himadri Sen Gupta
 
Vii ch 1 integers
Vii  ch 1 integersVii  ch 1 integers
Vii ch 1 integers
AmruthaKB2
 
COMPAPPABCA49085rFunrAP__Practical Number 9 & 10.docx
COMPAPPABCA49085rFunrAP__Practical Number 9 & 10.docxCOMPAPPABCA49085rFunrAP__Practical Number 9 & 10.docx
COMPAPPABCA49085rFunrAP__Practical Number 9 & 10.docx
TashiBhutia12
 
Running Free with the Monads
Running Free with the MonadsRunning Free with the Monads
Running Free with the Monads
kenbot
 
Data structures KTU chapter2.PPT
Data structures KTU chapter2.PPTData structures KTU chapter2.PPT
Data structures KTU chapter2.PPT
Albin562191
 
Python Puzzlers
Python PuzzlersPython Puzzlers
Python Puzzlers
Tendayi Mawushe
 
Python programming workshop session 3
Python programming workshop session 3Python programming workshop session 3
Python programming workshop session 3
Abdul Haseeb
 
Introduction to python programming
Introduction to python programmingIntroduction to python programming
Introduction to python programming
Rakotoarison Louis Frederick
 
Gilat_ch02.pdf
Gilat_ch02.pdfGilat_ch02.pdf
Gilat_ch02.pdf
ArhamQadeer
 
Module 1 linear functions
Module 1   linear functionsModule 1   linear functions
Module 1 linear functions
dionesioable
 
Functions in python
Functions in pythonFunctions in python
Functions in pythonIlian Iliev
 
NPTEL QUIZ.docx
NPTEL QUIZ.docxNPTEL QUIZ.docx
NPTEL QUIZ.docx
GEETHAR59
 
Python From Scratch (1).pdf
Python From Scratch  (1).pdfPython From Scratch  (1).pdf
Python From Scratch (1).pdf
NeerajChauhan697157
 
Gilat_ch03.pdf
Gilat_ch03.pdfGilat_ch03.pdf
Gilat_ch03.pdf
ArhamQadeer
 
Week 5
Week 5Week 5
Week 5
준성 조
 
Python Programming
Python Programming Python Programming
Python Programming
Sreedhar Chowdam
 
Math activities
Math activitiesMath activities
Math activitiesshandex
 

Similar to Python PCEP Creating Simple Functions (20)

Class Customization and Better Code
Class Customization and Better CodeClass Customization and Better Code
Class Customization and Better Code
 
Assignment3 solution 3rd_edition
Assignment3 solution 3rd_editionAssignment3 solution 3rd_edition
Assignment3 solution 3rd_edition
 
14 recursion
14 recursion14 recursion
14 recursion
 
Vii ch 1 integers
Vii  ch 1 integersVii  ch 1 integers
Vii ch 1 integers
 
COMPAPPABCA49085rFunrAP__Practical Number 9 & 10.docx
COMPAPPABCA49085rFunrAP__Practical Number 9 & 10.docxCOMPAPPABCA49085rFunrAP__Practical Number 9 & 10.docx
COMPAPPABCA49085rFunrAP__Practical Number 9 & 10.docx
 
Running Free with the Monads
Running Free with the MonadsRunning Free with the Monads
Running Free with the Monads
 
Data structures KTU chapter2.PPT
Data structures KTU chapter2.PPTData structures KTU chapter2.PPT
Data structures KTU chapter2.PPT
 
Python Puzzlers
Python PuzzlersPython Puzzlers
Python Puzzlers
 
Python programming workshop session 3
Python programming workshop session 3Python programming workshop session 3
Python programming workshop session 3
 
Introduction to python programming
Introduction to python programmingIntroduction to python programming
Introduction to python programming
 
Gilat_ch02.pdf
Gilat_ch02.pdfGilat_ch02.pdf
Gilat_ch02.pdf
 
Module 1 linear functions
Module 1   linear functionsModule 1   linear functions
Module 1 linear functions
 
Functions in python
Functions in pythonFunctions in python
Functions in python
 
NPTEL QUIZ.docx
NPTEL QUIZ.docxNPTEL QUIZ.docx
NPTEL QUIZ.docx
 
Python From Scratch (1).pdf
Python From Scratch  (1).pdfPython From Scratch  (1).pdf
Python From Scratch (1).pdf
 
Gilat_ch03.pdf
Gilat_ch03.pdfGilat_ch03.pdf
Gilat_ch03.pdf
 
Week 5
Week 5Week 5
Week 5
 
Chapter2
Chapter2Chapter2
Chapter2
 
Python Programming
Python Programming Python Programming
Python Programming
 
Math activities
Math activitiesMath activities
Math activities
 

More from IHTMINSTITUTE

Python PCEP Tuples and Dictionaries
Python PCEP Tuples and DictionariesPython PCEP Tuples and Dictionaries
Python PCEP Tuples and Dictionaries
IHTMINSTITUTE
 
Python PCEP Tuples and Dictionaries
Python PCEP Tuples and DictionariesPython PCEP Tuples and Dictionaries
Python PCEP Tuples and Dictionaries
IHTMINSTITUTE
 
Python PCEP Functions And Scopes
Python PCEP Functions And ScopesPython PCEP Functions And Scopes
Python PCEP Functions And Scopes
IHTMINSTITUTE
 
Python PCEP Function Parameters
Python PCEP Function ParametersPython PCEP Function Parameters
Python PCEP Function Parameters
IHTMINSTITUTE
 
Python PCEP Functions
Python PCEP FunctionsPython PCEP Functions
Python PCEP Functions
IHTMINSTITUTE
 
Python PCEP Multidemensional Arrays
Python PCEP Multidemensional ArraysPython PCEP Multidemensional Arrays
Python PCEP Multidemensional Arrays
IHTMINSTITUTE
 
Python PCEP Operations On Lists
Python PCEP Operations On ListsPython PCEP Operations On Lists
Python PCEP Operations On Lists
IHTMINSTITUTE
 
Python PCEP Sorting Simple Lists
Python PCEP Sorting Simple ListsPython PCEP Sorting Simple Lists
Python PCEP Sorting Simple Lists
IHTMINSTITUTE
 
Python PCEP Lists Collections of Data
Python PCEP Lists Collections of DataPython PCEP Lists Collections of Data
Python PCEP Lists Collections of Data
IHTMINSTITUTE
 
Python PCEP Logic Bit Operations
Python PCEP Logic Bit OperationsPython PCEP Logic Bit Operations
Python PCEP Logic Bit Operations
IHTMINSTITUTE
 
Python PCEP Loops
Python PCEP LoopsPython PCEP Loops
Python PCEP Loops
IHTMINSTITUTE
 
Python PCEP Comparison Operators And Conditional Execution
Python PCEP Comparison Operators And Conditional ExecutionPython PCEP Comparison Operators And Conditional Execution
Python PCEP Comparison Operators And Conditional Execution
IHTMINSTITUTE
 
Python PCEP How To Talk To Computer
Python PCEP How To Talk To ComputerPython PCEP How To Talk To Computer
Python PCEP How To Talk To Computer
IHTMINSTITUTE
 
Python PCEP Variables
Python PCEP VariablesPython PCEP Variables
Python PCEP Variables
IHTMINSTITUTE
 
Python PCEP Operators
Python PCEP OperatorsPython PCEP Operators
Python PCEP Operators
IHTMINSTITUTE
 
Python PCEP Literals
Python PCEP LiteralsPython PCEP Literals
Python PCEP Literals
IHTMINSTITUTE
 
IHTM Python PCEP Hello World
IHTM Python PCEP Hello WorldIHTM Python PCEP Hello World
IHTM Python PCEP Hello World
IHTMINSTITUTE
 
IHTM Python PCEP Introduction to Python
IHTM Python PCEP Introduction to PythonIHTM Python PCEP Introduction to Python
IHTM Python PCEP Introduction to Python
IHTMINSTITUTE
 
Python PCEP Welcome Opening
Python PCEP Welcome OpeningPython PCEP Welcome Opening
Python PCEP Welcome Opening
IHTMINSTITUTE
 

More from IHTMINSTITUTE (19)

Python PCEP Tuples and Dictionaries
Python PCEP Tuples and DictionariesPython PCEP Tuples and Dictionaries
Python PCEP Tuples and Dictionaries
 
Python PCEP Tuples and Dictionaries
Python PCEP Tuples and DictionariesPython PCEP Tuples and Dictionaries
Python PCEP Tuples and Dictionaries
 
Python PCEP Functions And Scopes
Python PCEP Functions And ScopesPython PCEP Functions And Scopes
Python PCEP Functions And Scopes
 
Python PCEP Function Parameters
Python PCEP Function ParametersPython PCEP Function Parameters
Python PCEP Function Parameters
 
Python PCEP Functions
Python PCEP FunctionsPython PCEP Functions
Python PCEP Functions
 
Python PCEP Multidemensional Arrays
Python PCEP Multidemensional ArraysPython PCEP Multidemensional Arrays
Python PCEP Multidemensional Arrays
 
Python PCEP Operations On Lists
Python PCEP Operations On ListsPython PCEP Operations On Lists
Python PCEP Operations On Lists
 
Python PCEP Sorting Simple Lists
Python PCEP Sorting Simple ListsPython PCEP Sorting Simple Lists
Python PCEP Sorting Simple Lists
 
Python PCEP Lists Collections of Data
Python PCEP Lists Collections of DataPython PCEP Lists Collections of Data
Python PCEP Lists Collections of Data
 
Python PCEP Logic Bit Operations
Python PCEP Logic Bit OperationsPython PCEP Logic Bit Operations
Python PCEP Logic Bit Operations
 
Python PCEP Loops
Python PCEP LoopsPython PCEP Loops
Python PCEP Loops
 
Python PCEP Comparison Operators And Conditional Execution
Python PCEP Comparison Operators And Conditional ExecutionPython PCEP Comparison Operators And Conditional Execution
Python PCEP Comparison Operators And Conditional Execution
 
Python PCEP How To Talk To Computer
Python PCEP How To Talk To ComputerPython PCEP How To Talk To Computer
Python PCEP How To Talk To Computer
 
Python PCEP Variables
Python PCEP VariablesPython PCEP Variables
Python PCEP Variables
 
Python PCEP Operators
Python PCEP OperatorsPython PCEP Operators
Python PCEP Operators
 
Python PCEP Literals
Python PCEP LiteralsPython PCEP Literals
Python PCEP Literals
 
IHTM Python PCEP Hello World
IHTM Python PCEP Hello WorldIHTM Python PCEP Hello World
IHTM Python PCEP Hello World
 
IHTM Python PCEP Introduction to Python
IHTM Python PCEP Introduction to PythonIHTM Python PCEP Introduction to Python
IHTM Python PCEP Introduction to Python
 
Python PCEP Welcome Opening
Python PCEP Welcome OpeningPython PCEP Welcome Opening
Python PCEP Welcome Opening
 

Recently uploaded

一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理
一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理
一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理
eutxy
 
test test test test testtest test testtest test testtest test testtest test ...
test test  test test testtest test testtest test testtest test testtest test ...test test  test test testtest test testtest test testtest test testtest test ...
test test test test testtest test testtest test testtest test testtest test ...
Arif0071
 
BASIC C++ lecture NOTE C++ lecture 3.pptx
BASIC C++ lecture NOTE C++ lecture 3.pptxBASIC C++ lecture NOTE C++ lecture 3.pptx
BASIC C++ lecture NOTE C++ lecture 3.pptx
natyesu
 
Comptia N+ Standard Networking lesson guide
Comptia N+ Standard Networking lesson guideComptia N+ Standard Networking lesson guide
Comptia N+ Standard Networking lesson guide
GTProductions1
 
This 7-second Brain Wave Ritual Attracts Money To You.!
This 7-second Brain Wave Ritual Attracts Money To You.!This 7-second Brain Wave Ritual Attracts Money To You.!
This 7-second Brain Wave Ritual Attracts Money To You.!
nirahealhty
 
History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shopHistory+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
laozhuseo02
 
一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理
一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理
一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理
keoku
 
Latest trends in computer networking.pptx
Latest trends in computer networking.pptxLatest trends in computer networking.pptx
Latest trends in computer networking.pptx
JungkooksNonexistent
 
JAVIER LASA-EXPERIENCIA digital 1986-2024.pdf
JAVIER LASA-EXPERIENCIA digital 1986-2024.pdfJAVIER LASA-EXPERIENCIA digital 1986-2024.pdf
JAVIER LASA-EXPERIENCIA digital 1986-2024.pdf
Javier Lasa
 
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
3ipehhoa
 
Multi-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Multi-cluster Kubernetes Networking- Patterns, Projects and GuidelinesMulti-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Multi-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Sanjeev Rampal
 
Internet-Security-Safeguarding-Your-Digital-World (1).pptx
Internet-Security-Safeguarding-Your-Digital-World (1).pptxInternet-Security-Safeguarding-Your-Digital-World (1).pptx
Internet-Security-Safeguarding-Your-Digital-World (1).pptx
VivekSinghShekhawat2
 
The+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptxThe+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptx
laozhuseo02
 
guildmasters guide to ravnica Dungeons & Dragons 5...
guildmasters guide to ravnica Dungeons & Dragons 5...guildmasters guide to ravnica Dungeons & Dragons 5...
guildmasters guide to ravnica Dungeons & Dragons 5...
Rogerio Filho
 
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptxBridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
Brad Spiegel Macon GA
 
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
3ipehhoa
 
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
ufdana
 
1.Wireless Communication System_Wireless communication is a broad term that i...
1.Wireless Communication System_Wireless communication is a broad term that i...1.Wireless Communication System_Wireless communication is a broad term that i...
1.Wireless Communication System_Wireless communication is a broad term that i...
JeyaPerumal1
 
How to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptxHow to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptx
Gal Baras
 
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
APNIC
 

Recently uploaded (20)

一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理
一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理
一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理
 
test test test test testtest test testtest test testtest test testtest test ...
test test  test test testtest test testtest test testtest test testtest test ...test test  test test testtest test testtest test testtest test testtest test ...
test test test test testtest test testtest test testtest test testtest test ...
 
BASIC C++ lecture NOTE C++ lecture 3.pptx
BASIC C++ lecture NOTE C++ lecture 3.pptxBASIC C++ lecture NOTE C++ lecture 3.pptx
BASIC C++ lecture NOTE C++ lecture 3.pptx
 
Comptia N+ Standard Networking lesson guide
Comptia N+ Standard Networking lesson guideComptia N+ Standard Networking lesson guide
Comptia N+ Standard Networking lesson guide
 
This 7-second Brain Wave Ritual Attracts Money To You.!
This 7-second Brain Wave Ritual Attracts Money To You.!This 7-second Brain Wave Ritual Attracts Money To You.!
This 7-second Brain Wave Ritual Attracts Money To You.!
 
History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shopHistory+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
 
一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理
一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理
一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理
 
Latest trends in computer networking.pptx
Latest trends in computer networking.pptxLatest trends in computer networking.pptx
Latest trends in computer networking.pptx
 
JAVIER LASA-EXPERIENCIA digital 1986-2024.pdf
JAVIER LASA-EXPERIENCIA digital 1986-2024.pdfJAVIER LASA-EXPERIENCIA digital 1986-2024.pdf
JAVIER LASA-EXPERIENCIA digital 1986-2024.pdf
 
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
 
Multi-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Multi-cluster Kubernetes Networking- Patterns, Projects and GuidelinesMulti-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Multi-cluster Kubernetes Networking- Patterns, Projects and Guidelines
 
Internet-Security-Safeguarding-Your-Digital-World (1).pptx
Internet-Security-Safeguarding-Your-Digital-World (1).pptxInternet-Security-Safeguarding-Your-Digital-World (1).pptx
Internet-Security-Safeguarding-Your-Digital-World (1).pptx
 
The+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptxThe+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptx
 
guildmasters guide to ravnica Dungeons & Dragons 5...
guildmasters guide to ravnica Dungeons & Dragons 5...guildmasters guide to ravnica Dungeons & Dragons 5...
guildmasters guide to ravnica Dungeons & Dragons 5...
 
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptxBridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
 
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
 
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
 
1.Wireless Communication System_Wireless communication is a broad term that i...
1.Wireless Communication System_Wireless communication is a broad term that i...1.Wireless Communication System_Wireless communication is a broad term that i...
1.Wireless Communication System_Wireless communication is a broad term that i...
 
How to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptxHow to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptx
 
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
 

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