Python Tutorials: Functions In Python Part-1

pattern matching with python

Read Time2 Minute, 42 Second

Functions in Python: Now as we have seen the basic data structures in Python, it’s now time to take a look at the functions in Python. Python has some built-in functions like print(), len(),  input() and more.  We’ve seen these in past few articles, but let us discuss these three again.

print() :

As we have seen this in our second post, the print function is used to print the data. This function can be used as follows.

Suppose we have to print your name on the screen. Then-

print ('Shubham')
>> Shubham

And if you want to print a number then-

print (10)
>> 10

Or to print a=10

>> 10

As you can see, the word ‘Shubham’ is written in single quotes and number without that. So you may now have concluded this that if you are going to print a string value, then you must write that in quotes. For the variables or numbers, it is not required.


The len() function returns the length of the particular data string. So if we have to calculate the length of large data, this function will be beneficial.

Take this simple example:

>> 7


The input() function is for taking input from the user. So if you want to make any code which will depend on the input from the user, then this function you have to use.

Example :


>> Shubham

Now as we have seen these inbuilt functions,  Let us see how user-defined functions work.

User Defined Functions:

To understand how user-defined functions work, we will first create one.

Now as you can see, our name function is started with ‘()’ at the end and ‘:’. Which you have to remember as the Python does not have the braces, instead indentation is used.

Just analyze this program, and you can easily understand how a function works. To call the function, just write that function name and it will execute.

Let us look at the two other examples.

As in this example, the def statement is with parameters, a and b. And it is taking values at the time of calling the functions. Meaning is that you can calculate addition at the time of calling the function and there is no need of declaring the values inside the function itself. You can either get those at the time of calling or from the user.

Also, as you can see in this image, we have saved the program as and run it from the shell. Take a note of this If you wonder how to run a python program.

Now take a look at this another simple program.

As you can see, we have taken input from the user using the built-in input() function. The variables p and q are global variables as those are declared out of the function and are used inside the function.

Note: The number of lines of this code can still be reduced but at this stage, just try this simple code.

So just this for now, in the next tutorial we will see if else statement and loops inside a function.