Author Archives: Manuel Madrigal

Mastery08. Python Conventions

Python conventions are 20 aphorisims created by Tim Peter, from which only 19 are being written down.

Python conventions, called “The Zen of Python” reflect the pilosophy of how coding should be.

Also there is an easter egg where you import this and the Zen of Pyhton is printed on the terminal..

Mastery23. Creation and use of lists in Python

In this mastery I will explain how to create and use lists in Python.

In the screen capture above we can observe how a list called “numbers” is created but with no elements in it, the elements are added one by one by the user input using the statement  “append” which add the element at the very end of the list.

But lists arent always created in blank, creating a list is as simple as putting different comma-separated values between squere brackets. For example:

list1 = ['physics', 'chemistry', 1997, 2000];

list2 = [1, 2, 3, 4, 5 ];

list3 = ["a", "b", "c", "d"];


Lists are very useful to store values that you can use later for other purposes.

You can learn more about lists here: http://www.tutorialspoint.com/python/python_lists.htm

 

Mastery22.When to use what type of repetition in a program

In mastery 21 I explained what a recursion is, and as I said before, a recursion helps our code to look cleaner and easier for other users to understand, but it is not always the best option to use.

If we want our code to look better yes, but some times we have to make it longer in order to be more effective, what do I mean by that?

For example, if we did code fore an airbag of a car, this code must be as faster as it can, everything has to happen in miliseconds or even nanoseconds if possible.

Here is an example of 2 different functions that do the same thing..

In the first function we can notice that it is using recursion which looks shorter and easier to understand, but the second function, even if it looks rare, it is more effective and it can solve the function faster.

 

Mastery21.Use of recursion for repetitive algorithms

In this mastery I will explain you what is a recursion in a repetitive algorithm.

Recursions are very simple and they are very useful to make your code cleaner.

As in the screen capture above we can observe that a recursion occurs when you use a function inside the same function, it may sound weird but once you see the example you will see that it is not that difficult.

In this case the recursion is used in order to get to the more simple values and it works as a loop that stop until it get to the last value.

Mastery20. Use of loops with for

The syntax of the for loop may be different from other for loops you have used in other languages. For python the syntax is the following:

     for i in range(x):

where “i” is just a variable that will be changing its value each time the loop is completed, and x is the number of times the loop is going to be repeated.

In this case I used “range” as the way of the for loop to recognize how many times it has to loop, but we can use the for loop to go over certain values from a list, as in the red circled statement of the screen capture above.

You can learn more about For loops in here: https://wiki.python.org/moin/ForLoop

 

Mastery13. Importing and Using Python Modules

For this mastery I will explain you how to import and use a module in Python.

A module is like a library that contains certain functions that belong to a particular class, for example, in the screen capture above we can see how to import the module statistics, a module that contains functions for this specific area.

The syntax of importing is just using the word “import”and the name of the module you want to import.

Alfo there is another way to import just a part of the module whis is using the statement “from”. For example if we wanted to just import a specific function of the module, we would use the statement “from” followed by the name of the module, and then the statement “import” followed by the name of the function.

To use a function from the module we imported we have to assign it to a variable, but first we type the name of the module. the name of the function we want to use and in parentheses the variable to which we want to apply the function.

You can check an example on this video: https://www.youtube.com/watch?v=MZlKCdybZrA

Mastery11. Calling Pyhon Functions

In this mastery Im going to explain how to call a function in Python, which is very easy to do.

To call a function you just have to type the name of it followed by the parameter you are going to introduce in that function. Also you can assign the function to a variable so it takes the output of the function.

For example, if we see the screen capture below we can notice that the function “fact” with the parameter “x” is assigned to the variable “y”, so “y” will take wathever value is returned from the function and then we can use “y” to acomplish some other tasks along our program.

You can visit this site to learn more about calling functions: http://www.tutorialspoint.com/python/python_functions.htm

Mastery12. Creating Python Functions

For this mastery im going to explain how to create a function in Python, which is very simple.

You can see in the screen capture above the syntax, which is composed by the word “def” followed by the name you are giving the function and the argument of this one, also, dont forget to put the tho dots at the end. In the following lines you have to define what your function is going to do, and this can vary depending of the functionality of this.

The statement return is the output of the function, is what the function is going to return the user with a giving parameter.

 Also you can check for more information here: http://www.tutorialspoint.com/python/python_functions.htm

Mastery06. Install Linux on my own computer

Ubuntu

For this mastery I had to Install Linux on my computer. After searching a little on the internet I found this option where I had to install first Virtual Box (here is the link: https://www.virtualbox.org/) and then I had to download Ubuntu (which is Linux). You can Download Ubuntu for free here: http://www.ubuntu.com/download.

Also you can check this video where is explained step by step how to install it: https://www.youtube.com/watch?v=RpgjBIGl9RU

WSQ11. Yo soy 196

In this assignment I had to create a program where the user had to enter a sequence of numbers by giving the lower and upper bounds, then the program had to print the number of natural palindromes, non-lychrel numbers and lychrel candidates contained in the sequecence.

In order to accomplish this task, I created a list where each of the numbers was allowed and then, I used a for loop to go over each of these numbes and check to which category they belonged by using a bunch of control flow tools (if, else) and a function that I defined at the begginning used to check if it was a natural palindrome number or not.

Also I added an extra list to allow lychrel numbers and then print them at the end.

You can check my code here: https://github.com/Manuelmv94/TC1014/blob/master/wsq11.py