SlideShare a Scribd company logo
1 of 10
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 functionsREYEMMANUELILUMBA
ย 
Array and pointer
Array and pointerArray and pointer
Array and pointerShinRongHuang
ย 
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 tmath260
ย 
Higher Order Procedures (in Ruby)
Higher Order Procedures (in Ruby)Higher Order Procedures (in Ruby)
Higher Order Procedures (in Ruby)Nate Murray
ย 
Ch07 13
Ch07 13Ch07 13
Ch07 13schibu20
ย 
Evaluating a function
Evaluating a functionEvaluating a function
Evaluating a functionMartinGeraldine
ย 
Composition Of Functions
Composition Of FunctionsComposition Of Functions
Composition Of Functionssjwong
ย 
เน€เธ‹เธ•
เน€เธ‹เธ•เน€เธ‹เธ•
เน€เธ‹เธ•Khiaokha
ย 
Data Structure and Algorithms Graphs
Data Structure and Algorithms GraphsData Structure and Algorithms Graphs
Data Structure and Algorithms GraphsManishPrajapati78
ย 
Prac 3 lagrange's thm
Prac  3 lagrange's thmPrac  3 lagrange's thm
Prac 3 lagrange's thmHimani Asija
ย 
Vector bits and pieces
Vector bits and piecesVector bits and pieces
Vector bits and piecesShaun Wilson
ย 
Function Operations
Function OperationsFunction Operations
Function Operationsswartzje
ย 
Vectors intro
Vectors introVectors intro
Vectors introShaun Wilson
ย 
Vector multiplication dot product
Vector multiplication   dot productVector multiplication   dot product
Vector multiplication dot productShaun Wilson
ย 
1 5 graphs of functions
1 5 graphs of functions1 5 graphs of functions
1 5 graphs of functionsAlaina 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_editionMysore
ย 
Vii ch 1 integers
Vii  ch 1 integersVii  ch 1 integers
Vii ch 1 integersAmruthaKB2
ย 
COMPAPPABCA49085rFunrAP__Practical Number 9 & 10.docx
COMPAPPABCA49085rFunrAP__Practical Number 9 & 10.docxCOMPAPPABCA49085rFunrAP__Practical Number 9 & 10.docx
COMPAPPABCA49085rFunrAP__Practical Number 9 & 10.docxTashiBhutia12
ย 
Running Free with the Monads
Running Free with the MonadsRunning Free with the Monads
Running Free with the Monadskenbot
ย 
Data structures KTU chapter2.PPT
Data structures KTU chapter2.PPTData structures KTU chapter2.PPT
Data structures KTU chapter2.PPTAlbin562191
ย 
Python Puzzlers
Python PuzzlersPython Puzzlers
Python PuzzlersTendayi Mawushe
ย 
Python programming workshop session 3
Python programming workshop session 3Python programming workshop session 3
Python programming workshop session 3Abdul Haseeb
ย 
Gilat_ch02.pdf
Gilat_ch02.pdfGilat_ch02.pdf
Gilat_ch02.pdfArhamQadeer
ย 
Module 1 linear functions
Module 1   linear functionsModule 1   linear functions
Module 1 linear functionsdionesioable
ย 
Functions in python
Functions in pythonFunctions in python
Functions in pythonIlian Iliev
ย 
NPTEL QUIZ.docx
NPTEL QUIZ.docxNPTEL QUIZ.docx
NPTEL QUIZ.docxGEETHAR59
ย 
Python From Scratch (1).pdf
Python From Scratch  (1).pdfPython From Scratch  (1).pdf
Python From Scratch (1).pdfNeerajChauhan697157
ย 
Gilat_ch03.pdf
Gilat_ch03.pdfGilat_ch03.pdf
Gilat_ch03.pdfArhamQadeer
ย 
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 DictionariesIHTMINSTITUTE
ย 
Python PCEP Tuples and Dictionaries
Python PCEP Tuples and DictionariesPython PCEP Tuples and Dictionaries
Python PCEP Tuples and DictionariesIHTMINSTITUTE
ย 
Python PCEP Functions And Scopes
Python PCEP Functions And ScopesPython PCEP Functions And Scopes
Python PCEP Functions And ScopesIHTMINSTITUTE
ย 
Python PCEP Function Parameters
Python PCEP Function ParametersPython PCEP Function Parameters
Python PCEP Function ParametersIHTMINSTITUTE
ย 
Python PCEP Functions
Python PCEP FunctionsPython PCEP Functions
Python PCEP FunctionsIHTMINSTITUTE
ย 
Python PCEP Multidemensional Arrays
Python PCEP Multidemensional ArraysPython PCEP Multidemensional Arrays
Python PCEP Multidemensional ArraysIHTMINSTITUTE
ย 
Python PCEP Operations On Lists
Python PCEP Operations On ListsPython PCEP Operations On Lists
Python PCEP Operations On ListsIHTMINSTITUTE
ย 
Python PCEP Sorting Simple Lists
Python PCEP Sorting Simple ListsPython PCEP Sorting Simple Lists
Python PCEP Sorting Simple ListsIHTMINSTITUTE
ย 
Python PCEP Lists Collections of Data
Python PCEP Lists Collections of DataPython PCEP Lists Collections of Data
Python PCEP Lists Collections of DataIHTMINSTITUTE
ย 
Python PCEP Logic Bit Operations
Python PCEP Logic Bit OperationsPython PCEP Logic Bit Operations
Python PCEP Logic Bit OperationsIHTMINSTITUTE
ย 
Python PCEP Loops
Python PCEP LoopsPython PCEP Loops
Python PCEP LoopsIHTMINSTITUTE
ย 
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 ExecutionIHTMINSTITUTE
ย 
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 ComputerIHTMINSTITUTE
ย 
Python PCEP Variables
Python PCEP VariablesPython PCEP Variables
Python PCEP VariablesIHTMINSTITUTE
ย 
Python PCEP Operators
Python PCEP OperatorsPython PCEP Operators
Python PCEP OperatorsIHTMINSTITUTE
ย 
Python PCEP Literals
Python PCEP LiteralsPython PCEP Literals
Python PCEP LiteralsIHTMINSTITUTE
ย 
IHTM Python PCEP Hello World
IHTM Python PCEP Hello WorldIHTM Python PCEP Hello World
IHTM Python PCEP Hello WorldIHTMINSTITUTE
ย 
IHTM Python PCEP Introduction to Python
IHTM Python PCEP Introduction to PythonIHTM Python PCEP Introduction to Python
IHTM Python PCEP Introduction to PythonIHTMINSTITUTE
ย 
Python PCEP Welcome Opening
Python PCEP Welcome OpeningPython PCEP Welcome Opening
Python PCEP Welcome OpeningIHTMINSTITUTE
ย 

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

