Here you learn about python functions, writing different of python function syntax, basic built-in functions, user-defined functions and return different data type.
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!")
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.
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)
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")
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
Note: there is no multiline comment in Python, Unfortunately :)
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.