Blog,  Programming,  Python

Python: Let’s have fun learning functions and learn it the easy way using Python predefined functions

Begin understanding functions

A function is not very difficult to do. I promise that in one day of practicing and learning function you will be close to master in programming. Look function is a block of the procedure. Instead of writing procedures line by line. You can tag that procedure in one block. It will call the function. Imagine that you want to make a program. In your program, there is a function that makes a data entry. You will name it “data_IN” functions. Inside of your functions, there is the procedure to enter the data in the database. This is easy and you don’t have to worry about it. We can beat it with one punch.

Do your functions and use it later

The functions that you defined you can call it to use. Yes, uses it where you need it. Example, the “data_IN” functions that we describe above. If we have another program that we need to insert the data into the database. Therefore, we define already the “data_IN” functions so we don’t need to write other procedures that make the same action.

I know this sounds like scramble eggs for you. I recommend you to think that the procedures or action we can tag to only one name. We put the procedures that do the action of the functions in one block. When you define your functions and you happy what it does. You are in a pretty good way.

The definition of functions

Before we jump into the examples. Let see what is functions definition. In the book always start with, “In the context of programming”. We know already that we are learning how to programming. But the book is still concern that way. Anyway, the definition is a “function is a named sequence of statements that performs a computation” (Downey, 2008). I will explain what is statements, are procedures we want the program does. The statements are inside of the functions.

To understand it better I will do some example for you. Thanks to my little ability to do the examples, it is my pleasure to do it for free. You just have to visit my blog. Win to win.

Function calls

Python programming language has already predefined functions that we can use. There is a list of the functions of Python predefined. Remember that every programming languages have each predefined functions.

List of Python predefined functions

bool()Returns the boolean value of the specified object.
bytes()Returns a bytes object
callable()Returns Thre if the specified object is callable, otherwise False
chr()Returns a character from the specified Unicode code
dict()Returns a dictionary(Array)
exec()Executes the specified code (or object)
float()Returns a floating point number
format()Formats a specified value
hash()Returns the hash value of a specified object
help()Executes the built-in help system
id()Returns the id of an object
len()Returns a list
type()Returns the type of an object
int()Returns the object to an integer

There are more predefined functions and you can use at you desire or needs. In this website, you can learn more about the predefined functions of Python. Look the format of the word. Is something like this name_of_function() after the name there is parenthesis.

Open the Python IDLE. Let’s call a function and use of any predefined functions of Python. Here I will be calling the type() function then see what it does. Because functions do something according to what it was made for.

Cool! We did call the type of the function () that returns the type of the object.

  • What is the type of a is an integer, correct.
  • What is the type of b is a float, correct.
  • What is the type of c is a string, correct?

Time to clear something more. The expression inside of the parenthesis is called the argument of the function. The argument is the value that we want the function to process and return the result. In later we will see more about arguments and how we can put multiple arguments if the function allows it.

Type conversion functions

The Python predefined functions are helpful. Try to check first the documentation in the official Python website.

Look if the function is already made so you don’t bother to make it again. The type conversion functions are it helpful when you want to convert a object.

What we converting in this example it was a string to integer and float to integer.

Did you notice that Python return an error when we want to convert the string characters in an integer? It is illegal and not possible because letters are not numbers.

Try to use the function float() to convert an integer in a float.

Math functions

Python has a math module that provides most of the familiar mathematical functions. This module we have to import. We cannot use it if the module is not imported in our program. Look the example.

Did you notice that after the math there is a dot then the function?

This is the way how to access the functions. The math is the module and the log10() is the function not forgetting to put a dot after the module name. The format is called dot notation.

Composition

Is the combination of the elements of a program – variables, expressions, and statements – in isolation.

One of the most useful features of programming languages is their ability to take small building blocks and compose them. This is right you need to know how to combine the elements of a program.

The example:

Did you have fun learning the principle of functions? In the next episode or post of mine. I will learn how to do our new functions. How to write a function and name that function. Likewise, in an easy way. Dad Syntax is here to help you how to programming in Python. As well, Dadsyntax is learning and reviewing programming. This is not intended that you will be a good programmer. However, this is the beginning and the foundation of good learning how to program.

In this blog, there is some pretty good post that you will be interesting. If you visit this blog often you are helping me to post more valuable content. If you want to suggest something. Please contact me.

Reference

Downey, A. (2008). Think Python. How to Think Like a Computer Scientest. Check it out in Amazon: https://amzn.to/2UKrv5V

Leave a Reply

Your email address will not be published. Required fields are marked *