Real Men Wear Diapers T Shirts sweatshirt
Real Men Wear Diapers T Shirts sweatshirtReal Men Wear Diapers T Shirts sweatshirt
Real Men Wear Diapers T Shirts sweatshirtrahman018755
ย 
VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...
VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...
VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...SUHANI PANDEY
ย 
Dubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls Dubai
Dubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls DubaiDubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls Dubai
Dubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls Dubaikojalkojal131
ย 
20240509 QFM015 Engineering Leadership Reading List April 2024.pdf
20240509 QFM015 Engineering Leadership Reading List April 2024.pdf20240509 QFM015 Engineering Leadership Reading List April 2024.pdf
20240509 QFM015 Engineering Leadership Reading List April 2024.pdfMatthew Sinclair
ย 
VIP Model Call Girls NIBM ( Pune ) Call ON 8005736733 Starting From 5K to 25K...
VIP Model Call Girls NIBM ( Pune ) Call ON 8005736733 Starting From 5K to 25K...VIP Model Call Girls NIBM ( Pune ) Call ON 8005736733 Starting From 5K to 25K...
VIP Model Call Girls NIBM ( Pune ) Call ON 8005736733 Starting From 5K to 25K...SUHANI PANDEY
ย 
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service AvailableCall Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service AvailableSeo
ย 
Pune Airport ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready...
Pune Airport ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready...Pune Airport ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready...
Pune Airport ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready...tanu pandey
ย 
(+971568250507 ))# Young Call Girls in Ajman By Pakistani Call Girls in ...
(+971568250507  ))#  Young Call Girls  in Ajman  By Pakistani Call Girls  in ...(+971568250507  ))#  Young Call Girls  in Ajman  By Pakistani Call Girls  in ...
(+971568250507 ))# Young Call Girls in Ajman By Pakistani Call Girls in ...Escorts Call Girls
ย 
Wagholi & High Class Call Girls Pune Neha 8005736733 | 100% Gennuine High Cla...
Wagholi & High Class Call Girls Pune Neha 8005736733 | 100% Gennuine High Cla...Wagholi & High Class Call Girls Pune Neha 8005736733 | 100% Gennuine High Cla...
Wagholi & High Class Call Girls Pune Neha 8005736733 | 100% Gennuine High Cla...SUHANI PANDEY
ย 
WhatsApp ๐Ÿ“ž 8448380779 โœ…Call Girls In Mamura Sector 66 ( Noida)
WhatsApp ๐Ÿ“ž 8448380779 โœ…Call Girls In Mamura Sector 66 ( Noida)WhatsApp ๐Ÿ“ž 8448380779 โœ…Call Girls In Mamura Sector 66 ( Noida)
WhatsApp ๐Ÿ“ž 8448380779 โœ…Call Girls In Mamura Sector 66 ( Noida)Delhi Call girls
ย 
VIP Call Girls Pollachi 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Pollachi 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Pollachi 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Pollachi 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
ย 
Real Escorts in Al Nahda +971524965298 Dubai Escorts Service
Real Escorts in Al Nahda +971524965298 Dubai Escorts ServiceReal Escorts in Al Nahda +971524965298 Dubai Escorts Service
Real Escorts in Al Nahda +971524965298 Dubai Escorts ServiceEscorts Call Girls
ย 
( Pune ) VIP Baner Call Girls ๐ŸŽ—๏ธ 9352988975 Sizzling | Escorts | Girls Are Re...
( Pune ) VIP Baner Call Girls ๐ŸŽ—๏ธ 9352988975 Sizzling | Escorts | Girls Are Re...( Pune ) VIP Baner Call Girls ๐ŸŽ—๏ธ 9352988975 Sizzling | Escorts | Girls Are Re...
( Pune ) VIP Baner Call Girls ๐ŸŽ—๏ธ 9352988975 Sizzling | Escorts | Girls Are Re...nilamkumrai
ย 
Nanded City ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready ...
Nanded City ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready ...Nanded City ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready ...
Nanded City ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready ...tanu pandey
ย 
Sarola * Female Escorts Service in Pune | 8005736733 Independent Escorts & Da...
Sarola * Female Escorts Service in Pune | 8005736733 Independent Escorts & Da...Sarola * Female Escorts Service in Pune | 8005736733 Independent Escorts & Da...
Sarola * Female Escorts Service in Pune | 8005736733 Independent Escorts & Da...SUHANI PANDEY
ย 
"Boost Your Digital Presence: Partner with a Leading SEO Agency"
"Boost Your Digital Presence: Partner with a Leading SEO Agency""Boost Your Digital Presence: Partner with a Leading SEO Agency"
"Boost Your Digital Presence: Partner with a Leading SEO Agency"growthgrids
ย 

