Basic Python - Functions


NaN years ago

A function is a block of code that only runs when it is called. You can pass data, known as parameters, into a function. Furthermore, it avoids repetition and makes the code reusable.

Creating a Function

def Function_name():
  print("Hello from a function")

Calling a Function

def first_function():
  print("Hello from a function")


Function with arguments

def second_function(name):
  print(name + "satyam")


Function with arbitrary arguments, *args

def third_function(*names):
  print("The second person is " + names[2])

third_function("Emil", "Tobias", "Linus")

Function with arbitrary keyword arguments, **kwargs

def fouth_function(**name):
  print("His last name is " + name["lname"])

fouth_function(fname = "satyam", lname = "sharma")

Function with default parameter value

def fifth_function(country = "China"):
  print("I am from " + country)

fifth_function("Sweden") // its prints Sweden
fifth_function("India") // its prints India
fifth_function() // its prints China

Basically, we can divide functions into the following two types:

Built-in functions: Functions that are built into Python.

User-defined functions: Functions defined by the users themselves.

approvedApproved by experts
Advance Topics

Neuton's Mission

We are a team of young and enthusiastic people who are passionate about education and want to help students to learn the skills they need to succeed in life. If you want to support us, please join our community.