

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to define a function in Python?
A function is a piece of code that is called by name. It can be passed data to operate on (i.e. the parameters) and can optionally return data (the return value). All data that is passed to a function is explicitly passed.
Syntax of defining a function
def function_name(): #do something
A function is defined using the keyword def following a space and the function_name with parentheses and a colon. The next line contain a indented code block to do something
Method is a function that is associated with an object. In Python, method is not unique to class instances. Any object type can have methods.
It is said that everything in Python is an object. In python, functions too are objects. So they have attributes like other objects. We can also assign new attributes to them, as well as retrieve the values of those attributes. Functions can have even functions written inside them.
- Related Questions & Answers
- How to define a function in JavaScript?
- How to define a JavaScript function using Function() Constructor?
- How can we define a Python function at runtime?
- How to define global variable in a JavaScript function?
- Can we explicitly define datatype in a Python Function?
- How do we use function literal to define a function in JavaScript?
- How to define a JavaScript function using the expression?
- How to define functions inside a function body in JavaScript?
- How to define rest parameter for a function in JavaScript?
- How to define and call a function in Lua Programming?
- why Python can't define tuples in a function?
- How to define an Arrow Function in JavaScript?
- How to define custom sort function in JavaScript?
- define() function in PHP
- How to define classes in Python?