Devnote

Web Development Blog Company, Services India

MySQL Error 121 Duplicate key on write - Practical Repair Strategy
MySQL Error 121 Duplicate key on write – Practical Repair Strategy
February 13, 2026
PHP cURL error 60: SSL certificate problem - Windows & Linux Fix
PHP cURL error 60: SSL certificate problem – Windows & Linux Fix
February 11, 2026
Fix localhost refused to connect After Installing XAMPP / WAMP
Fix “localhost refused to connect” After Installing XAMPP / WAMP
February 9, 2026
Composer Error Memory exhausted When Installing Packages - Real Fix
Composer Error: Memory exhausted When Installing Packages – Real Fix
February 6, 2026
MySQL 8 Error Unknown collation: utf8mb4_0900_ai_ci - Safe Migration Guide
MySQL 8 Error Unknown collation: utf8mb4_0900_ai_ci – Safe Migration Guide
February 4, 2026
Devnote

Type and hit Enter to search

  • Home
  • Laravel
    • Auth
    • Migration
    • DATATABLE
    • Yajra
  • wordpress
    • plugin
    • WPBakery
    • woocommerce
  • PHP
    • MYSQL
    • Ajax
  • Blog
  • Informational
    • About us
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
  • Contact US
Devnote
  • Home
  • Laravel
    • Auth
    • Migration
    • DATATABLE
    • Yajra
  • wordpress
    • plugin
    • WPBakery
    • woocommerce
  • PHP
    • MYSQL
    • Ajax
  • Blog
  • Informational
    • About us
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
  • Contact US
create function in python

How to create a function in python

Image
Devnote team
November 9, 2020
2
752 Views
0 Comments

This tutorial is how to create a function in python. Python function is a block of code that performs some specific task. and Its function name has unique.

The python function’s main aim of the program more readable, and organized. and Reduce repeated code.

Define a function

Create in python function use def keyword. Define function name is unique for the function and in parenthesis, you can specify your parameters.

Function block starts with colon(:).

Syntax

def function_name(paramenter):
     # statement

Example

def fun_call():
     print("Python function is called!")

fun_call() function which not take any arguments. when fun_call function called It print text on the screen.

Call a function

Python function call to specify the function name and specify the parameters in parentensis().

Syntax

def function_name(parameter): # define function
     # statement

function_name(parameter) # call function

Example

def fun_call():
     print("Function called successfully!")
fun_call()
fun_call()

Output:

Function called successfully!
Function called successfully!

Function with parameter

Python function call to specify the parameter in the function different-different parameters.

Syntax

def function_name(parameter):
     # statement
function_name(parameter)

Example

def checkoddeven(number):
    if number%2 == 0:
        print("Even number")
    else:
        print("Odd number")
checkoddeven(30)
checkoddeven(7)
checkoddeven(55)

Output:

Even number
Odd number
Odd number

Function with return

Python function Use return statement to return value from the function.

Syntax

def function_name(paramenter):
    # statement
    return value

Example

def checkMaxValue(list_data):
    max_val = list_data[0]
    for val in list_data:
        if val > max_val:
            max_val = val
    return max_val
list_data = [1,32,33,55,98,99,56,46,22]
print("Maximum value is :",checkMaxValue(list_data))

Output:
Maximum value is : 99

Default value

The python function call to the Default parameter automatically gets even if don’t pass arguments while calling the function.

Syntax

def function_name(variable1=value,variable2=value,n):
    # statement

Example

def studentInfo(firstname,lastname="Joi",age=33):
    print("first name : ",firstname,", last name : ",lastname,", age : ",age)

studentInfo("Die")
studentInfo("Hitesh",age=22) # Change age
studentInfo("Emaly","Kill") # Change lastname

Output:
first name :  Die , last name :  Joi , age :  33
first name :  Hitesh , last name :  Joi , age :  22
first name :  Emaly , last name :  Kill , age :  33

Multiple parameters

The python function call to handling an unknown amount of items to function we use an asterisk (*)
sign-in function.

Syntax

def function_name(*argument):
    # statement

Example

def display_fun(name,*friends):
    print("Name :",name)
    print("Friends :",lastname)

display_fun("Emily kill","joi","die","john")

Output:

Name : Emily kill
Friends : ('joi', 'die', 'john')

*friends variable is converts the friends argument into a tuple.

Categories:

python

Other Articles

show all MySQL databases from the command line
Previous

How to show all MySQL databases from the command line

Command line arguments
Next

Command line arguments in Python

No Comment! Be the first one.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related Posts

How to add a date filter to Django admin?

How to add a date filter to Django admin?

February 1, 2021
How to remove default apps in Django admin?

How to remove default apps in Django admin?

January 29, 2021
Remove an item from a list in Python

How to remove an item from a list in Python

January 28, 2021
How to change Django administration text?

How to change Django administration text?

January 25, 2021
Devnote

Devnote provides a collection of tutorials about PHP, Laravel, WordPress, Django, MySQL, Bootstrap, Jquery, Ajax, APIs, CRUD operations, etc.

Devnote © , All Rights Reserved.

Page Links

  • Home
  • About us
  • Blog
  • Contact US
  • Site map

Category

  • PHP
  • Laravel
  • wordpress
  • HTML
  • jQuery

Follow Us

Facebook Instagram Twitter Youtube Linkedin Pinterest
Morbi, Gujarat, India - 363641
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Cookie settingsACCEPT
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT
Advertisement