Recently uploaded (20)

Russian Call Girls in %(+971524965298 )# Call Girls in Dubai
Russian Call Girls in %(+971524965298  )#  Call Girls in DubaiRussian Call Girls in %(+971524965298  )#  Call Girls in Dubai
Russian Call Girls in %(+971524965298 )# Call Girls in Dubai
ย 
Real Men Wear Diapers T Shirts sweatshirt
Real Men Wear Diapers T Shirts sweatshirtReal Men Wear Diapers T Shirts sweatshirt
Real Men Wear Diapers T Shirts sweatshirt
ย 
VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...
VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...
VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...
ย 
Low Sexy Call Girls In Mohali 9053900678 ๐ŸฅตHave Save And Good Place ๐Ÿฅต
Low Sexy Call Girls In Mohali 9053900678 ๐ŸฅตHave Save And Good Place ๐ŸฅตLow Sexy Call Girls In Mohali 9053900678 ๐ŸฅตHave Save And Good Place ๐Ÿฅต
Low Sexy Call Girls In Mohali 9053900678 ๐ŸฅตHave Save And Good Place ๐Ÿฅต
ย 
Dubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls Dubai
Dubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls DubaiDubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls Dubai
Dubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls Dubai
ย 
20240509 QFM015 Engineering Leadership Reading List April 2024.pdf
20240509 QFM015 Engineering Leadership Reading List April 2024.pdf20240509 QFM015 Engineering Leadership Reading List April 2024.pdf
20240509 QFM015 Engineering Leadership Reading List April 2024.pdf
ย 
VIP Model Call Girls NIBM ( Pune ) Call ON 8005736733 Starting From 5K to 25K...
VIP Model Call Girls NIBM ( Pune ) Call ON 8005736733 Starting From 5K to 25K...VIP Model Call Girls NIBM ( Pune ) Call ON 8005736733 Starting From 5K to 25K...
VIP Model Call Girls NIBM ( Pune ) Call ON 8005736733 Starting From 5K to 25K...
ย 
(INDIRA) Call Girl Pune Call Now 8250077686 Pune Escorts 24x7
(INDIRA) Call Girl Pune Call Now 8250077686 Pune Escorts 24x7(INDIRA) Call Girl Pune Call Now 8250077686 Pune Escorts 24x7
(INDIRA) Call Girl Pune Call Now 8250077686 Pune Escorts 24x7
ย 
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service AvailableCall Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
ย 
Pune Airport ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready...
Pune Airport ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready...Pune Airport ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready...
Pune Airport ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready...
ย 
valsad Escorts Service โ˜Ž๏ธ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service โ˜Ž๏ธ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...valsad Escorts Service โ˜Ž๏ธ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service โ˜Ž๏ธ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
ย 
(+971568250507 ))# Young Call Girls in Ajman By Pakistani Call Girls in ...
(+971568250507  ))#  Young Call Girls  in Ajman  By Pakistani Call Girls  in ...(+971568250507  ))#  Young Call Girls  in Ajman  By Pakistani Call Girls  in ...
(+971568250507 ))# Young Call Girls in Ajman By Pakistani Call Girls in ...
ย 
Wagholi & High Class Call Girls Pune Neha 8005736733 | 100% Gennuine High Cla...
Wagholi & High Class Call Girls Pune Neha 8005736733 | 100% Gennuine High Cla...Wagholi & High Class Call Girls Pune Neha 8005736733 | 100% Gennuine High Cla...
Wagholi & High Class Call Girls Pune Neha 8005736733 | 100% Gennuine High Cla...
ย 
WhatsApp ๐Ÿ“ž 8448380779 โœ…Call Girls In Mamura Sector 66 ( Noida)
WhatsApp ๐Ÿ“ž 8448380779 โœ…Call Girls In Mamura Sector 66 ( Noida)WhatsApp ๐Ÿ“ž 8448380779 โœ…Call Girls In Mamura Sector 66 ( Noida)
WhatsApp ๐Ÿ“ž 8448380779 โœ…Call Girls In Mamura Sector 66 ( Noida)
ย 
VIP Call Girls Pollachi 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Pollachi 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Pollachi 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Pollachi 7001035870 Whatsapp Number, 24/07 Booking
ย 
Real Escorts in Al Nahda +971524965298 Dubai Escorts Service
Real Escorts in Al Nahda +971524965298 Dubai Escorts ServiceReal Escorts in Al Nahda +971524965298 Dubai Escorts Service
Real Escorts in Al Nahda +971524965298 Dubai Escorts Service
ย 
( Pune ) VIP Baner Call Girls ๐ŸŽ—๏ธ 9352988975 Sizzling | Escorts | Girls Are Re...
( Pune ) VIP Baner Call Girls ๐ŸŽ—๏ธ 9352988975 Sizzling | Escorts | Girls Are Re...( Pune ) VIP Baner Call Girls ๐ŸŽ—๏ธ 9352988975 Sizzling | Escorts | Girls Are Re...
( Pune ) VIP Baner Call Girls ๐ŸŽ—๏ธ 9352988975 Sizzling | Escorts | Girls Are Re...
ย 
Nanded City ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready ...
Nanded City ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready ...Nanded City ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready ...
Nanded City ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready ...
ย 
Sarola * Female Escorts Service in Pune | 8005736733 Independent Escorts & Da...
Sarola * Female Escorts Service in Pune | 8005736733 Independent Escorts & Da...Sarola * Female Escorts Service in Pune | 8005736733 Independent Escorts & Da...
Sarola * Female Escorts Service in Pune | 8005736733 Independent Escorts & Da...
ย 
"Boost Your Digital Presence: Partner with a Leading SEO Agency"
"Boost Your Digital Presence: Partner with a Leading SEO Agency""Boost Your Digital Presence: Partner with a Leading SEO Agency"
"Boost Your Digital Presence: Partner with a Leading SEO Agency"
ย 

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