650 likes | 1.92k Views
** Python Certification Training: https://www.edureka.co/python ** <br>This Edureka PPT on Python Functions tutorial covers all the important aspects of functions in Python right from the introduction to what functions are, all the way till checking out the major functions and using the code-first approach to understand them better. <br><br>Agenda <br>Why use Functions? <br>What are the Functions? <br>Types of Python Functions <br>Built-in Functions in Python <br>User-defined Functions in Python <br>Python Lambda Function <br>Conclusion <br><br>Python Tutorial Playlist: https://goo.gl/WsBpKe <br>Blog Series: http://bit.ly/2sqmP4s <br><br>Follow us to never miss an update in the future. <br><br>Instagram: https://www.instagram.com/edureka_learning/ <br>Facebook: https://www.facebook.com/edurekaIN/ <br>Twitter: https://twitter.com/edurekain <br>LinkedIn: https://www.linkedin.com/company/edureka
E N D
Python Functions Agenda Python Certification Training https://www.edureka.co/python
Python Functions Agenda Python Certification Training https://www.edureka.co/python
Agenda 01 Introduction Why use Functions? 02 Getting Started What are functions? 03 Concepts Types of functions 04 Practical Approach Looking at code to understand theory Python Certification Training https://www.edureka.co/python
Python Functions Why Use Functions Python Certification Training https://www.edureka.co/python
Why Use Functions? You write a program in which Celsius must be converted to Fahrenheit multiple times Reuse: #collect input from user celsius = float(input(“Enter Celsius value: ")) #calculate value in Fahrenheit Fahrenheit = (celsius*1.8) + 32 print(“Fahrenheit value is “,fahrenheit) Fahrenheit = (celsius*1.8) + 32 print(“Fahrenheit value is “,fahrenheit) Fahrenheit = (celsius*1.8) + 32 print(“Fahrenheit value is “,fahrenheit) Fahrenheit = (celsius*1.8) + 32 print(“Fahrenheit value is “,fahrenheit) Fahrenheit = (celsius*1.8) + 32 print(“Fahrenheit value is “,fahrenheit) #collect input from user celsius = float(input(“Enter Celsius value: ")) #calculate value in Fahrenheit Fahrenheit = (celsius*1.8) + 32 print(“Fahrenheit value is “,fahrenheit) Program to calculate Fahrenheit #collect input from user celsius = float(input(“Enter Celsius value: ")) #calculate value in Fahrenheit value: ")) #calculate value in Fahrenheit value: ")) #calculate value in Fahrenheit value: ")) #calculate value in Fahrenheit #collect input from user celsius = float(input(“Enter Celsius #collect input from user celsius = float(input(“Enter Celsius #collect input from user celsius = float(input(“Enter Celsius You wouldn’t want to repeat those same lines of code every time a value needed conversion Fahrenheit = (9/5)Celsius + 32 Logic to calculate Fahrenheit Python Certification Training https://www.edureka.co/python
Why Use Functions? DRY –Don’t Repeat Yourself Functions are reusable tasks Flip Channels Adjust Volume Functions reduce lines of code in your main program by letting you avail predefined features multiple times without having to repeat its set of codes again. Python Certification Training https://www.edureka.co/python
Python Functions What are Functions? Python Certification Training https://www.edureka.co/python
What Are Functions? Functions provide a way to break problems or processes down into smaller and independent blocks of code. A function is a block of organized, reusable code that is used to perform some task. • It is usually called by its name when its task needs execution. • You can also pass values to it or have it return results to you. Defkeyword: ‘def’ keyword before its name. And its name is to be followed by parentheses, before a colon(:). def function_name(): “””This function does nothing.””” pass Functions are tasks that one wants to perform. Python Certification Training https://www.edureka.co/python
Docstring The first string after the function header is called the docstring and is short for documentation string. Example >>> print(greet.__doc__) This function greets to the person passed into the name parameter Remember this! Python Certification Training https://www.edureka.co/python
Python Functions Types of Functions Python Certification Training https://www.edureka.co/python
Functions Functions Built-in functions User defined functions Python Certification Training https://www.edureka.co/python
Python Functions Built-in Functions in Python Python Certification Training https://www.edureka.co/python
abs() function Definition The abs() function returns the absolute value of the specified number. Syntax abs(n) Example x = abs(3+5j) C:\Users\My Name>python demo_abs_complex.py 5.830951894845301 Python Certification Training https://www.edureka.co/python
all() function The all() function returns True if all items in an iterableare true, otherwise it returns False. Definition Syntax all(iterable) mylist = [True, True, True] x = all(mylist) Example Same for lists, tuples and dictionaries as well! C:\Users\My Name>python demo_all.py True Python Certification Training https://www.edureka.co/python
ascii() function The ascii() function returns a readable version of any object (Strings, Tuples, Lists, etc). Definition Syntax ascii(object) Example x = ascii("My name is Ståle") C:\Users\My Name>python demo_ascii.py 'My name is St\e5le' Python Certification Training https://www.edureka.co/python
bool() function Definition The bool() function returns the booleanvalue of a specified object. Syntax bool(object) Example x = bool(1) C:\Users\My Name>python demo_bool.py True Python Certification Training https://www.edureka.co/python
enumerate() function The enumerate() function takes a collection (e.g. a tuple) and returns it as an enumerate object. Definition Syntax enumerate(iterable, start) Example x = ('apple', 'banana', 'cherry') y = enumerate(x) C:\Users\My Name>python demo_enumerate.py [(0, 'apple'), (1, 'banana'), (2, 'cherry')] Python Certification Training https://www.edureka.co/python
format() function Definition The format() function formats a specified value into a specified format. Syntax format(value, format) Example x = format(0.5, '%') C:\Users\My Name>python demo_format.py 50.000000% Python Certification Training https://www.edureka.co/python
getattr() function The getattr() function returns the value of the specified attribute from the specified object. Definition Syntax getattr(object, attribute, default) class Person: name = "John" age = 36 country = "Norway" Example x = getattr(Person, 'age') C:\Users\My Name>python demo_getattr.py 36 Python Certification Training https://www.edureka.co/python
id() function Definition The id() function returns a unique id for the specified object. Syntax id(object) x = ('apple', 'banana', 'cherry') y = id(x) Example C:\Users\My Name>python demo_id.py 56450738 Python Certification Training https://www.edureka.co/python
len() function Definition The len() function returns the number of items in an object. Syntax len(object) mylist = "Hello" x = len(mylist) Example C:\Users\My Name>python demo_len2.py 5 Python Certification Training https://www.edureka.co/python
map() function The map() function executes a specified function for each item in a iterable. The item is sent to the function as a parameter. Definition Syntax map(function, iterables) def myfunc(n): return len(n) Example x = map(myfunc, ('apple', 'banana’, 'cherry')) C:\Users\My Name>python demo_map.py <map object at 0x056D44F0> ['5', '6', '6'] Python Certification Training https://www.edureka.co/python
min() function The min() function returns the item with the lowest value, or the item with the lowest value in an iterable. Definition Syntax min(n1, n2, n3, ...) Example x = min(5, 10) C:\Users\My Name>python demo_min.py 5 Python Certification Training https://www.edureka.co/python
pow() function Definition The pow() function returns the value of x to the power of y (x^y). Syntax pow(x, y, z) Example x = pow(4, 3) C:\Users\My Name>python demo_pow.py 64 Python Certification Training https://www.edureka.co/python
print() function The print() function prints the specified message to the screen, or other standard output device. Definition Syntax print(object(s), separator=separator, end=end, file=file, flush=flush) Example print("Hello World") C:\Users\My Name>python demo_print.py Hello World Python Certification Training https://www.edureka.co/python
setattr() function The setattr() function sets the value of the specified attribute of the specified object. Definition Syntax setattr(object, attribute, value) class Person: name = "John" age = 36 country = "Norway" Example setattr(Person, 'age', 40) C:\Users\My Name>python demo_setattr.py 40 Python Certification Training https://www.edureka.co/python
sorted() function The sorted() function returns a sorted list of the specified iterable object. Definition Syntax sorted(iterable, key=key, reverse=reverse) a = ("b", "g", "a", "d", "f", "c", "h", "e") x = sorted(a) print(x) Example C:\Users\My Name>python demo_sorted.py ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'] Python Certification Training https://www.edureka.co/python
type() function Definition The type() function returns the type of the specified object Syntax type(object, bases, dict) a = ('apple', 'banana', 'cherry') b = "Hello World" c = 33 Example x = type(a) y = type(b) z = type(c) C:\Users\My Name>python demo_type.py <class 'tuple'> <class 'str'> <class 'int'> Python Certification Training https://www.edureka.co/python
Python Functions User Defined Functions in Python Python Certification Training https://www.edureka.co/python
User-Defined Functions In Python Code first approach, let’s begin Creating a function Calling a function def my_function(): print("Hello from a function") def my_function(): print("Hello from a function") my_function() Python Certification Training https://www.edureka.co/python
Parameters Information passed to functions Example C:\Users\My Name>python demo_function_param.py Emil Refsnes Tobias Refsnes Linus Refsnes def my_function(fname): print(fname + " Refsnes") my_function("Emil") my_function("Tobias") my_function("Linus") Python Certification Training https://www.edureka.co/python
Parameters Default parameter value Example C:\Users\My Name>python demo_function_param2.py I am from Sweden I am from India I am from Norway I am from Brazil def my_function(country = "Norway"): print("I am from " + country) my_function("Sweden") my_function("India") my_function() my_function("Brazil") Python Certification Training https://www.edureka.co/python
Parameters Return values Example C:\Users\My Name>python demo_function_return.py 15 25 45 def my_function(x): return 5 * x print(my_function(3)) print(my_function(5)) print(my_function(9)) Python Certification Training https://www.edureka.co/python
Parameters Recursion Function Example C:\Users\My Name>python demo_recursion.py def tri_recursion(k): if(k>0): result = k+tri_recursion(k-1) print(result) else: result = 0 return result Recursion Example Results 1 3 6 10 15 21 print("\n\nRecursion Example Results") tri_recursion(6) Python Certification Training https://www.edureka.co/python
Python Functions Python Lambda Function Python Certification Training https://www.edureka.co/python
Lambda Function A lambda function is a small anonymous function. It can take any number of arguments, but can only have one expression. What is Lambda? Syntax lambda arguments : expression C:\Users\My Name>python demo_lambda.py 15 Example x = lambda a : a + 10 print(x(5)) Python Certification Training https://www.edureka.co/python
Lambda Function A lambda function that multiplies argument a with argument b and print the result: C:\Users\My Name>python demo_lambda2.py 30 x = lambda a, b : a * b print(x(5, 6)) A lambda function that sums argument a, b, and c and print the result: C:\Users\My Name>python demo_lambda3.py 13 x = lambda a, b, c : a + b + c print(x(5, 6, 2)) Python Certification Training https://www.edureka.co/python
Why Use Lambda Function? The power of lambda is better shown when you use them as an anonymous function inside another function. def myfunc(n): return lambda a : a * n Use that function definition to make a function that always doubles the number you send in: C:\Users\My Name>python demo_lambda_double.py 22 def myfunc(n): return lambda a : a * n mydoubler = myfunc(2) print(mydoubler(11)) Python Certification Training https://www.edureka.co/python
Why Use Lambda Function? Or, use the same function definition to make a function that always triples the number you send in: C:\Users\My Name>python demo_lambda_both.py 22 33 def myfunc(n): return lambda a : a * n mydoubler = myfunc(2) mytripler = myfunc(3) print(mydoubler(11)) print(mytripler(11)) Python Certification Training https://www.edureka.co/python
Python Functions Conclusion Python Certification Training https://www.edureka.co/python
Conclusion Python Functions, yay! Python Certification Training https://www.edureka.co/python