1 / 18

PYTHON FUNCTIONS

Python has a set of built-in methods that you can use on strings. All string methods return new values. They do not change the original string. In this tutorial, we have discussed numerous methods of strings such as index(), strip(), and so on with the examples.tt<br>For more such informative content about python, trending data science courses, online data science courses, A.I and M.I course training etc visit: http://bit.ly/DatascienceCourse<br>Follow us for more.

Download Presentation

PYTHON FUNCTIONS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. FUNCTIONS IN PYTHON

  2. CONCEPT OF FUNCTION IN PYTHON A function is a series of statements that take inputs, perform some particular computation, and generate output.  The idea is to bring together some frequently or repeatedly performed task and create a function, so  that instead of writing the same code again and again for multiple levels, we can call the function.  Python offers built-in functions like print() and so  on but we can also build your own functions.  These functions are known as user-defined functions. Note: Function definition is not function execution .To execute you need to all a function.

  3. Syntax of function • A parameter is the variable listed inside the parentheses in the function definition .An argument is the value that is sent to the function when it is called.

  4. Rules to define a function • Here are some simple rules for defining a function in Python: • Function blocks start with the def keyword followed by the name ofthe function and the parenthesis (()).  • Any input parameters or arguments should be placed within these brackets. Inside these brackets, you can also define parameters.  • The first statement of a function can be an optional statement-a document string of a function or a document string.  • The code block within each function starts with a colon (:) and is indented.  • The [expression] return statement exits the function, andoptionally returns the expression to the caller.  • The return statement with no arguments is the same as the return of None.

  5. Eg: whether x is even or odd def even Odd( x ):   if (x % 2 == 0):          print "even"      else: print "odd" even Odd(2) even Odd(3) • O/P: even odd

  6. Eg 2

  7. Types of arguments • There are 4 types of arguments in python.

  8. Positional/Required Arguments • Required arguments are the arguments passed to a function in correct positional order. During function call, values passed through arguments should be in the order of parameters in the function definition. • Hence, the number of arguments in the function call should match exactly with the function definitionelse it gives error.

  9. E.g. of Positional Arguments • E.g.:def add(a, b , c): return (a +b +c ) print (add(10,20,30)) • Output:60 • Note:10 is assigned to a,20 is assigned to b and 30 is assigned to c

  10. Key worded Arguments • Here function parameters are mentioned while calling the function as well. • You can also send arguments with the key = value syntax. • Thus values can be given in any position or order. • Also, if we have positional and key worded arguments in same function then key worded argument should always come after the positional argument else it gives error.

  11. E.g.: • def add(a , b , c , d):total sum = a +b +c + dtotal _ prod = a*b*c*d return total _ sum, total _ prodadd(d = 30, c = 90, a = 10, b = 40) • O/P: 170 1080000

  12. Eg 2: • Mix of positional arguments and key wordedarguments:key worded arguments should always come after the positional arguments • def add( a, b ,c ,d ):total _ sum = a +b +c + dtotal _ prod = a*b*c*d return total _ sum, total _ prodadd( 30,  90, d= 10, c= 40) • O/P: 170 1080000

  13.  Default Parameters/Arguments  • E.g. :def add( a, b, c = 0,d = 0):  print( a, b, c, d )total _ sum = a + b + c + d    return total _ sumprint(add(10,20)) • O/P:30 a=10,b=20,c=0,d=0 • print(add(10,20,30)) • O/P:60 ...a=10,b=20,c=30,d=0

  14. Mix of positional and default - default should follow the positionalMix of positional and default - default should follow the positional • E.g.:def add( a, b, c = 0,d = 0):total _ sum = a + b + c + d  return total _ sumprint(add(10,20,d = 30)) • O/P: 60

  15. Variable length arguments • These are basically of 2 types: • *args:In this case all the arguments are accepted in the form of tuple. Any number of arguments are accepted. • **kwargs: variable length key worded argument: Here, all the arguments are accepted in the form of dictionary.

  16. Example of *args • def add(*var):  print(var, type(var))   total = 1   for i in var:      total = total * i    print(total) add(10) • O/P: (10,) <class 'tuple'> 10 • add(10,20) • O/P: (10, 20) <class 'tuple'> 200

  17. Example of **kwargs • def add(**var)   print(var, type(var))  total = 0    for i in var:  total = total + var[i]  print(total)add(a = 10) • O/P: {'a': 10} <class 'dict'> 10 • add(a=10,b=20,c=30,d=40,e=50) • O/P:{'a': 10, 'b': 20, 'c': 30, 'd': 40, 'e': 50} <class 'dict'>150

  18. Thanks For Watching! • For more such content visit: http://bit.ly/DatascienceCourse • Or follow us on our social media platforms: • Facebook: Learnbay • Instagram: Learnbay_datascience • Twitter: Learnbay1 • LiknkedIn: Learnbay • Youtube (Tutorials): https://www.youtube.com/channel/UC-ntE_GnjjiUuKYqih9ENYA

More Related