Python explanation - Keyword arguments and functions in Python

Pybeginner
By -
0

 let us explain some Python basics.

You can read or watch the explanation




Keyword arguments and functions


Now let’s understand how print( ) exactly works.


What happens when Python encounters an invocation like this one? where invocation means calling the function.


      print("Hello… Girl Coder")

 

      Function(argument)


Any guesses?


First It checks.


The function that you specified is legal or not.


In the code you've used the print function. Python will check whether it is legal or not. 

How will it be checked?

Since print is an inbuilt function, Python browses its internal data to find an existing function of the name that you’ve given which is print if there is no such function then Python aborts the code it means python stops execution.


Let us assume that python has found the print function. 


The second thing is python checks about the function’s requirements i.e.,the arguments.


For example:

if a specific function demands exactly two arguments and if you send one or three arguments then python treats it as an error and aborts the code’s execution.



Coming to the arguments, print will take string as arguments. You can give as many strings as you can.


Let’s modify the code to :



print("Hello… Girl Coder")


You can see the same output that you’ve seen before. To understand it more clearly let us give one more argument.


So the code would be as



print("Hello…", "Girl Coder”,”Welcome to Python Programming")


Save it and press F5 to run it.


You can see that the print function takes as many strings as it can.


Not only strings, It will also takes virtually all types of data offered by Python. Strings, numbers, characters, logical values, objects – any of these may be successfully passed to print().You will understand it clearly in coming blogs.


Now python checks these arguments. If it satisfied then


Now, Python leaves your code for a moment and jumps into the function you want to invoke by taking the arguments and passes them to the function.


And then the function executes its code and finishes its task.


So Python executes the function in four steps:

It checks 


  1. The specified name of a function is legal or not.(if correct)   
  2. checks about the function’s requirements i.e.,the arguments.(If everything is okay then goes to third step i.e.,)
  3. jumps into the function you want to invoke by taking the arguments and passes them to the function.
  4. Then the function executes its code and finishes its task.

These are the 4 basic steps that every function follows in Python. 



Let’s play with print function and learn some more interesting things about it.


And let’s change code a bit to 



print("Hello…", "Girl Coder", end=' ')



print("Welcome to Python programming")


And here we go, you will be seeing the output like 


Hello... Girl Coder Welcome to python programming 


You need to understand what is the end doing here and What is this thing actually?


End is a keyword argument. What is a keyword argument then?


Keyword argument means you are sending an already existing argument as parameters. 


Here you are sending ‘end’ as an argument which is the keyword.

  • a keyword argument consists of three elements: a keyword identifying the argument (end here); an equal sign (=); and a value assigned to that argument;
  • any keyword arguments have to be put after the last positional argument (this is very important)

Positional argument is nothing but, The argument which specifies the position of the object. In this, the object is a string and the position is the end of the string.

In our example, we have made use of the end keyword argument, and set it to a string containing one space. 


There is another keyword argument called sep let’s use it and check what it does



print("Hello Girl Coder", sep = "-")


As you can see in the output, it acts like a separator. It is one of the keyword arguments that you can use as a separator.


You can use dash  or slash or anything as a separator. You can also keep an empty string.


One more interesting thing is you can use both keyword arguments at the same time in the same function. Let me show you.



print("Hello", "Girl", "Coder", sep = "-", end='*')
print("welcome", "to","python", " programming",sep='*',end="\n")  


You will understand the difference between end and separator by this single example.


Let’s save it and run it.


You are printing 2 lines of code in the same line separated with space in between them using end and you are separating inline strings using sep.


So far, you have learned about :


How many steps python will take to execute a function.
What are keyword arguments?

In the next blog we will go through some more interesting stuff related to Python programming. 


Post a Comment

0Comments

Post a Comment (0)