Author Archives: Jorge Padilla

Nesting of conditonal statements

I made a Python program with a basic nesting of conditional statements. Check it out.

Nesting is about having an if statement within another if statement where the second if statement depends on wether the first if statement is true or not. Then, with the second if statement comes an else, where you define what you want the program to do if the second if statement is false. Here’s a very simple and easy example of nesting. 🙂

Creation and use of tuples in Python

To make easy the explanation of tuples, we can compare them with lists, this lists called tuples have something different, the items that are inside the tuple can not be changed, all the items that are inside are going to stay there as they where typed when the tuple was created. To create a tuple, you just have to type the name of the tuple followed by an equal and then between parenthesis the items that are going to be inside the tuple, separated each one with a comma and between quotation marks, for example: name = (“Jorge”, “Padilla”)

Estimating e

I created a Python program that gives you the Euler constant with the decimal point you want. Check it out.

Creating and using a Python module

Let’s get clear what a module is.A module is a Python file that contains pre-built functions. Now,  to creat a module we have to creat a file with the extension “.py”,(that represen tha the file is a file python),and save it on the main folder of python.The direction of this folder can be found on C:Python34.

In this file you can create as many functions as you want after you’ve create’d your function you saved. So now that your functions are in you module and your module is in your main folder of python, you can use it, writing “import” followed the name of the module. But that is not all, with “import” you open the module but now you have to choose which fuction you are going to use. You have to write the name of the module, point, the name of the function and in parentheses the variable to which we want to apply the function. Here is a very simple example to help you understand better this

Use of recursion for repetitive algorithms

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

As in the screenshot below we can observe that a recursion occurs when you use a function within the same function. It may sound weird and tricky but after you see the example you will notice that it’s 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.

When to use what type of repetition in a program

Using recursion will make your code easier to understand and shorter than other repetitions, but sometimes it’s just not the best way to do it.You have to make the code longer and with more statements.If you want your code to be super effective all the time you have to be very carful about what type of repetition you are going to use, because yes, the recursion is easier but when time is something to be considered you shouldn’t use the recursion type, because it takes more time than other types of repetition.

Creation and use of ranges in Python

Loops created with “for”  necessarily need to have ranges. First write for, then the variable this loop is applying like x, after this goes in range, and in parenthesis the number it will stop, and it will start from 0 (8), another option is to separate with a comma the number you want to start and the number you want it to stop, (0,-8), the last way of using the range is first the numer it is going to start, then separated with a comma the number it is going to end, and sepparated from another comma the number of numbers it will count per time. For example: for i in range (1,sz+1):

 

 

Untitled

To make easy the explanation of tuples, we can compare them with lists, this lists called tuples have something different, the items that are inside the tuple can not be changed, all the items that are inside are going to stay there as they where typed when the tuple was created. To create a tuple, you just have to type the name of the tuple followed by an equal and then between parenthesis the items that are going to be inside the tuple, separated each one with a comma and between quotation marks, for example: name = (“Jorge”, “Padilla”)

Importing and using Python modules

I made a Python program with a simple example of how to import and use a module. 

Basic types and their use in Python

The basic types of data that python works with are:

1.- Integer: these are whole numbers from negative to positive, they can be used in the program simply writing the number in the program like 45 and to turn an input into an integer write int before the input.

2.- Float: floats are numbers with decimals like 2.5, to turn an integer into float just write float before it.

3.- strings: a set of letters, number or other characters, these dont hold any value appart from the character itself, a  number written as a string wont be counted as a numeric value. strings are between ” ” to differentiate them.

4.- Tuples: a list with a fixed number of elements, they are use ( ).

5.- List: a list without a fixed number of elements, they use [ ].

6.- Dictionaries: a list of multiple elements that can be adressed by text, they use { }.

You can see more detailed explanations and some examples of these different types of data. Just click on the following link: http://en.wikiversity.org/wiki/Python/Basic_data_types