Python explanation - Python Introduction to functions

Pybeginner
By -
0

let us explain some Python basics.

You can read or watch the explanation




I assume that all of you have already installed python. If you haven’t installed python yet then 


here’s the link : https://www.python.org/downloads/


It is quite easy and it will not take much time to install. After installing python, It’s time to write your very first program. 


Steps to write your first program in other programming languages.


Step 1 : Writing a Code


You will always be writing code to print “Hello world” right? Let’s try something different this time. Now put just one line into your newly opened editor window.



print("Hello Girl Coder")


Here I’m printing my name. You better print your name. 


Step 2 : Saving and Running the Program


Now try to run the code. In your own editors after saving it. To save it, create a folder on the desktop as pypro fies I.e., python programming files and press F5 to run it.


Step 3 : Getting the output


we will get the output  as “Hello Girl Coder”. 


What’s next ?


Up to now, Everything is perfect. Let’s detach the closing parentheses and run the code and check what happens?


You got an error right? We will get back to this topic later. For now let’s focus on code, So reset all those changes that you’ve made. 


As you can see, the first program consists of the following parts:


  • the word print;
  • an opening parenthesis;
  • a quotation mark;
  • Hello and Your name;
  • another quotation mark;
  • a closing parenthesis.

 


Each of the above plays a very important role in the code.


Word print :


The word print that you can see here is a function name.

 

You’ve probably encountered the term function many times before, during math classes. You can probably also list several names of mathematical functions, like sine or log.


Python functions are more flexible, and can contain more content than their mathematical siblings.


What is a function then? 


A function (in this context) is a separate part of the computer code which is able to do 2 basic things.


  • causing some effect
  • evaluate values


Let’s understand these 2 things briefly. 


Causing some effect : It is nothing but doing something. You will create functions to do something. Examples like opening some files, playing songs. Even now whatever you’re doing right now comes under a function. Everything that you are dealing with in your computer is a complete set of functions. like creating a folder is a function, opening a web browser is a function. So, Causing some effect means doing something. So, you will write functions to do something. Alright… perfect.


Evaluating values: You understand that you write functions to do something like opening files, playing music etc. Not only these things you can also make mathematical calculations using functions I.e functions are also used for evaluating values like, finding the square root of a value or other numeric calculations. This is what makes Python functions the relatives of mathematical concepts.


Moreover, many of Python’s functions can do the above two things together. That means you can write a function that can cause some effect and evaluates some values at the same time. 


Now we understand clearly what functions are. Well I have a doubt, Did you get the same doubt?? 


 Where do the functions come from?


Let’s know about it


The functions may come from Python itself i.e., built-in. So, the print function is one of this kind. it is built-in and you don’t have to do anything special, if you want to make use of it. 


For example, If you want to use print function or method. You are asking python to make this job done by calling print. So, if you want to make use of something and if python has it ready for you, You can use it.


Sometimes, you have to install if the function that you are looking for is not there in python modules. Functions are not only built-in, you can also create functions yourself, placing as many functions as you want based on your need inside your program to make it simpler, clearer and more elegant.

 

But if you are creating your own function, make sure that the


The name of the function should be significant (the name of the print function is self-evident).


Of course, if you’re going to make use of any already existing function i.e., built-in function, you have no influence on its name, but when you start writing your own functions, you should consider carefully your choice of names.


I hope your doubt was clear.


So, the functions may come from 


  • Modules
  • User defined


Coming to the print() function

 


As I said before, a function should Cause an effect and Evaluate values.

 

There’s also a third, very important, function component – the arguments.


If Mathematics is your favourite subject, then you would definitely know what is an argument. Mathematical functions usually take one argument, e.g., You know about sin(x) where x inside sin is an argument, which is the measure of an angle.


Python functions, on the other hand, are more versatile. Depending on your needs, they may accept any number of arguments that are necessary and these arguments are kept inside parenthesis


Note: Some Python functions don’t need any arguments. Even if you didn’t specify any arguments. It is important to represent those parentheses


Python functions strongly demand the presence of a pair of parentheses – opening and closing ones, respectively. This is called standard convention of functions.


 The function we’re talking about here is print().


Does the print( ) function in this example have any arguments? Of course it does, but what are they?


The only argument delivered to the print( ) function in this example is a string. As you can see, the string is delimited with quotes – in fact, the quotes make the string – they cut out a part of the code and assign a different meaning to it.


You can imagine that the quotes say something like:

“The text between us is not code. It isn’t intended to be executed, and you should take it as it is.”


Almost anything you put inside the quotes will be taken literally, not as code, but as data. Try to play with this particular string – modify it, enter some new content, delete some of the existing content. There’s more than one way to specify a string inside Python’s code, but for now, this one is enough.


So far, you have learned about two important parts of the code: the function and the string.


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


Post a Comment

0Comments

Post a Comment (0)