Python functions with arguments

Here you learn about python functions, writing different of python function syntax, basic built-in functions, user-defined functions and return different data type.

How to write Python code basic syntax

Best way to understand and practice python basic syntax without creating any complex project, just create a simple python application (type of console app), then write following code and run the application to see the output on console screen.

Once you create a python project, a default python file with extension .py will be added, you can rename the file to anything (if you want), then write a simple print statement and run the project, if you see the your message on console screen, you are set to start writing python code.

print("Welcome to python world!")

How to write functions in Python

The function we write often termed as user-defined function in Python.

Functions are re-useable piece of code to perform some specific task for any business requirement, ideal function usually follow Single Responsibility Principal, which means a function carry out only one task / one calculation. There are also many built-in functions that come with python packages and libraries.

Like any other programming language we can write function in python with and without arguments, also can call built-in functions.

Here are few strong reasons why we should follow best practice guidelines while writing functions in python.

  • It helps reducing coding times drastically, makes code meaningful, easy to read, improve re-usability
  • It can support modular design approach, very helpful while working in big projects with multiple developers across remote locations, one developer can easily understand and depend on other developer’s function
  • Easy to figure out error or issue with system, and fixing only that function to resolve the whole problem
  • As business requirement keep on changing consistently, we know which function to alter to match the demand
  • Should follow single responsibility principal (SRS) while writing any function, that helps designing any complex enterprise system easily

Here is a simple function definition with one parameter

def welcomeStudent(name):
    print("Welcome ", name)

This is how you can call the above function

welcomeStudent("Bubai");

We can also write function with optional parameter

def welcomeStudent(name="Friend"):
    print("Welcome ", name)

Now we can call the function in two differ ways, with and without parameter, if we don't pass any value then the default value will be printed, else the default value will be over written.

# calling without parameter
welcomeStudent();

# calling with parameter
welcomeStudent("Ratan");

Here is how we can create a function that accepts a list as parameter.

def myCountries(countries):
    for c in countries:
        print(c)

Here is how we can pass a list of parameters

# storing comma separated values in a variable
countries = ["India", "Singapore", "Canada", "USA"]

# calling function with above parameter
myCountries(countries)
Return a Dictionary from Python function

How to write function in Python that returns dictionary type of data, Returning a dictionary object from python function. Here is an example

def contact():    
return dict(
    name='Richard Williams',
    mobile='98214785414',
    address="Kolkata, India",
    email="rw@wtr.com"
)

Instead of hardcoded value we also can set dictionary values from function arguments value.

def contact(Name,Mobile,Adress,Email):    
return dict(
    name=Name,
    mobile=Mobile,
    address=Adress,
    email=Email
)
friend1=contact("Arunavo","98470457","Bangalore","arunavo@gmail.com")
Commenting in Python Code, Single line and Multiline

Like any other programming language, in python also we have a way to write comment in between our code, which is part of software development lifecycle.

v1=100
print (v1)
# single line comment example

Here is an example of how we can make multiline comment in Python

""" Line 1, 
 line 2 
 line 3.

 line n
"""

Though there is no multiline comment option, but there is a alternate way to achieve that.

"""
This is a "block comment" in Python, here 
we can have multiple lines.
This could be a fantastic way
to write multiline comment in python
"""

Before you start writing program in python, you must practice above basic syntax of writing python code, which will help you to understand advance programming in python.

 
Python Function Example
Learn python programming with free python coding tutorials.
Other Popular Tutorials
User defined function in Python
Python programming examples | Join Python Course