What is the best way to master "writing a function" in python?

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

Hello Tutor,

What is the best way to master "writing a function" in python?

Please also provide any example or any helpful url links.

Thanks

Expert Solution
Introduction

I assume you know what are python functions. if yes please proceed to step 2.

If not, let me give you a brief introduction to python functions.

Function: Function is a block of reusable code, used to perform repetitive tasks.

python functions are three types:

  • Built-in Functions 
  • User-defined Functions

  • Anonymous Functions

Built-in Functions: In python, many ready-to-use functions are available which are called built-in functions.

Example: print(), range() etc.,

User-defined Functions: Functions defined by the user to accomplish a certain task are called user-defined functions. It can have parameters and return values based on the requirement.

Syntax:

              def function_name():

                  /*----code goes here ---*/

Example:

              def add():                        output: 5

                a=2

                b=3

                print(a+b)

Anonymous Functions: Anonymous functions are also known as lambda functions. These functions are defined without a name and unlike using def we use lambda to define these functions. It can have any number of arguments but can have only one expression.

Example:

    cube = lambda x: x*x*x            output: 27

    print(cube(3))

 

 

 

steps

Step by step

Solved in 2 steps

Blurred answer
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education